1、安装
1、Docker安装
1、单机部署
# 镜像拉取
docker pull foxiswho/rocketmq:server-4.3.2
docker pull foxiswho/rocketmq:broker-4.3.2
# 挂载目录
mkdir -p /opt/docker/rocketmq/rmqserver01/logs
mkdir -p /opt/docker/rocketmq/rmqserver01/store
mkdir -p /opt/docker/rocketmq/rmqbroker01/logs
mkdir -p /opt/docker/rocketmq/rmqbroker01/store
mkdir -p /opt/docker/rocketmq/rmqbroker01/conf
Broker配置文件修改
vi /opt/docker/rocketmq/rmqbroker01/conf/broker.conf
# 命名服务器地址
namesrvAddr=172.17.88.204:9876
# 集群名称
brokerClusterName = DefaultCluster
# broker名称
brokerName = broker-a
# broker id
brokerId = 0
# 删除时间
deleteWhen = 04
fileReservedTime = 48
# 角色
brokerRole = ASYNC_MASTER
# 刷盘方式
flushDiskType = ASYNC_FLUSH
# 绑定IP
brokerIP1 = 172.17.88.204
# 绑定端口
listenPort=10911
启动
#启动nameserver容器
docker run -d -p 9876:9876 --name rmqserver01 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/rmqserver01/logs:/opt/logs -v /opt/docker/rocketmq/rmqserver01/store:/opt/store foxiswho/rocketmq:server-4.3.2
# 启动broker,这里net是为了注册上nameserver的服务地址是可访问的
docker run -it -d -p 10911:10911 --name rmqbroker01 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/rmqbroker01/conf/broker.conf:/etc/rocketmq/broker.conf -v /opt/docker/rocketmq/rmqbroker01/logs:/opt/logs -v /opt/docker/rocketmq/rmqbroker01/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
管理工具安装
#创建并启动容器
docker run -d -e "JAVA_OPTS=-Drocketmq.namesrv.addr=172.17.88.204:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false" -p 8082:8080 -t styletang/rocketmq-console-ng:1.0.0
2、集群部署
2个nameserver、2个master、两个slave,使用同步方式同步数据
mkdir -p /opt/docker/rocketmq/name-server01/logs
mkdir -p /opt/docker/rocketmq/name-server01/store
mkdir -p /opt/docker/rocketmq/name-server02/logs
mkdir -p /opt/docker/rocketmq/name-server02/store
mkdir -p /opt/docker/rocketmq/broker01-master/logs
mkdir -p /opt/docker/rocketmq/broker01-master/store
mkdir -p /opt/docker/rocketmq/broker01-master/conf
mkdir -p /opt/docker/rocketmq/broker01-slave/logs
mkdir -p /opt/docker/rocketmq/broker01-slave/store
mkdir -p /opt/docker/rocketmq/broker01-slave/conf
mkdir -p /opt/docker/rocketmq/broker02-master/logs
mkdir -p /opt/docker/rocketmq/broker02-master/store
mkdir -p /opt/docker/rocketmq/broker02-master/conf
mkdir -p /opt/docker/rocketmq/broker02-slave/logs
mkdir -p /opt/docker/rocketmq/broker02-slave/store
mkdir -p /opt/docker/rocketmq/broker02-slave/conf
修改两个Broker配置
Master1
vim /opt/docker/rocketmq/broker01-master/conf/broker.conf
# 命名服务器地址
namesrvAddr=172.17.88.204:9876;172.17.88.204:9877
# 集群名称
brokerClusterName = DefaultCluster
# broker名称
brokerName = broker-a
# broker id
brokerId = 0
# 删除时间
deleteWhen = 04
fileReservedTime = 48
# 同步方式
brokerRole = SYNC_MASTER
# 刷盘方式
flushDiskType = SYNC_FLUSH
# 绑定IP
brokerIP1 = 112.74.39.44
# 绑定端口
listenPort = 10911
Slave1
vim /opt/docker/rocketmq/broker01-slave/conf/broker.conf
# 命名服务器地址
namesrvAddr=172.17.88.204:9876;172.17.88.204:9877
# 集群名称
brokerClusterName = DefaultCluster
# broker名称
brokerName = broker-a
# broker id
brokerId = 1
# 删除时间
deleteWhen = 04
fileReservedTime = 48
# 角色
brokerRole = SLAVE
# 刷盘方式
flushDiskType = SYNC_FLUSH
# 绑定IP
brokerIP1 = 112.74.39.44
# 绑定端口
listenPort = 10912
Master2
vim /opt/docker/rocketmq/broker02-master/conf/broker.conf
# 命名服务器地址
namesrvAddr=172.17.88.204:9876;172.17.88.204:9877
# 集群名称
brokerClusterName = DefaultCluster
# broker名称
brokerName = broker-b
# broker id
brokerId = 0
# 删除时间
deleteWhen = 04
fileReservedTime = 48
# 同步方式
brokerRole = SYNC_MASTER
# 刷盘方式
flushDiskType = SYNC_FLUSH
# 绑定IP
brokerIP1 = 112.74.39.44
# 绑定端口
listenPort = 10913
Slave2
vim /opt/docker/rocketmq/broker02-slave/conf/broker.conf
# 命名服务器地址
namesrvAddr=172.17.88.204:9876;172.17.88.204:9877
# 集群名称
brokerClusterName = DefaultCluster
# broker名称
brokerName = broker-b
# broker id
brokerId = 1
# 删除时间
deleteWhen = 04
fileReservedTime = 48
# 角色
brokerRole = SLAVE
# 刷盘方式
flushDiskType = SYNC_FLUSH
# 绑定IP
brokerIP1 = 112.74.39.44
# 绑定端口
listenPort = 10914
启动容器
#启动nameserver容器
docker run -d -p 9876:9876 --name name-server01 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/name-server01/logs:/opt/logs -v /opt/docker/rocketmq/name-server01/store:/opt/store foxiswho/rocketmq:server-4.3.2
docker run -d -p 9877:9876 --name name-server02 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/name-server02/logs:/opt/logs -v /opt/docker/rocketmq/name-server02/store:/opt/store foxiswho/rocketmq:server-4.3.2
# 启动broker,双主双从
docker run -it -d -p 10911:10911 --name broker-master01 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/broker01-master/conf/broker.conf:/etc/rocketmq/broker.conf -v /opt/docker/rocketmq/broker01-master/logs:/opt/logs -v /opt/docker/rocketmq/broker01-master/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
docker run -it -d -p 10912:10912 --name broker-slave01 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/broker01-slave/conf/broker.conf:/etc/rocketmq/broker.conf -v /opt/docker/rocketmq/broker01-slave/logs:/opt/logs -v /opt/docker/rocketmq/broker01-slave/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
docker run -it -d -p 10913:10913 --name broker-master02 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/broker02-master/conf/broker.conf:/etc/rocketmq/broker.conf -v /opt/docker/rocketmq/broker02-master/logs:/opt/logs -v /opt/docker/rocketmq/broker02-master/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
docker run -it -d -p 10914:10914 --name broker-slave02 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /opt/docker/rocketmq/broker02-slave/conf/broker.conf:/etc/rocketmq/broker.conf -v /opt/docker/rocketmq/broker02-slave/logs:/opt/logs -v /opt/docker/rocketmq/broker02-slave/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
# 启动dashbord
docker run -d -p 8082:8080 --name rocketma-console -e "JAVA_OPTS=-Drocketmq.namesrv.addr=172.17.88.204:9876;172.17.88.204:9877 -Dcom.rocketmq.sendMessageWithVIPChannel=false" styletang/rocketmq-console-ng:1.0.0
2、快速开始
1、测试收发
# 快速向TopTest发送1000条消息
export NAMESRV_ADDR=localhost:9876
./tools.sh org.apache.rocketmq.example.quickstart.Producer
# 消费消息
export NAMESRV_ADDR=localhost:9876
./tools.sh org.apache.rocketmq.example.quickstart.Consumer
一些问题
RocketMQ的结构
基础架构分为Namespace和Broker
Broker下有多个Topic,每个Topic下有多个队列
生产者和发送者收发消息的时候可以指定Tag和队列
RocketMQ的存储结构
消息的相关信息存储到commitlog文件中,为了加速查询的速度,会在ConsumerQueue和Index目录下存放消息的offset;
consumerqueue存放的是消费组的索引,Index存放时间等其它条件的索引;
消息消费时,先根据消费组和Topic去consumerQueue中找到对应的offset,然后去commitlog中读取。
RocketMQ的负载均衡
集群模式默认就是负载均衡的,当有多个消费者(同一个消费组)时,则每个consumer平均分配consumer queue;
还有一种时环状分流的算法,即在broker基础上再进行平均分配;
消费者数量要小于Queue的数量;
RocketMQ中的消息重试
顺序消息:
顺序消息,单一消费者,消费失败时,会进行重试导致阻塞,需要监控并处理失败的情况。
顺序消息将无限重试,是否是顺序消息取决于消费者端的设置。
无序消息:
集群模式重试,广播模式失败不再重试;
重试时间不断递增,最多16次;
消费时返回null,抛出异常,返回reconsumer都将导致重试;
重试次数在消费者端进行设置(对整个消费者组生效),传入Properties,超过16次,每次等待2小时后重试。
RocketMQ的Tag
消费组消费时,如果对应消息不符合消费者的tag,则会跳过该消息,并且将offset移动;
意味着后面消费组改了tag过滤规则,也无法读取到前面符合规则的消息了,因为当前的offset已经是最新的了。
RocketMQ的offset
RocketMQ是以消费组的方式进行消费的,首先消息通过生产者发送到对应的Broker下的队列中;
消费者注册上来指定消费者组,Broker存储指定消费组对应Topic下的每个队列的偏移量,存储位置如下
vim ${user.home}/store/config/consumerOffset.json
格式如下:
{
"offsetTable":{
"zanpocc@base_consumer_group":{0:0,1:0,2:5,3:0,4:0,5:0,6:0,7:0,8:0,9:0,10:0,11:0,12:0,13:0,14:0,15:0},
"%RETRY%order_consumer_group@order_consumer_group":{0:0},
"test_topic@base_consumer_group":{0:8,1:7,2:6,3:4},
"zanpocc@order_consumer_group":{0:0,1:0,2:5,3:0,4:3,5:0,6:4,7:0,8:3,9:0,10:4,11:0,12:3,13:3,14:2,15:2},
"%RETRY%base_consumer_group@base_consumer_group":{0:0}
}
}
// 即
"offsetTable":{
"topic名称@消费组名称":{队列ID:偏移}
}
RocketMQ中的死信队列
当消息重试次数超过限制后,消息进入死信队列中;
死信队列对应的是一个消费者组。消费者组的所有重试失败消息都在这个队列,与Topic无关;
RocketMQ的事物消息
半消息会同步发送,发送成功后会运行本地事物,本地事物执行成功后MQ回调检查本地事物,回调检查成功后提交消息。
幂等性
使用业务主键等唯一标识,判断消息是否重复消费
线程安全
RocketMQ的生产者和消费者客户端对象是线程安全的
标签:opt,store,broker,RocketMQ,conf,docker,rocketmq From: https://www.cnblogs.com/zanpocc/p/16715592.html