RocketMQ4.3.0集群搭建和部署rocketMq监控平台

集群部署(采用2个master,2个slave异步复制的集群结构)
 

RocketMQ具有以下特点:

1)是一个队列模型的消息中间件,具有高性能、高可靠、高实时、分布式特点。

2)Producer、Consumer、队列都可以分布式。

3)Producer向一些队列轮流发送消息,队列集合称为Topic,Consumer如果做广播消费,则一个consumer实例消费这个Topic对应的所有队列,如果做集群消费,则多个Consumer实例平均消费这个topic对应的队列集合。

4)支持严格的消息顺序;

5)提供丰富的消息拉取模式

6)高效的订阅者水平扩展能力

7)实时的消息订阅机制

8)亿级消息堆积能力

9)较少的依赖

10)支持Topic与Queue两种模式;

11)同时支持Push与Pull方式消费消息;

消息队列的应用场景

1)异步处理 将不是必须的业务逻辑,进行异步处理,比如注册之后短信、邮箱的发送

2)应用解耦 订单系统:用户下单后,订单系统完成持久化处理,将消息写入消息队列,返回用户订单下单成功。 库存系统:订阅下单的消息,采用拉/推的方式,获取下单信息,库存系统根据下单信息,进行库存操作。 假如:在下单时库存系统不能正常使用。也不影响正常下单,因为下单后,订单系统写入消息队列就不再关心其他的后续操作了。实现订单系统与库存系统的应用解耦。

3)流量削锋,也是消息队列中的常用场景,一般在秒杀或团抢活动中使用广泛。 应用场景:秒杀活动,一般会因为流量过大,导致流量暴增,应用挂掉。为解决这个问题,一般需要在应用前端加入消息队列。 a)可以控制活动的人数; b)可以缓解短时间内高流量压垮应用; c)用户的请求,服务器接收后,首先写入消息队列。假如消息队列长度超过最大数量,则直接抛弃用户请求或跳转到错误页面; d)秒杀业务根据消息队列中的请求信息,再做后续处理。

4)日志处理

5)消息通讯 消息通讯是指,消息队列一般都内置了高效的通信机制,因此也可以用在纯的消息通讯。比如实现点对点消息队列,或者聊天室等。

6)性能 RocketMQ单机也可以支持亿级的消息堆积能力。单机写入TPS单实例约7万条/秒,单机部署3个Broker,可以跑到最高12万条/秒,消息大小10个字节

如上图所示, RocketMQ的部署结构有以下特点:

1)Name Server 可集群部署,节点之间无任何信息同步。

2)Broker(消息中转角色,负责存储消息,转发消息) 部署相对复杂,Broker 分为Master 与Slave,一个Master 可以对应多个Slave,但是一个Slave 只能对应一个Master,Master 与Slave 的对应关系通过指定相同的BrokerName,不同的BrokerId来定 义,BrokerId为0 表示Master,非0 表示Slave。Master 也可以部署多个。每个Broker 与Name。

3)Producer 与Name Server 集群中的其中一个节点(随机选择)建立长连接,定期从Name Server 取Topic 路由信息,并向提供Topic 服务的Master 建立长连接,且定时向Master 发送心跳。Producer 完全无状态,可集群部署。

4)Consumer 与Name Server 集群中的其中一个节点(随机选择)建立长连接,定期从Name Server 取Topic 路由信息,并向提供Topic 服务的Master、Slave 建立长连接,且定时向Master、Slave 发送心跳。Consumer既可以从Master 订阅消息,也可以从Slave 订阅消息,订阅规则由Broker 配置决定。

Broker:消息中转角色,负责存储消息,转发消息 Broker集群有多种配置方式:

1)单Master 优点:除了配置简单没什么优点 缺点:不可靠,该机器重启或宕机,将导致整个服务不可用

2)多Master 优点:配置简单,性能最高 缺点:可能会有少量消息丢失(配置相关),单台机器重启或宕机期间,该机器下未被消费的消息在机器恢复前不可订阅,影响消息实时性

3)多Master多Slave,每个Master配一个Slave,有多对Master-Slave,集群采用异步复制方式,主备有短暂消息延迟,毫秒级 优点:性能同多Master几乎一样,实时性高,主备间切换对应用透明,不需人工干预 缺点:Master宕机或磁盘损坏时会有少量消息丢失

