RocketMQ简介

RocketMQ是阿里巴巴2016年MQ中间件,使用Java语言开发,在阿里内部,RocketMQ承接了例如“双11”等高并发场景的消息流转,能够处理万亿级别的消息。

RocketMQ 安装

准备工作

下载RocketMQ安装包

rocketmq-all-4.7.1-bin-release.zip

安装RocketMQ

  • 解压压缩包
  • 切换至rocketmq的位置

    RocketMQ 目录简介

  • bin:启动脚本

  • conf:配置文件
  • lib:依赖jar包

    单机安装

    修改rocketmq的配置文件broker.conf
    1. brokerClusterName = DefaultCluster
    2. brokerName = broker-a
    3. brokerId = 0
    4. namesrvAddr = 192.168.137.130:9876
    5. deleteWhen = 04
    6. fileReservedTime = 48
    7. brokerIP1 = ${localhost} # 填自己的虚拟机ip
    8. brokerRole = ASYNC_MASTER
    9. flushDiskType = ASYNC_FLUSH
    10. authCreateTopicEnable = true #开启自动创建topic的功能
    11. storePathRootDir=/opt/module/rocketmq-all-4.7.0-bin-release/store # 填自己的rocketmq的位置
    12. storePathCommitLog=/opt/module/rocketmq-all-4.7.0-bin-release/store/commitlog # 填自己的rocketmq的位置
    修改NameServer以及Broker的启动内存
    修改NameServer的配置
    1. vim bin/runserver.sh
    2. # 将67行左右的配置改成如下
    3. JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
    修改Broker的配置
    1. vim bin/runbroker.sh
    2. # 将67行左右的配置改成如下
    3. JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn256m"

    单机启动测试

    1. cd /yourrocketmq/
    2. # 启动namesrv
    3. nohup bin/mqnamesrv -n 192.168.137.130:9876 >nameserver.log 2>&1 &
    4. # 启动broker
    5. nohup bin/mqbroker -n 192.168.137.130:9876 -c conf/broker.conf >broker.log 2>&1 &
    6. # 查看启动进程,看到如下的两个进程启动之后,则rocketmq运行成功
    7. jps
    8. 9760 Jps
    9. 8981 NamesrvStartup
    10. 8985 BrokerStartup

rocketmq集群安装

准备工作

在多台机器上,拷贝配置文件,分别为master配置文件及slave配置文件
第一台机器
broker-m.conf

  1. # 修改broker-m为如下内容
  2. namesrvAddr = 192.168.137.130:9876;192.168.137.131:9876;192.168.137.132:9876
  3. brokerName = broker-a
  4. brokerId = 0
  5. deleteWhen = 04
  6. fileReservedTime = 48
  7. brokerRole = SYNC_MASTER
  8. flushDiskType = ASYNC_FLUSH
  9. autoCreateTopicEnable = true
  10. listenPort = 10911
  11. storePathRootDir = /opt/module/rocketmq-all-4.7.0-bin-release/store-m

broker-s.conf

  1. namesrvAddr = 192.168.137.130:9876;192.168.137.131:9876;192.168.137.132:9876
  2. brokerName = broker-a
  3. brokerId = 1
  4. deleteWhen = 04
  5. fileReservedTime = 48
  6. brokerRole = SLAVE
  7. flushDiskType = ASYNC_FLUSH
  8. autoCreateTopicEnable = true
  9. listenPort = 10811
  10. storePathRootDir = /opt/module/rocketmq-all-4.7.0-bin-release/store-s

对于master的配置文件和slave的配置文件,仅修改 storePathRootDir和 brokerRole 以及监听端口这三个配置,其他配置在同一台机器上无变化
第二台机器
broker-m.conf

  1. namesrvAddr = 192.168.137.130:9876;192.168.137.131:9876;192.168.137.132:9876
  2. brokerName = broker-b
  3. brokerId = 0
  4. deleteWhen = 04
  5. fileReservedTime = 48
  6. brokerRole = SYNC_MASTER
  7. flushDiskType = ASYNC_FLUSH
  8. autoCreateTopicEnable = true
  9. listenPort = 10911
  10. storePathRootDir = /opt/module/rocketmq-all-4.7.0-bin-release/store-m

broker-s.conf

  1. namesrvAddr = 192.168.137.130:9876;192.168.137.131:9876;192.168.137.132:9876
  2. brokerName = broker-b
  3. brokerId = 1
  4. deleteWhen = 04
  5. fileReservedTime = 48
  6. brokerRole = SLAVE
  7. flushDiskType = ASYNC_FLUSH
  8. autoCreateTopicEnable = true
  9. listenPort = 10811
  10. storePathRootDir = /opt/module/rocketmq-all-4.7.0-bin-release/store-s

对于不同机器的master 和 slave配置文件,仅有 brokerName不同,代表着不同的broker,其他配置均相同。hadoop132的配置与上述相同,不再列出

启动测试(在多台机器上全部执行如下的命令)

  1. # 启动namesrv
  2. nohup bin/mqnamesrv >nameserver.log 2>&1 &
  3. # 启动 broker-master
  4. nohup bin/mqbroker -c conf/broker-m.conf >broker-m.log 2>&1 &
  5. # 启动 broker-slave
  6. nohup bin/mqbroker -c conf/broker-s.conf >broker-s.log 2>&1 &

查看集群状态,此时需要借助rocketmq-console控制台管理工具进行查看

下载rocketmq-console 工具
rocketmq-console

  • 下载完成之后,使用idea打开项目,修改其中的配置文件(applicaion.properties)

主要修改 rocketmq.config.namesrvAddr 和 rocketmq.config.dataPath 两个配置
最终的修改结果如下,注意修改 rocketmq.config.dataPath,rocketmq.config.namesrvAddr 为自己的路径即可

  1. server.contextPath=
  2. server.port=8080
  3. #spring.application.index=true
  4. spring.application.name=rocketmq-console
  5. spring.http.encoding.charset=UTF-8
  6. spring.http.encoding.enabled=true
  7. spring.http.encoding.force=true
  8. logging.config=classpath:logback.xml
  9. #if this value is empty,use env value rocketmq.config.namesrvAddr NAMESRV_ADDR | now, you can set it in ops page.default localhost:9876
  10. rocketmq.config.namesrvAddr=192.168.137.130:9876;192.168.137.131:9876;192.168.137.132:9876
  11. #if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
  12. rocketmq.config.isVIPChannel=
  13. #rocketmq-console's data path:dashboard/monitor
  14. rocketmq.config.dataPath=F:/源码构建/rocketmq-console/data
  15. #set it false if you don't want use dashboard.default true
  16. rocketmq.config.enableDashBoardCollect=true
  • 修改完配置文件之后,修改maven依赖,将rocketmq的版本改成自己下的版本

    1. <properties>
    2. <rocketmq.version>4.7.0</rocketmq.version>
    3. </properties>
  • 运行rocketmq-console,在idea中运行SpringBoot的主启动类

访问 http://localhost:8080/#/cluster 看到如下的 三个集群节点启动成功,则集群搭建成功.且控制台也搭建成功.
clipboard.png