RocketMQ是什么?
RocketMQ是一款分布式、队列模型的消息中间件,具有以下特点:
能够保证严格的消息顺序
提供丰富的消息拉取模式
高效的订阅者水平扩展能力
实时的消息订阅机制
亿级消息堆积能力
RocketMQ是alibaba开源的java消息中间件。在github上开源,项目同时也在oschina上。
进入到RocketMQ下载包解压的路径下
D:\machine\RocketMQ-3.0.8\RocketMQ-3.0.8>
接下来安装
执行下边的命令或者执行install.bat(在这个bat文件中的命令如下)对maven熟悉的一眼就知道是执行clean package install assembly等操作。
mvn -Dmaven.test.skip=true clean packageinstall assembly:assembly –U
操作信息如下:
接下来把编译好的项目copy出来
启动服务
//启动mqnamesrv
可以通过jps查看一下是不是有了RocketMQ的进程,如下方的6484
看日志中信息D:\logs\alibaba-rocketmq/mqnamesrv.log
The Name Server boot success.
//启动broker
看日志中信息D:\logs\alibaba-rocketmq/mqbroker.log
The broker[houchangren, 10.57.41.19:10911]boot success.
1. 创建maven项目
Pom.xml文件如下
2. 编写消息产生者Producer
文件路径:/gomeTest/src/main/java/com/ruishenh/rocketmq/example/Producer.java
3. 编写消息消费者Consumer
文件路径:Test/src/main/java/com/ruishenh/rocketmq/example/PushConsumer.java
Producer执行console:
12:05:39.399 [main] DEBUGi.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
12:05:39.405 [main] DEBUGi.n.c.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
12:05:39.411 [main] DEBUGi.n.util.internal.PlatformDependent - Platform: Windows
12:05:39.411 [main] DEBUGi.n.util.internal.PlatformDependent - Java version: 6
12:05:39.411 [main] DEBUGi.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
12:05:39.412 [main] DEBUGi.n.util.internal.PlatformDependent0 - java.nio.ByteBuffer.cleaner: available
12:05:39.413 [main] DEBUGi.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
12:05:39.413 [main] DEBUGi.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
12:05:39.413 [main] DEBUGi.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
12:05:39.413 [main] DEBUGi.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: true
12:05:39.413 [main] DEBUGi.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
12:05:39.413 [main] DEBUGi.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
12:05:39.414 [main] DEBUGi.n.util.internal.PlatformDependent - Javassist: unavailable
12:05:39.414 [main] DEBUGi.n.util.internal.PlatformDependent - You don't have Javassist in your classpath or you don't have enough permission to load dynamically generatedclasses. Please check the configurationfor better performance.
12:05:39.414 [main] DEBUGi.n.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
12:05:39.429 [main] DEBUGio.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
12:05:39.429 [main] DEBUGio.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold:512
12:05:39.481 [main] DEBUGi.n.util.internal.ThreadLocalRandom - -Dio.netty.initialSeedUniquifier:0x653a966ddb02e036
12:05:39.523[NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector --Dio.netty.noResourceLeakDetection: false
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BA16, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=0], queueOffset=66]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BAAB, messageQueue=MessageQueue [topic=TopicTest2,brokerName=houchangren, queueId=0], queueOffset=66]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BB41, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=0], queueOffset=66]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BBD6, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=1], queueOffset=66]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BC6B, messageQueue=MessageQueue [topic=TopicTest2,brokerName=houchangren, queueId=1], queueOffset=66]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BD01, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=1], queueOffset=66]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BD96, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=2], queueOffset=61]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BE2B, messageQueue=MessageQueue [topic=TopicTest2,brokerName=houchangren, queueId=2], queueOffset=61]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BEC1, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=2], queueOffset=61]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BF56, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=3], queueOffset=61]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001BFEB, messageQueue=MessageQueue[topic=TopicTest2, brokerName=houchangren, queueId=3], queueOffset=61]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C081, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=3], queueOffset=61]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C116, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=0], queueOffset=67]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C1AB, messageQueue=MessageQueue[topic=TopicTest2, brokerName=houchangren, queueId=0], queueOffset=67]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C241, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=0], queueOffset=67]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C2D6, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=1], queueOffset=67]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C36B, messageQueue=MessageQueue[topic=TopicTest2, brokerName=houchangren, queueId=1], queueOffset=67]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C401, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=1], queueOffset=67]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C496, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=2], queueOffset=62]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C52B, messageQueue=MessageQueue[topic=TopicTest2, brokerName=houchangren, queueId=2], queueOffset=62]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C5C1, messageQueue=MessageQueue [topic=TopicTest3,brokerName=houchangren, queueId=2], queueOffset=62]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C656, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=3], queueOffset=62]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C6EB, messageQueue=MessageQueue[topic=TopicTest2, brokerName=houchangren, queueId=3], queueOffset=62]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C781, messageQueue=MessageQueue [topic=TopicTest3,brokerName=houchangren, queueId=3], queueOffset=62]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C816, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=0], queueOffset=68]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C8AB, messageQueue=MessageQueue[topic=TopicTest2, brokerName=houchangren, queueId=0], queueOffset=68]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C941, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=0], queueOffset=68]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001C9D6, messageQueue=MessageQueue[topic=TopicTest1, brokerName=houchangren, queueId=1], queueOffset=68]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001CA6B, messageQueue=MessageQueue[topic=TopicTest2, brokerName=houchangren, queueId=1], queueOffset=68]
SendResult [sendStatus=SEND_OK,msgId=0A39291300002A9F000000000001CB01, messageQueue=MessageQueue[topic=TopicTest3, brokerName=houchangren, queueId=1], queueOffset=68]
Consumer执行console:
12:00:49.539 [main] DEBUGi.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
12:00:49.545 [main] DEBUGi.n.c.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
12:00:49.550 [main] DEBUGi.n.util.internal.PlatformDependent - Platform: Windows
12:00:49.550 [main] DEBUGi.n.util.internal.PlatformDependent - Java version: 6
12:00:49.550 [main] DEBUGi.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
12:00:49.551 [main] DEBUGi.n.util.internal.PlatformDependent0 - java.nio.ByteBuffer.cleaner: available
12:00:49.552 [main] DEBUGi.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
12:00:49.552 [main] DEBUGi.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
12:00:49.552 [main] DEBUGi.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
12:00:49.552 [main] DEBUGi.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: true
12:00:49.552 [main] DEBUGi.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
12:00:49.552 [main] DEBUGi.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
12:00:49.553 [main] DEBUGi.n.util.internal.PlatformDependent - Javassist: unavailable
12:00:49.553 [main] DEBUGi.n.util.internal.PlatformDependent - You don't have Javassist in your classpath or you don't have enough permission to load dynamically generatedclasses. Please check the configurationfor better performance.
12:00:49.553 [main] DEBUGi.n.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
12:00:49.568 [main] DEBUGio.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
12:00:49.568 [main] DEBUGio.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold:512
12:00:49.622 [main] DEBUGi.n.util.internal.ThreadLocalRandom - -Dio.netty.initialSeedUniquifier:0x2e4f8e2e67b4cc9b
12:00:49.663[NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector --Dio.netty.noResourceLeakDetection: false
Consumer Started.
ConsumeMessageThread-ConsumerGroupName-4Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-2Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-1Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-6Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-5Receive New Messages: 1
ConsumeMessageThread-ConsumerGroupName-3Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-8Receive New Messages: 1
Hello MetaQA
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-9Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-10Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-7Receive New Messages: 1
Hello MetaQA
ConsumeMessageThread-ConsumerGroupName-11Receive New Messages: 1
Hello MetaQB
ConsumeMessageThread-ConsumerGroupName-12Receive New Messages: 1
Hello MetaQB
ConsumeMessageThread-ConsumerGroupName-13Receive New Messages: 1
Hello MetaQB
ConsumeMessageThread-ConsumerGroupName-15Receive New Messages: 1
Hello MetaQB
ConsumeMessageThread-ConsumerGroupName-14Receive New Messages: 1
Hello MetaQB
ConsumeMessageThread-ConsumerGroupName-16Receive New Messages: 1
ConsumeMessageThread-ConsumerGroupName-17Receive New Messages: 1
Hello MetaQB
Hello MetaQB
ConsumeMessageThread-ConsumerGroupName-18Receive New Messages: 1
ConsumeMessageThread-ConsumerGroupName-19Receive New Messages: 1
Hello MetaQB
Hello MetaQB
ConsumeMessageThread-ConsumerGroupName-20Receive New Messages: 1
Hello MetaQB
参考:
http://my.oschina.net/cloudcoder/blog/200741
https://github.com/alibaba/RocketMQ/wiki/Quick-Start
如果报com.alibaba.rocketmq.client.exception.MQClientException: No route info of this topic, TopicTest1
See https://github.com/alibaba/RocketMQ/issues/264 for further details.
at com.alibaba.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:570)
at com.alibaba.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:972)
at com.alibaba.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:109)
at com.ruishenh.rocketmq.example.Producer.main(Producer.java:43)
这是因为启动broker需要 mqbroker.exe -n localhost:9876,而不是双击 mqbroker.exe