4)多Master多Slave,每个Master配一个Slave,有多对Master-Slave,集群采用同步双写方式,主备都写成功,向应用返回成功 优点:服务可用性与数据可用性非常高 缺点:性能比异步集群略低,当前版本主宕备不能自动切换为主

Master和Slave的配置文件参考conf目录下的配置文件 Master与Slave通过指定相同的brokerName参数来配对,Master的BrokerId必须是0,Slave的BrokerId必须是大于0的数。 一个Master下面可以挂载多个Slave,同一Master下的多个Slave通过指定不同的BrokerId来区分。

下面模拟两台机器上搭建集群 -- 主从散落在不同节点上

名称

broker角色

IP&port

nameSer-1

注册服务中心

10.xx.xx.111:9876

nameSer-2

注册服务中心

10.xx.xx.112:9876

broker-a-0

master

10.xx.xx.111:10911   broker-a.properties

broker-a-1

slave

10.xx.xx.112:10921   broker-a-s.properties

broker-b-0

master

10.xx.xx.112:10911   broker-b.properties

broker-b-1

slave

10.xx.xx.111:10921   broker-b-s.properties

第一步:
关闭2台机器的iptables和selinux(所有节点机器上都要操作)
[root@master01 ~]# /etc/init.d/iptables stop
[root@master01 ~]# vim /etc/sysconfig/selinux
......
SELINUX=disabled
[root@master01 ~]# setenforce 0
[root@master01 ~]# getenforce
Permissive
第二步:linux上准备好jdk,配置好环境变量,还有安装包maven,这里不再赘述,自行Google解决。。

第三步:去RocketMQ官网下载源码包 RocketMQ官网 如下图:这里安装最新版本RocketMQ4.3.0

下载下来丢到linux上,unzip  ....zip 解压 ,解压后 再执行maven编译打包

然后 cd distribution/target/apache-rocketmq 该目录下,然后把apache-rocketmq 移动到最其他目录,比如 /opt/ 并改名

rocketmq-4.3.0

第四步:接下来先建好目录,进入 rocketmq-4.3.0目录,把logs 目录、data/store/........如下所示的文件夹建好

第五步:把启动脚本的参数改下,

1.vim runbroker.sh  (因为默认的值适合在生产上使用)     -server -Xms512m -Xmx512m -Xmn256m

2. vim runserver.sh (同样的道理) -server -Xms512m -Xmx512m -Xmn126m -XX:PermSize=128m -XX:MaxPermSize=320m

3. vim tools.sh           -server -Xms256m -Xmx256m -Xmn128m -XX:PermSize=128m -XX:MaxPermSize=128m

第六步:到/opt/rocketmq-4.3.0/conf/2m-2s-async 下 修改这四个文件

111机器上主要 broker-a.properties  broker-b-s.properties两个文件 内容分别如下

brokerClusterName=RocketMQCluster
brokerName=broker-a
brokerId=0
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
##Broker 对外服务的监听端口
listenPort=10911
#nameserver地址,分号分割
namesrvAddr=10.xx.xx.111:9876;10.xx.xx.112:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true

brokerIP1=10.100.50.111
storePathRootDir=/opt/rocketmq-4.3.0/data/store
storePathCommitLog=/opt/rocketmq-4.3.0/data/store/commitlog
# 消费队列存储路径存储路径
storePathConsumerQueue=/opt/rocketmq-4.3.0/data/store/consumequeue
#消息索引存储路径
storePathIndex=/opt/rocketmq-4.3.0/data/store/index
#checkpoint 文件存储路径
storeCheckpoint=/opt/rocketmq-4.3.0/data/store/checkpoint
#abort 文件存储路径
abortFile=/opt/rocketmq-4.3.0/data/store/abort

#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
#diskMaxUsedSpaceRatio=88

brokerClusterName=RocketMQCluster
brokerName=broker-b
brokerId=1
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=10921
#nameserver地址,分号分割
namesrvAddr=10.xx.xx.111:9876;10.xx.xx.112:9876
brokerIP1=10.xx.xx.111
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true

