本博客以当前RocketMQ最新版介绍:v4.4.0
环境要求 64位JDK 1.8+; Maven 3.2.x; // 源码编译时需要用到 二进制文件安装 下载二进制文件:http://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.4.0/rocketmq-all-4.4.0-bin-release.zip 二进制文件放到任意目录(由于是纯净的ubuntu镜像,docker环境,因此我放在/root/xxx目录下) 解压zip包,并重命名文件名> unzip rocketmq-all-4.4.0-bin-release.zip && mv rocketmq-all-4.4.0-bin-release rocketmq启动server
> cd /root/rocketmq> nohup sh bin/mqnamesrv & // 第一次安装时,可执行sh bin/mqnamesrv观察是否能够启动> tailf -f ~/logs/rocketmqlogs/namesrv.log // 观察到以下日志时,server已启动成功2019-09-07 18:06:13 INFO main - The name Server boot success. serializeType=JsON启动broker
> nohup sh bin/mqbroker -n localhost:9876> tailf -f ~/logs/rocketmqlogs/broker.log // 观察到以下日志时,server已启动成功2019-09-07 20:40:06 INFO main - The broker[0daf9bd41237,172.17.0.2:10911] boot success. serializeType=JsON and name server is 172.17.0.2:9876
测试生产者和消费者注:broker启动如果过一会直接退出,无任何日志或报错的话,检查一下机子的内存是否充足。RocketMQ的broker默认内存为8g。
修改文件:/root/rocketmq/bin/runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -xms1g -Xmx1g -Xmn512m"
// 在测试之前,我们需要先设置环境变量:export nameSRV_ADDR=localhost:9876> sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer SendResult [sendStatus=SEND_OK,msgid= ... > sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer ConsumeMessageThread_%d Receive New Messages: [MessageExt...
Producer的源码
public class Producer { public static voID main(String[] args) throws MQClIEntException,InterruptedException { // 默认消费组 DefaultMQProducer producer = new DefaultMQProducer("default"); producer.start(); for (int i = 0; i < 1000; i++) { try { Message msg = new Message("topicTest" /* topic */,"TagA" /* Tag */,("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAulT_CHARSET) /* Message body */ ); // 消息发送 SendResult sendResult = producer.send(msg); System.out.printf("%s%n",sendResult); } catch (Exception e) { e.printstacktrace(); Thread.sleep(1000); } } // 关闭生产者 producer.shutdown(); }}
Consumer的源码
public class Consumer { public static voID main(String[] args) throws InterruptedException,MQClIEntException { // 指定消费组 DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("default"); // 设置消费偏移点 consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FirsT_OFFSET); // 订阅topic,以及tag consumer.subscribe("topicTest","*"); // 注册消息监听器 consumer.registerMessageListener(new MessageListenerConcurrently() { @OverrIDe public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs,ConsumeConcurrentlyContext context) { System.out.printf("%s Receive New Messages: %s %n",Thread.currentThread().getname(),msgs); // 收到数据后,返回ack确认 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } }); consumer.start(); System.out.printf("Consumer Started.%n"); }}关闭server和broker
> sh bin/mqshutdown brokerThe mqbroker(36695) is running...Send shutdown request to mqbroker(36695) OK> sh bin/mqshutdown namesrvThe mqnamesrv(36664) is running...Send shutdown request to mqnamesrv(36664) OK总结
以上是内存溢出为你收集整理的RocketMQ在linux下安装部署全部内容,希望文章能够帮你解决RocketMQ在linux下安装部署所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)