专题目录
RocketMQ详解(一)原理概览
RocketMQ详解(二)安装使用详解
RocketMQ详解(三)启动运行原理
RocketMQ详解(四)核心设计原理
RocketMQ详解(五)总结提高
引子
本节比较轻松,做2个事:
- 1.在本地安装:RocketMQ NameServer名字服务+Broker代理服务+Dashboard看板。
- 2.使用简单样例,实现消息发送、消息消费。并在Dashboard上验证。
一、安装流程
目标:
- W10上本地安装启动RocketMQ
- RocketMQ DashBoard 看板(即老的console)
1.1 安装环境
JDK8(jdk7+),验证如下:
maven 3.8.2,验证如下:
1.2 安装RocketMQ
1.下载bin压缩包并解压缩
去官网下载安装包:https://rocketmq.apache.org/dowloading/releases/
点击进入https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.8.0/rocketmq-all-4.8.0-bin-release.zip,如下图:
如上图,点击链接下载rocketmq-all-4.8.0-bin-release.zip,解压缩如下:
2.配置系统环境变量
变量名:ROCKETMQ_HOME
变量值:D:\rocketmq-all-4.8.0-bin-release
3.启动nameserver
进入D:\rocketmq-all-4.8.0-bin-release\bin目录下,执行:start mqnamesrv.cmd,如下图:
如上图,name server启动成功。 注:不要关闭窗口。
4.启动broker
进入D:\rocketmq-all-4.8.0-bin-release\bin目录下,执行:start mqbroker.cmd -n 127.0.0.1:9876 autoCreateTopicEnable=true,如下图:
如上图,broker启动成功,broker name=GZYZFA00071414(这个名字记住,后面验证要用), 关联的name server是127.0.0.1:9876。注:不要关闭窗口。
1.3 安装RocketMQ DashBoard看板
1.下载zip包并解压缩
注意:最新的官方后台看板只有 git: RocketMQ DashBoard,(https://github.com/apache/rocketmq-externals这个项目已经没有看板了):
rocketmq-dashboard-master.zip 下载完后解压缩。如下图:
2.修改nameserver地址
到D:\rocketmq-dashboard-master\src\main\resources下修改application.properties,修改nameserver地址。
3.maven编译并启动spring boot
进入D:\rocketmq-dashboard-master 目录,执行 mvn spring-boot:run,几分钟后,熟悉的Spring Boot banner出来了:
4.访问验证
访问http://localhost:8080,看板如下图所示,此时还没消息。
查看nameserver, 如下图:
安装完毕,下一节我们来看看怎么使用。
二、简单使用
我们使用官方简单样例来验证:https://github.com/apache/rocketmq/blob/master/docs/cn/Example_Simple_cn.md。
2.1 消息发送(生产者)
1 @Test
2 public void sandMessageTest() throws InterruptedException, RemotingException, MQClientException, MQBrokerException, UnsupportedEncodingException {
3
4 // 实例化消息生产者Producer
5 DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
6 // 设置NameServer的地址
7 producer.setNamesrvAddr("localhost:9876");
8 // 启动Producer实例
9 producer.start();
10 for (int i = 0; i < 10; i++) {
11 // 创建消息,并指定Topic,Tag和消息体
12 Message msg = new Message("TopicTest" , "TagA" ,""+i,
13 ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
14 );
15 // 发送消息到一个Broker
16 SendResult sendResult = producer.send(msg);
17 // 通过sendResult返回消息是否成功送达
18 System.out.printf("%s%n", sendResult);
19 }
20 // 如果不再发送消息,关闭Producer实例。
21 producer.shutdown();
22 }
如上图,
1)先启动producer
2)同步发送10条消息,topic='TopicTest', tag="TagA",keys=""+i, 消息体=Hello RocketMQ +i。
3)关闭生产者。
运行日志:
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2840000, offsetMsgId=AC133C2C00002A9F0000000000000000, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=8], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2AB0001, offsetMsgId=AC133C2C00002A9F00000000000000D0, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=9], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2AC0002, offsetMsgId=AC133C2C00002A9F00000000000001A0, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=10], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2AE0003, offsetMsgId=AC133C2C00002A9F0000000000000270, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=11], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2B00004, offsetMsgId=AC133C2C00002A9F0000000000000340, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=12], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2B10005, offsetMsgId=AC133C2C00002A9F0000000000000410, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=13], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2B30006, offsetMsgId=AC133C2C00002A9F00000000000004E0, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=14], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2B40007, offsetMsgId=AC133C2C00002A9F00000000000005B0, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=15], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2B60008, offsetMsgId=AC133C2C00002A9F0000000000000680, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=0], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=AC133C2C0E0018B4AAC2515ED2B70009, offsetMsgId=AC133C2C00002A9F0000000000000750, messageQueue=MessageQueue [topic=TopicTest, brokerName=GZYZFA00071414, queueId=1], queueOffset=0]
19:12:48.835 [NettyClientSelector_1] INFO RocketmqRemoting - closeChannel: close the connection to remote address[172.19.60.44:10911] result: true
如上图所示,接收到10条发送结果日志,msgId消息ID,topic=TopicTest, brokerName=GZYZFA00071414,queueId=均匀分布在0-15一共默认的16个队列中的10个。
打开dashboard查看消息如下图:
点击消息详情按钮,查看一个消息:
如上图所示,topic、tag、key、消息体都是正确的。checked!
2.2 消费消息(消费者)
官方样例,一个简单的并发消费,如下:
1 @Test
2 public void consumerMessageTest() throws MQClientException, InterruptedException {
3 // 实例化消费者
4 DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");
5
6 // 设置NameServer的地址
7 consumer.setNamesrvAddr("localhost:9876");
8
9 // 订阅一个或者多个Topic,以及Tag来过滤需要消费的消息
10 consumer.subscribe("TopicTest", "*");
11 // 注册回调实现类来处理从broker拉取回来的消息
12 consumer.registerMessageListener(new MessageListenerConcurrently() {
13 @Override
14 public ConsumeConcurrentlyStatus consumeMessage(List msgs, ConsumeConcurrentlyContext context) {
15 System.out.printf("%s Receive New Messages: %s ,body= %s %n ", Thread.currentThread().getName(), msgs,new String(msgs.get(0).getBody()));
16 // 标记该消息已经被成功消费
17 return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
18 }
19 });
20 // 启动消费者实例
21 consumer.start();
22 System.out.printf("Consumer Started.%n");
23 // 史前巨坑,这里官方样例不带这个,加上才能来得及消费。
24 Thread.sleep(10000L);
25 }
如上图,
1)构造了一个消费者实例,并设置了nameserver、订阅条件(topic+tag表达式)。
2) 注册了一个并发消息监听器,定义如何消费消息。
3)启动消费者,执行消费。这里最后一行Thread.sleep(10000L);要记得加上,不加还没消费线程就被关闭了...
执行后,消费日志如下:
Consumer Started.
ConsumeMessageThread_1 Receive New Messages: [MessageExt [queueId=8, storeSize=208, queueOffset=0, sysFlag=0, bornTimestamp=1631790768772, bornHost=/172.19.60.44:59863, storeTimestamp=1631790768795, storeHost=/172.19.60.44:10911, msgId=AC133C2C00002A9F0000000000000000, commitLogOffset=0, bodyCRC=613185359, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=1, KEYS=0, CONSUME_START_TIME=1631839990074, UNIQ_KEY=AC133C2C0E0018B4AAC2515ED2840000, CLUSTER=DefaultCluster, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 48], transactionId='null'}]]
ConsumeMessageThread_3 Receive New Messages: [MessageExt [queueId=14, storeSize=208, queueOffset=0, sysFlag=0, bornTimestamp=1631790768819, bornHost=/172.19.60.44:59863, storeTimestamp=1631790768819, storeHost=/172.19.60.44:10911, msgId=AC133C2C00002A9F00000000000004E0, commitLogOffset=1248, bodyCRC=1307562618, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=1, KEYS=6, CONSUME_START_TIME=1631839990074, UNIQ_KEY=AC133C2C0E0018B4AAC2515ED2B30006, CLUSTER=DefaultCluster, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 54], transactionId='null'}]]
ConsumeMessageThread_5 Receive New Messages: [MessageExt [queueId=1, storeSize=208, queueOffset=0, sysFlag=0, bornTimestamp=1631790768823, bornHost=/172.19.60.44:59863, storeTimestamp=1631790768824, storeHost=/172.19.60.44:10911, msgId=AC133C2C00002A9F0000000000000750, commitLogOffset=1872, bodyCRC=1565577195, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=1, KEYS=9, CONSUME_START_TIME=1631839990074, UNIQ_KEY=AC133C2C0E0018B4AAC2515ED2B70009, CLUSTER=DefaultCluster, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111,
...省略其它
消费完后,查看dashboard: topic->consumer manager可以看到消费延迟为0了,即全部消费掉了。如下图:
三、总结
本节我们实现了本地从0到1安装RocketMQ+Dashboard、并简单实现了消费发送和消费,并在Dashboard上验证消费成功。