storePathRootDir=/opt/rocketmq-4.3.0/data2/store
storePathCommitLog=/opt/rocketmq-4.3.0/data2/store/commitlog2
# 消费队列存储路径存储路径
storePathConsumerQueue=/opt/rocketmq-4.3.0/data2/store/consumequeue2
#消息索引存储路径
storePathIndex=/opt/rocketmq-4.3.0/data2/store/index2
#checkpoint 文件存储路径
storeCheckpoint=/opt/rocketmq-4.3.0/data2/store/checkpoint2
#abort 文件存储路径
abortFile=/opt/rocketmq-4.3.0/data2/store/abort2
 

#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
#diskMaxUsedSpaceRatio=88

112机器上只修改 broker-b.properties 和broker-a-s.properties 内容分别如下:

brokerClusterName=RocketMQCluster
brokerName=broker-b
brokerId=0
brokerRole=ASYNC_MASTER

flushDiskType=ASYNC_FLUSH
listenPort=10911
#nameserver地址,分号分割
namesrvAddr=10.xx.xx.111:9876;10.xx.xx.112:9876
brokerIP1=10.xx.xx.112
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true

storePathRootDir=/opt/rocketmq-4.3.0/data/store
storePathCommitLog=/opt/rocketmq-4.3.0/data/store/commitlog
# 消费队列存储路径存储路径
storePathConsumerQueue=/opt/rocketmq-4.3.0/data/store/consumequeue
#消息索引存储路径
storePathIndex=/opt/rocketmq-4.3.0/data/store/index
#checkpoint 文件存储路径
storeCheckpoint=/opt/rocketmq-4.3.0/data/store/checkpoint
#abort 文件存储路径
abortFile=/opt/rocketmq-4.3.0/data/store/abort
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000

#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
#diskMaxUsedSpaceRatio=88

brokerClusterName=RocketMQCluster
brokerName=broker-a
brokerId=1
listenPort=10921
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
namesrvAddr=10.xx.xx.111:9876;10.xx.xx.112:9876
brokerIP1=10.xx.xx.112
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true

storePathRootDir=/opt/rocketmq-4.3.0/data2/store
storePathCommitLog=/opt/rocketmq-4.3.0/data2/store/commitlog
# 消费队列存储路径存储路径
storePathConsumerQueue=/opt/rocketmq-4.3.0/data2/store/consumequeue
#消息索引存储路径
storePathIndex=/opt/rocketmq-4.3.0/data2/store/index
#checkpoint 文件存储路径
storeCheckpoint=/opt/rocketmq-4.3.0/data2/store/checkpoint
#abort 文件存储路径
abortFile=/opt/rocketmq-4.3.0/data2/store/abort
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000

#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
#diskMaxUsedSpaceRatio=88

最后启动

1.111 112 上都执行
bin目录下执行启动nameServer  
nohup sh bin/mqnamesrv > ./logs/namesrvrun.log 2>&1 &

111上启动master
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties -n"10.xx.xx.111:9876;10.xx.xx.112:9876" > ./logs/broker-a.log 2>&1 &


112上启动master
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties  -n"10.xx.xx.111:9876;10.xx.xx.112:9876" > ./logs/broker-b.log 2>&1 &

111上启动slave 
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties -n"10.xx.xx.111:9876;10.xx.xx.112:9876" > ./logs/broker-b-s.log 2>&1 &


112上启动slave 
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties -n"10.xx.xx.111:9876;10.xx.xx.112:9876" > ./logs/broker-a-s.log 2>&1 &

每台机器上两个broker 才对

最后部署RocketMQ监控平台

4)rocketMq监控平台rocketmq-console部署(我部署在112机器上操作)

下面的操作不是在我机器上,大致步骤一样,根据个人情况修改即可,
也可以直接git在线下载
[root@mq-console-nameserver ~]# cd /opt
[root@mq-console-nameserver opt]# git clone https://github.com/apache/rocketmq-externals.git

做软链接
[root@mq-console-nameserver opt]# ln -s /opt/rocketmq-externals /data/
[root@mq-console-nameserver opt]# ll /data/rocketmq-externals
lrwxrwxrwx. 1 root root 23 May 9 14:10 /data/rocketmq-externals -> /opt/rocketmq-externals

