RocketMQ 官方文档中说明的安装先决条件包括:
- 64bit OS, Linux/Unix/Mac is recommended
- 64bit JDK 1.8+
- Maven 3.2.x
- Git
虽然推荐的操作系统是 Linux/Unix/Mac,实际上 Windows 下也是可以安装部署的,本文演示 RocketMQ 在 Windows 10 和 Mac 中的安装部署及运行。
Windows 10
为简单起见,直接下载 binary release,本文使用 4.2.0 版本
1 解压到本地目录
2 设置系统环境变量 ROCKETMQ_HOME 指向 RocketMQ 的安装目录
此步非常重要,否则无法启动 NameServer
为了方便后续在命令提示符(cmd)窗口快速使用 RocketMQ 的命令,还需要将 %ROCKETMQ_HOME%\bin 添加到系统环境变量 Path 中。
3 启动 NameServer
在命令提示符(cmd)窗口中执行命令 mqnamesrv.cmd
启动日志如果有 The Name Server boot success
打印则表示 NameServer 启动成功,暂时忽略启动日志中的警告,注意一旦命令提示符(cmd)窗口关闭则 NameServer 停止运行。
Java HotSpot(TM) 64-Bit Server VM warning: Using the DefNew young collector with the CMS collector is deprecated and will likely be removed in a future release
Java HotSpot(TM) 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.
The Name Server boot success. serializeType=JSON
4 启动 Broker
重新开启一个命令提示符(cmd)窗口,执行命令 mqbroker.cmd -n localhost:9876
,注意只要没有报错日志应该就是启动成功了,如果启动成功则不会打印任何日志,不要关闭命令提示符(cmd)窗口。需要注意的是必须先启动 NameServer 再启动 Broker,Broker 要在 NameServer 上注册。
5 验证 RocketMQ 是否正常运行
有两种验证方法:
(1) 运行官方提供的快速启动示例
首先,重新开启一个命令提示符(cmd)窗口,执行命令 set NAMESRV_ADDR=localhost:9876
设置环境变量,也可以像第 2 步一样将 NAMESRV_ADDR 添加到系统环境变量中;
其次,执行命令 tools.cmd org.apache.rocketmq.example.quickstart.Producer
,运行官方示例中的消息生产者,可以看到消息全部成功发送,以下截取了部分消息发布日志
22:26:06.213 [main] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
22:26:06.224 [main] DEBUG i.n.c.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 16
22:26:06.238 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
22:26:06.238 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
22:26:06.239 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
22:26:06.240 [main] DEBUG i.n.util.internal.PlatformDependent0 - direct buffer constructor: available
22:26:06.240 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
22:26:06.240 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
22:26:06.241 [main] DEBUG io.netty.util.internal.Cleaner0 - java.nio.ByteBuffer.cleaner(): available
22:26:06.242 [main] DEBUG i.n.util.internal.PlatformDependent - Platform: Windows
22:26:06.242 [main] DEBUG i.n.util.internal.PlatformDependent - Java version: 8
22:26:06.242 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
22:26:06.242 [main] DEBUG i.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
22:26:06.243 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
22:26:06.327 [main] DEBUG i.n.util.internal.PlatformDependent - Javassist: available
22:26:06.328 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\Ji\AppData\Local\Temp (java.io.tmpdir)
22:26:06.328 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
22:26:06.329 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
22:26:06.329 [main] DEBUG i.n.util.internal.PlatformDependent - io.netty.maxDirectMemory: 1040187392 bytes
22:26:06.342 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
22:26:06.342 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
22:26:06.347 [main] DEBUG i.n.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
22:26:06.600 [main] DEBUG i.n.util.internal.ThreadLocalRandom - -Dio.netty.initialSeedUniquifier: 0x2f0e8a555da48856 (took 0 ms)
22:26:06.621 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: unpooled
22:26:06.622 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 65536
22:26:06.623 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
22:26:06.639 [NettyClientSelector_1] DEBUG i.n.u.i.JavassistTypeParameterMatcherGenerator - Generated: io.netty.util.internal.__matchers__.org.apache.rocketmq.remoting.protocol.RemotingCommandMatcher
22:26:06.652 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacity.default: 32768
22:26:06.652 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
22:26:06.653 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
22:26:06.656 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
22:26:06.667 [NettyClientWorkerThread_1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
22:26:06.673 [NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
22:26:06.673 [NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.maxRecords: 4
22:26:06.675 [NettyClientWorkerThread_1] DEBUG i.n.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@395ad0f
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20380000, offsetMsgId=0A00000700002A9F0000000000000000, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=3], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20880001, offsetMsgId=0A00000700002A9F00000000000000B2, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=0], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D208F0002, offsetMsgId=0A00000700002A9F0000000000000164, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=1], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20990003, offsetMsgId=0A00000700002A9F0000000000000216, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=2], queueOffset=0]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D209F0004, offsetMsgId=0A00000700002A9F00000000000002C8, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=3], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20A50005, offsetMsgId=0A00000700002A9F000000000000037A, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=0], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20AB0006, offsetMsgId=0A00000700002A9F000000000000042C, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=1], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20B20007, offsetMsgId=0A00000700002A9F00000000000004DE, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=2], queueOffset=1]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20B60008, offsetMsgId=0A00000700002A9F0000000000000590, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=3], queueOffset=2]
SendResult [sendStatus=SEND_OK, msgId=0A000007093C5CAD80860F1D20BD0009, offsetMsgId=0A00000700002A9F0000000000000642, messageQueue=MessageQueue [topic=TopicTest, brokerName=LAPTOP-C375ASPB, queueId=0], queueOffset=2]
......
最后,执行命令 tools.cmd org.apache.rocketmq.example.quickstart.Consumer
,运行官方示例中的消息消费者,可以看到消息全部成功消费,以下截取了部分消息消费日志
22:28:56.473 [main] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
22:28:56.476 [main] DEBUG i.n.c.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 16
22:28:56.490 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
22:28:56.490 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
22:28:56.491 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
22:28:56.491 [main] DEBUG i.n.util.internal.PlatformDependent0 - direct buffer constructor: available
22:28:56.491 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
22:28:56.492 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
22:28:56.493 [main] DEBUG io.netty.util.internal.Cleaner0 - java.nio.ByteBuffer.cleaner(): available
22:28:56.493 [main] DEBUG i.n.util.internal.PlatformDependent - Platform: Windows
22:28:56.493 [main] DEBUG i.n.util.internal.PlatformDependent - Java version: 8
22:28:56.493 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
22:28:56.494 [main] DEBUG i.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
22:28:56.494 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
22:28:56.580 [main] DEBUG i.n.util.internal.PlatformDependent - Javassist: available
22:28:56.581 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\Ji\AppData\Local\Temp (java.io.tmpdir)
22:28:56.582 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
22:28:56.582 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
22:28:56.582 [main] DEBUG i.n.util.internal.PlatformDependent - io.netty.maxDirectMemory: 1040187392 bytes
22:28:56.595 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
22:28:56.595 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
22:28:56.600 [main] DEBUG i.n.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
22:28:57.080 [main] DEBUG i.n.util.internal.ThreadLocalRandom - -Dio.netty.initialSeedUniquifier: 0x1e52632a543f7abb (took 0 ms)
22:28:57.104 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: unpooled
22:28:57.105 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 65536
22:28:57.106 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
22:28:57.122 [NettyClientSelector_1] DEBUG i.n.u.i.JavassistTypeParameterMatcherGenerator - Generated: io.netty.util.internal.__matchers__.org.apache.rocketmq.remoting.protocol.RemotingCommandMatcher
22:28:57.135 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacity.default: 32768
22:28:57.136 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
22:28:57.136 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
22:28:57.137 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
22:28:57.146 [NettyClientWorkerThread_1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
22:28:57.149 [NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
22:28:57.149 [NettyClientWorkerThread_1] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.maxRecords: 4
22:28:57.150 [NettyClientWorkerThread_1] DEBUG i.n.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@4aed4bcc
Consumer Started.
ConsumeMessageThread_7 Receive New Messages: [MessageExt [queueId=2, storeSize=178, queueOffset=1, sysFlag=0, bornTimestamp=1530627967154, bornHost=/10.0.0.7:55776, storeTimestamp=1530627967156, storeHost=/10.0.0.7:10911, msgId=0A00000700002A9F00000000000004DE, commitLogOffset=1246, bodyCRC=988340972, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=250, CONSUME_START_TIME=1530628137407, UNIQ_KEY=0A000007093C5CAD80860F1D20B20007, WAIT=true, TAGS=TagA}, body=16]]]
ConsumeMessageThread_15 Receive New Messages: [MessageExt [queueId=3, storeSize=178, queueOffset=2, sysFlag=0, bornTimestamp=1530627967158, bornHost=/10.0.0.7:55776, storeTimestamp=1530627967161, storeHost=/10.0.0.7:10911, msgId=0A00000700002A9F0000000000000590, commitLogOffset=1424, bodyCRC=710410109, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=250, CONSUME_START_TIME=1530628137410, UNIQ_KEY=0A000007093C5CAD80860F1D20B60008, WAIT=true, TAGS=TagA}, body=16]]]
ConsumeMessageThread_9 Receive New Messages: [MessageExt [queueId=1, storeSize=179, queueOffset=2, sysFlag=0, bornTimestamp=1530627967171, bornHost=/10.0.0.7:55776, storeTimestamp=1530627967173, storeHost=/10.0.0.7:10911, msgId=0A00000700002A9F00000000000006F4, commitLogOffset=1780, bodyCRC=193412630, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=250, CONSUME_START_TIME=1530628137415, UNIQ_KEY=0A000007093C5CAD80860F1D20C3000A, WAIT=true, TAGS=TagA}, body=17]]]
ConsumeMessageThread_16 Receive New Messages: [MessageExt [queueId=1, storeSize=179, queueOffset=3, sysFlag=0, bornTimestamp=1530627967196, bornHost=/10.0.0.7:55776, storeTimestamp=1530627967198, storeHost=/10.0.0.7:10911, msgId=0A00000700002A9F00000000000009C0, commitLogOffset=2496, bodyCRC=216726031, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message [topic=TopicTest, flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=250, CONSUME_START_TIME=1530628137411, UNIQ_KEY=0A000007093C5CAD80860F1D20DC000E, WAIT=true, TAGS=TagA}, body=17]]]
......
(2) 关闭 Broker 和 NameServer
首先,执行命令 mqshutdown.cmd broker
关闭 Broker,如果有 Broker 运行则会打印关闭的 Broker 所在线程,如:
killing broker
成功: 已终止 PID 为 3960 的进程。
Done!
其次,执行命令 `` 关闭 NameServer,如果有 NameServer 运行则会打印关闭的 NameServer 所在线程,如:
killing name server
成功: 已终止 PID 为 19504 的进程。
Done!