• Centos7 部署 RocketMQ 高可用集群


    1.简要说明

    1.1概述

    RocketMQ主要有四大组成部分:NameServer、Broker、Producer、Consumer

    Producer

    消息发布的角色,支持分布式集群方式部署。Producer通过MQ的负载均衡模块选择相应的Broker集群队列进行消息投递,投递的过程支持快速失败并且低延迟

    Consumer

    消息消费的角色,支持分布式集群方式部署。支持以push推,pull拉两种模式对消息进行消费。同时也支持集群方式和广播方式的消费,它提供实时消息订阅机制,可以满足大多数用户的需求

    NameServer

    NameServer是一个非常简单的Topic路由注册中心,其角色类似Dubbo中的zookeeper,支持Broker的动态注册与发现。
    主要包括两个功能:Broker管理,NameServer接受Broker集群的注册信息并且保存下来作为路由信息的基本数据。然后提供心跳检测机制,检查Broker是否还存活;路由信息管理,每个NameServer将保存关于Broker集群的整个路由信息和用于客户端查询的队列信息。然后Producer和Conumser通过NameServer就可以知道整个Broker集群的路由信息,从而进行消息的投递和消费。
    NameServer通常也是集群的方式部署,各实例间相互不进行信息通讯。Broker是向每一台NameServer注册自己的路由信息,所以每一个NameServer实例上面都保存一份完整的路由信息。当某个NameServer因某种原因下线了,Broker仍然可以向其它NameServer同步其路由信息,Producer,Consumer仍然可以动态感知Broker的路由的信息。

    Broker

    Broker主要负责消息的存储、投递和查询以及服务高可用保证,为了实现这些功能,Broker包含了以下几个重要子模块
    #Remoting Module:整个Broker的实体,负责处理来自clients端的请求。
    #Client Manager:负责管理客户端(Producer/Consumer)和维护Consumer的Topic订阅信息
    #Store Service:提供方便简单的API接口处理消息存储到物理硬盘和查询功能。
    #HA Service:高可用服务,提供Master Broker 和 Slave Broker之间的数据同步功能。
    #Index Service:根据特定的Message key对投递到Broker的消息进行索引服务,以提供消息的快速查询。

    1.2rocketmq的部署方式

    双主模式:多Master模式,无Slave
    双主双从+同步模式:多Master多Slave模式,同步双写
    双主双从+异步模式:多Master多Slave模式,异步复制

    1.3rocketmq发送消息方式

    同步发送: 指消息发送方发出数据后会在收到接收方发回响应之后才发下一个数据包。
    异步发送: 指发送方发出数据后,不等接收方发回响应,接着发送下个数据包, 异步方式也需要Broker返回确认信息。
    单向发送: 指只负责发送消息而不等待服务器回应且没有回调函数触发。

    #RocketMQ 三种消息发送模式的使用场景:
    同步发送:主要运用在比较重要一点消息传递/通知等业务:
    异步发送:通常用于对发送消息响应时间要求更高/更快的场景:
    单向发送:适用于某些耗时非常短,但对可靠性要求并不高的场景,例如日志收集。只发送消息,不等待服务器响应,只发送请求不等待应答。此方式发送消息的过程耗时非常短,一般在微秒级别。

    1.4rocketmq端口

    rocketmq 默认端口:9876(即nameserver端口)
    非vip通道端口:10911
    vip通道端口:10909
    #10909是VIP通道对应的端口,在JAVA中的消费者对象或者是生产者对象中关闭VIP通道即可无需开放10909端口

    1.5部署模式

    #本实践部署的是RocketMQ的"双主双从+同步模式"
    broker-a            主1
    broker-b            主2
    broker-a-s          从1
    broker-b-s          从2
    NameServer1         注册中心1
    NameServer2         注册中心2
    dashboard           可视化web界面

    2.环境准备

    1)10.0.61.12操作

    1. #1.创建目录
    2. mkdir -p /opt/rocketmq
    3. mkdir -p /opt/rocketmq/namesrv1
    4. cd /opt/rocketmq/namesrv1
    5. #2.下载rocketmq
    6. wget https://archive.apache.org/dist/rocketmq/4.9.3/rocketmq-all-4.9.3-bin-release.zip
    7. #3.解压
    8. unzip rocketmq-all-4.9.3-bin-release.zip

    2)10.0.61.21操作

    1. #1.创建目录
    2. mkdir -p /opt/rocketmq
    3. mkdir -p /opt/rocketmq/{master1,namesrv2,slave2,dashboard}
    4. cd /opt/rocketmq/namesrv2
    5. #2.下载rocketmq
    6. wget https://archive.apache.org/dist/rocketmq/4.9.3/rocketmq-all-4.9.3-bin-release.zip
    7. #3.解压
    8. unzip rocketmq-all-4.9.3-bin-release.zip
    9. #4.复制目录
    10. cp -r rocketmq-4.9.3 /opt/rocketmq/master1/
    11. cp -r rocketmq-4.9.3 /opt/rocketmq/slave2/

    3)10.0.61.22操作

    1. #1.创建目录
    2. mkdir -p /opt/rocketmq
    3. mkdir -p /opt/rocketmq/{master2,slave1}
    4. cd /opt/rocketmq/master2
    5. #2.下载rocketmq
    6. wget https://archive.apache.org/dist/rocketmq/4.9.3/rocketmq-all-4.9.3-bin-release.zip
    7. #3.解压
    8. unzip rocketmq-all-4.9.3-bin-release.zip
    9. #4.复制目录
    10. cp -r rocketmq-4.9.3 /opt/rocketmq/slave1/

    3.部署NameServer

    3.1部署namesrv1

            10.0.61.12上操作

    1)更改日志输出目录

    1. vim /opt/rocketmq/namesrv1/rocketmq-4.9.3/conf/logback_namesrv.xml
    2. #将配置文件中所有的${user.home}/logs更改为 /opt/rocketmq/namesrv1/logs

    2)更改启动脚本jvm内存大小

    vim /opt/rocketmq/namesrv1/rocketmq-4.9.3/bin/runserver.sh

    3)后台启动namesrv1

    nohup sh /opt/rocketmq/namesrv1/rocketmq-4.9.3/bin/mqnamesrv -n 10.0.61.12:9876 autoCreateTopicEnable=true > /dev/null 2>&1 &

    3.2部署namesrv2

            10.0.61.21上操作

    1)更改日志输出目录

    1. vim /opt/rocketmq/namesrv2/rocketmq-4.9.3/conf/logback_namesrv.xml
    2. #将配置文件中所有的${user.home}/logs更改为 /opt/rocketmq/namesrv2/logs

    2)更改启动脚本修改jvm内存大小

    vim /opt/rocketmq/namesrv2/rocketmq-4.9.3/bin/runserver.sh

    3)后台启动namesrv2

    nohup sh /opt/rocketmq/namesrv2/rocketmq-4.9.3/bin/mqnamesrv -n 10.0.61.21:9876 autoCreateTopicEnable=true > /dev/null 2>&1 &

    4.部署Broker

    4.1部署master1

            10.0.61.21上操作

    1)更改启动脚本jvm内存大小

    1. #runbroker.sh
    2. vim /opt/rocketmq/master1/rocketmq-4.9.3/bin/runbroker.sh

    1. #tools.sh
    2. vim /opt/rocketmq/master1/rocketmq-4.9.3/bin/tools.sh

    2)修改配置文件

            将原来的配置内容全部注释掉(之后的properties配置文件都得这样)

    1. #broker-a.properties
    2. vim /opt/rocketmq/master1/rocketmq-4.9.3/conf/2m-2s-sync/broker-a.properties
    3. brokerClusterName=rocketmq-cluster
    4. brokerName=broker-a
    5. brokerId=0
    6. namesrvAddr=10.0.61.12:9876;10.0.61.21:9876
    7. brokerIP1=10.0.61.21
    8. brokerIP2=10.0.61.21
    9. defaultTopicQueueNums=4
    10. autoCreateTopicEnable=true
    11. autoCreateSubscriptionGroup=true
    12. listenPort=10911
    13. deleteWhen=04
    14. fileReservedTime=48
    15. mapedFileSizeCommitLog=1073741824
    16. mapedFileSizeConsumeQueue=300000
    17. diskMaxUsedSpaceRatio=88
    18. storePathRootDir=/opt/rocketmq/master1/data/store
    19. storePathCommitLog=/opt/rocketmq/master1/data/store/commitlog
    20. storePathConsumeQueue=/opt/rocketmq/master1/data/store/consumequeue
    21. storePathIndex=/opt/rocketmq/master1/data/store/index
    22. storeCheckpoint=/opt/rocketmq/master1/data/store/checkpoint
    23. abortFile=/opt/rocketmq/master1/data/store/abort
    24. maxMessageSize=65536
    25. brokerRole=SYNC_MASTER
    26. flushDiskType=SYNC_FLUSH

            配置文件解释:

    1. #所属集群名字
    2. brokerClusterName=rocketmq-cluster
    3. #broker名字,注意此处不同的配置文件填写的不一样; 在broker-b.properties中此处需要修改为:brokerName=broker-b
    4. brokerName=broker-a
    5. #0: Master; >0: Slave
    6. brokerId=0
    7. #nameServer地址,分号分割
    8. namesrvAddr=10.0.61.12:9876;10.0.61.21:9876
    9. #broker启动地址,rocketmq默认内网启动
    10. brokerIP1=10.0.61.21
    11. #broker的HAIP地址(供Slave同步消息的地址)
    12. brokerIP2=10.0.61.21
    13. #在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
    14. defaultTopicQueueNums=4
    15. #是否允许Broker自动创建Topic,建议线下开启,线上关闭
    16. autoCreateTopicEnable=true
    17. #是否允许Broker自动创建订阅组,建议线下开启,线上关闭
    18. autoCreateSubscriptionGroup=true
    19. #Broker对外服务的监听端口
    20. listenPort=10911
    21. #删除文件时间点,默认凌晨4
    22. deleteWhen=04
    23. #文件保留时间,默认48h
    24. fileReservedTime=48
    25. #commitLog每个文件的大小默认1G
    26. mapedFileSizeCommitLog=1073741824
    27. #ConsumeQueue每个文件默认存30W条,根据业务情况调整
    28. mapedFileSizeConsumeQueue=300000
    29. #检测物理文件磁盘空间
    30. diskMaxUsedSpaceRatio=88
    31. #存储路径
    32. storePathRootDir=/opt/rocketmq/master1/data/store
    33. #commitLog 存储路径
    34. storePathCommitLog=/opt/rocketmq/master1/data/store/commitlog
    35. #消费队列存储路径存储路径
    36. storePathConsumeQueue=/opt/rocketmq/master1/data/store/consumequeue
    37. #消息索引存储路径
    38. storePathIndex=/opt/rocketmq/master1/data/store/index
    39. #checkpoint 文件存储路径
    40. storeCheckpoint=/opt/rocketmq/master1/data/store/checkpoint
    41. #abort 文件存储路径
    42. abortFile=/opt/rocketmq/master1/data/store/abort
    43. #限制的消息大小
    44. maxMessageSize=65536
    45. #Broker角色: ASYNC_MASTER(异步复制Master)、SYNC_MASTER(同步双写Master)、SLAVE(从节点)
    46. brokerRole=SYNC_MASTER
    47. #刷盘方式: ASYNC_FLUSH(异步刷盘)、SYNC_FLUSH(同步刷盘)
    48. flushDiskType=SYNC_FLUSH

    3)后台启动master1

    nohup sh /opt/rocketmq/master1/rocketmq-4.9.3/bin/mqbroker -c /opt/rocketmq/master1/rocketmq-4.9.3/conf/2m-2s-sync/broker-a.properties > /opt/rocketmq/master1/logs/master-broker1.log 2>&1 &

    4)查看日志

    tail -f ~/logs/rocketmqlogs/broker.log

    4.2部署master2

            10.0.61.22上操作

    1)更改启动脚本jvm内存大小

    1. #runbroker.sh
    2. vim /opt/rocketmq/master2/rocketmq-4.9.3/bin/runbroker.sh

    1. #tools.sh
    2. vim /opt/rocketmq/master2/rocketmq-4.9.3/bin/tools.sh

    2)修改配置文件

    1. #broker-b.properties
    2. vim /opt/rocketmq/master2/rocketmq-4.9.3/conf/2m-2s-sync/broker-b.properties
    3. brokerClusterName=rocketmq-cluster
    4. brokerName=broker-b
    5. brokerId=0
    6. namesrvAddr=10.0.61.12:9876;10.0.61.21:9876
    7. brokerIP1=10.0.61.22
    8. brokerIP2=10.0.61.22
    9. defaultTopicQueueNums=4
    10. autoCreateTopicEnable=true
    11. autoCreateSubscriptionGroup=true
    12. listenPort=10911
    13. deleteWhen=04
    14. fileReservedTime=48
    15. mapedFileSizeCommitLog=1073741824
    16. mapedFileSizeConsumeQueue=300000
    17. diskMaxUsedSpaceRatio=88
    18. storePathRootDir=/opt/rocketmq/master2/data/store
    19. storePathCommitLog=/opt/rocketmq/master2/data/store/commitlog
    20. storePathConsumeQueue=/opt/rocketmq/master2/data/store/consumequeue
    21. storePathIndex=/opt/rocketmq/master2/data/store/index
    22. storeCheckpoint=/opt/rocketmq/master2/data/store/checkpoint
    23. abortFile=/opt/rocketmq/master2/data/store/abort
    24. maxMessageSize=65536
    25. brokerRole=SYNC_MASTER
    26. flushDiskType=SYNC_FLUSH

    3)后台启动master2

    nohup sh /opt/rocketmq/master2/rocketmq-4.9.3/bin/mqbroker -c /opt/rocketmq/master2/rocketmq-4.9.3/conf/2m-2s-sync/broker-b.properties > /opt/rocketmq/master2/logs/master-broker2.log 2>&1 &

    4)查看日志

    tail -f ~/logs/rocketmqlogs/broker.log

    4.3部署slave1

            10.0.61.22上操作

    1)更改启动脚本jvm内存大小

    1. #runbroker.sh
    2. vim /opt/rocketmq/slave1/rocketmq-4.9.3/bin/runbroker.sh

    1. #tools.sh
    2. vim /opt/rocketmq/slave1/rocketmq-4.9.3/bin/tools.sh

    2)修改配置文件

    1. #broker-a-s.properties
    2. vim /opt/rocketmq/slave1/rocketmq-4.9.3/conf/2m-2s-sync/broker-a-s.properties
    3. brokerClusterName=rocketmq-cluster
    4. brokerName=broker-a
    5. brokerId=70
    6. namesrvAddr=10.0.61.12:9876;10.0.61.21:9876
    7. brokerIP1=10.0.61.22
    8. brokerIP2=10.0.61.22
    9. defaultTopicQueueNums=4
    10. autoCreateTopicEnable=true
    11. autoCreateSubscriptionGroup=true
    12. listenPort=11011
    13. deleteWhen=04
    14. fileReservedTime=48
    15. mapedFileSizeCommitLog=1073741824
    16. mapedFileSizeConsumeQueue=300000
    17. diskMaxUsedSpaceRatio=88
    18. storePathRootDir=/opt/rocketmq/slave1/data/store
    19. storePathCommitLog=/opt/rocketmq/slave1/data/store/commitlog
    20. storePathConsumeQueue=/opt/rocketmq/slave1/data/store/consumequeue
    21. storePathIndex=/opt/rocketmq/slave1/data/store/index
    22. storeCheckpoint=/opt/rocketmq/slave1/data/store/checkpoint
    23. abortFile=/opt/rocketmq/slave1/data/store/abort
    24. maxMessageSize=65536
    25. brokerRole=SLAVE
    26. flushDiskType=SYNC_FLUSH

    3)后台启动slave1

    nohup sh /opt/rocketmq/slave1/rocketmq-4.9.3/bin/mqbroker -c /opt/rocketmq/slave1/rocketmq-4.9.3/conf/2m-2s-sync/broker-a-s.properties > /opt/rocketmq/slave1/logs/slave1-broker-a-s.log 2>&1 &

    4)查看日志

    tail -f ~/logs/rocketmqlogs/broker.log

    4.4部署slave2

            10.0.61.21上操作

    1)更改启动脚本jvm内存大小

    1. #runbroker.sh
    2. vim /opt/rocketmq/slave2/rocketmq-4.9.3/bin/runbroker.sh

    1. #tools.sh
    2. vim /opt/rocketmq/slave2/rocketmq-4.9.3/bin/tools.sh

    2)修改配置文件

    1. #broker-a-s.properties
    2. vim /opt/rocketmq/slave2/rocketmq-4.9.3/conf/2m-2s-sync/broker-b-s.properties
    3. brokerClusterName=rocketmq-cluster
    4. brokerName=broker-b
    5. brokerId=70
    6. namesrvAddr=10.0.61.12:9876;10.0.61.21:9876
    7. brokerIP1=10.0.61.21
    8. brokerIP2=10.0.61.21
    9. defaultTopicQueueNums=4
    10. autoCreateTopicEnable=true
    11. autoCreateSubscriptionGroup=true
    12. listenPort=11011
    13. deleteWhen=04
    14. fileReservedTime=48
    15. mapedFileSizeCommitLog=1073741824
    16. mapedFileSizeConsumeQueue=300000
    17. diskMaxUsedSpaceRatio=88
    18. storePathRootDir=/opt/rocketmq/slave2/data/store
    19. storePathCommitLog=/opt/rocketmq/slave2/data/store/commitlog
    20. storePathConsumeQueue=/opt/rocketmq/slave2/data/store/consumequeue
    21. storePathIndex=/opt/rocketmq/slave2/data/store/index
    22. storeCheckpoint=/opt/rocketmq/slave2/data/store/checkpoint
    23. abortFile=/opt/rocketmq/slave2/data/store/abort
    24. maxMessageSize=65536
    25. brokerRole=SLAVE
    26. flushDiskType=SYNC_FLUSH

    3)后台启动slave1

    nohup sh /opt/rocketmq/slave2/rocketmq-4.9.3/bin/mqbroker -c /opt/rocketmq/slave2/rocketmq-4.9.3/conf/2m-2s-sync/broker-b-s.properties > /opt/rocketmq/slave2/logs/slave2-broker-b-s.log 2>&1 &

    4)查看日志

    tail -f ~/logs/rocketmqlogs/broker.log

    5.部署dashboard

            10.0.61.21上操作

    5.1下载插件

            地址:GitHub - rocketmq/rocketmq-externals: Mirror of Apache RocketMQ (Incubating)

    1. cd /opt/rocketmq/dashboard
    2. git clone git@gitcode.net:mirrors/rocketmq/rocketmq-externals.git
    3. #下载不下来可以下载tar包传到服务器再解压

    5.2修改配置文件

    1. #application.properties
    2. vim /opt/rocketmq/dashboard/rocketmq-externals-master/rocketmq-console/src/main/resources/application.properties
    3. server.contextPath=
    4. server.port=30916
    5. #spring.application.index=true
    6. spring.application.name=rocketmq-console
    7. spring.http.encoding.charset=UTF-8
    8. spring.http.encoding.enabled=true
    9. spring.http.encoding.force=true
    10. logging.config=classpath:logback.xml
    11. #if this value is empty,use env value rocketmq.config.namesrvAddr NAMESRV_ADDR | now, you can set it in ops page.default localhost:9876
    12. rocketmq.config.namesrvAddr=10.0.61.12:9876;10.0.61.21:9876
    13. #if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
    14. rocketmq.config.isVIPChannel=
    15. #rocketmq-console's data path:dashboard/monitor
    16. rocketmq.config.dataPath=/tmp/rocketmq-console/data
    17. #set it false if you don't want use dashboard.default true
    18. rocketmq.config.enableDashBoardCollect=true

    5.3编译

    1. cd /opt/rocketmq/dashboard/rocketmq-externals-master/rocketmq-console
    2. mvn clean package -Dmaven.test.skip=true

    5.4后台启动

    nohup /opt/jdk/jdk1.8.0_333/bin/java -jar /opt/rocketmq/dashboard/rocketmq-externals-master/rocketmq-console/target/rocketmq-console-ng-1.0.0.jar >/opt/rocketmq/dashboard/console.log 2>&1 &

    5.5web端访问

            地址:http://10.0.61.21:30916

    6.测试收发消息

    6.1修改脚本

    1. vim /opt/rocketmq/master1/rocketmq-4.9.3/bin/tools.sh
    2. # 在export JAVA_HOME上面添加如下这段代码
    3. export NAMESRV_ADDR=10.0.61.12:9876

    6.2发送消息

    1. cd /opt/rocketmq/master1/rocketmq-4.9.3/bin
    2. ./tools.sh org.apache.rocketmq.example.quickstart.Producer

    6.3消费消息

    ./tools.sh org.apache.rocketmq.example.quickstart.Consumer

    6.4控制台查看

            发送成功后我们可以到控制台查看消息和消费情况等信息

  • 相关阅读:
    Hadoop集群资源管理器-YARN
    Spring源码解析——IOC之循环依赖处理
    docker打包发版
    #Docker 提示空间不足的解决方法#
    卫龙辣条第三次冲刺上市:业绩增速下滑,刘卫平、刘福平提前套现
    centos7安装nginx
    Sora - 探索AI视频模型的无限可能
    基于FPGA的去雾算法
    敏捷管理工具和scrum敏捷管理方法介绍
    [机缘参悟-74]:沟通技巧-无论在职场还是在家,尽量少用反问句
  • 原文地址:https://blog.csdn.net/qq_19734597/article/details/128153968