修改配置文件
[root@mq-console-nameserver ~]# vim /data/rocketmq-externals/rocketmq-console/src/main/resources/application.properties
server.contextPath=
server.port=8080 #默认访问端口是8080
#spring.application.index=true
spring.application.name=rocketmq-console
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
logging.config=classpath:logback.xml
#if this value is empty,use env value rocketmq.config.namesrvAddr NAMESRV_ADDR | now, you can set it in ops page.default localhost:9876
rocketmq.config.namesrvAddr=192.168.10.207:9876 #如果nameserver是多台集群机器,则后面就配置多个ip+port,即"rocketmq.config.namesrvAddr=ip1:port;ip2:port"
#if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
rocketmq.config.isVIPChannel=false #注意这个参数,是否设置为false或true取决于rocketmq的版本号
#rocketmq-console's data path:dashboard/monitor
rocketmq.config.dataPath=/tmp/rocketmq-console/data
#set it false if you don't want use dashboard.default true
rocketmq.config.enableDashBoardCollect=true

------------------------------------------------------------------------------------------
温馨提示:
由于我这里使用的rocketmq是3.2.6版本,低于3.5.8版本,所以上面的rocketmq.config.isVIPChannel应设置为false,默认为true!
Rocket如果开启了VIP通道,VIP通道端口为10911-2=10909。若Rocket服务器未启动端口10909,则报connect to <:10909> failed。
------------------------------------------------------------------------------------------

安装Maven的安装
[root@mq-console-nameserver ~]# cd /data/software/
[root@mq-console-nameserver software]# wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
[root@mq-console-nameserver software]# tar -zvxf apache-maven-3.3.9-bin.tar.gz
[root@mq-console-nameserver software]# mv apache-maven-3.3.9 /usr/local/maven
[root@mq-console-nameserver software]# vim /etc/profile
........
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:$MAVEN_HOME/bin
[root@mq-console-nameserver software]# source /etc/profile
[root@mq-console-nameserver software]# mvn --version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_131/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-696.el6.x86_64", arch: "amd64", family: "unix"

接着对rocketmq-console进行编译打包运行(mvn的编辑过程需要等待一段时间)
[root@mq-console-nameserver ~]# cd /data/rocketmq-externals/rocketmq-console/
[root@mq-console-nameserver rocketmq-console]# mvn clean package -Dmaven.test.skip=true

编译成功后在target下找到文件:rocketmq-console-ng-1.0.0.jar
[root@mq-console-nameserver rocketmq-console]# ls
doc LICENSE NOTICE pom.xml README.md src style target
[root@mq-console-nameserver rocketmq-console]# ls target/
checkstyle-cachefile classes maven-status rocketmq-console-ng-1.0.0-sources.jar
checkstyle-checker.xml generated-sources rocketmq-console-ng-1.0.0.jar
checkstyle-result.xml maven-archiver rocketmq-console-ng-1.0.0.jar.original

启动rocketmq-console,执行命令:
[root@mq-console-nameserver rocketmq-console]# nohup java -jar target/rocketmq-console-ng-1.0.0.jar >/dev/null 2>&1 &
----------------------------------------------------------------------------------------------------
也可以直接跟NameServer的ip和端口进行启动
# java -jar rocketmq-console-ng-1.0.0.jar --server.port=8080 --rocketmq.config.namesrvAddr=192.168.10.207:9876

如果NameServer有多台集群机器的话,后面就多加
# java -jar rocketmq-console-ng-1.0.0.jar --server.port=8080 --rocketmq.config.namesrvAddr=192.168.10.207:9876;192.168.10.208:9876

这里需要注意两个参数:
--server.port=8080 指定console访问端口,默认的就是8080,也可以指定为其他端口
--rocketmq.config.namesrvAddr 指定nameserver的地址
----------------------------------------------------------------------------------------------------

rocketmq-console的默认端口是8080
[root@mq-console-nameserver rocketmq-console]# lsof -i:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 5574 confluence 81u IPv6 24674767 0t0 TCP mq-console-nameserver:44888->mq-console-nameserver:webcache (CLOSE_WAIT)
java 5649 jira 157u IPv6 879851 0t0 TCP *:webcache (LISTEN)
java 5649 jira 249u IPv6 4166247 0t0 TCP mq-console-nameserver:46886->mq-console-nameserver:webcache (CLOSE_WAIT)
java 5649 jira 262u IPv6 24669705 0t0 TCP mq-console-nameserver:43464->mq-console-nameserver:webcache (CLOSE_WAIT)

最后再浏览器里输入:http://10.xx.112:8076,(这是我自己的)即可访问rockermq的web管理界面了

猜你喜欢

转载自www.cnblogs.com/gavin5033/p/12583687.html