方案
因为borker本身需要存储消息信息,所以高可用方案选择主从方式。
主从方式可以通过主从同步热备或主从异步热备两种方式。
同步热备需要主从都将消息信息处理完成以后才返回,所以性能要差一些。
异步热备就是主结点成功处理消息完成就返回,从结点通过内部的同步机制主结点消息数据。
本案例采用主从异步热备模式。在RocketMQ默认的配置文件路径下有${ROCKET_HOME}/conf/2m-2s-async/目录路径,在该路径下有两主两从异步热备配置的模板。本案例因为只考虑高可用,不考虑高性能,所以只配置一主一从。只需要使用基于broker-a.properties和broker-a-s.properties两个模板文件进行修改配置。
如果要考虑高性能,只需要将broker-b.properties做类似修改就可以了。如果竟考虑高可用,又考虑高性能,则配置从个broker-x.properties和broker-x-s.properties配置就可以了。
namesrv配置
这里参照Docker下部署RocketMQ(一)namesrv单点配置就可以了。
broker镜像文件重构
首先需要修改一下Docker镜像生成的Docerfile,主要是为broker的启动指定配置文件的路径。
# Start from a Java image.
FROM java:8
# Rocketmq version
ENV ROCKETMQ_VERSION 4.2.0
# Rocketmq home
ENV ROCKETMQ_HOME /opt/rocketmq-${ROCKETMQ_VERSION}
WORKDIR ${ROCKETMQ_HOME}
RUN mkdir -p \
/opt/logs \
/opt/store
ADD rocketmq-all-4.2.0-bin-release.zip rocketmq.zip
RUN unzip rocketmq.zip \
&& rm rocketmq.zip
RUN chmod +x bin/mqbroker
CMD cd ${ROCKETMQ_HOME}/bin && export JAVA_OPT=" -Duser.home=/opt" && sh mqbroker -n namesrv:9876 -c /opt/conf/broker.properties
EXPOSE 10909 10911
VOLUME /opt/logs \
/opt/store \
/opt/conf
borker主结点配置
broker-a-m:
imag going/rocketmq-broker:4.2.1
ports:
- 10909:10909
- 10911:10911
volumes:
- "/rocketmq/broker/a-m/logs:/opt/logs"
- "/rocketmq/broker/a-m/stor/opt/store"
- "/rocketmq/broker/a-m/conf:/opt/conf"
links:
- namesrv:namesrv
borker配置文件为
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
borker从结点配置
broker-a-s:
imag going/rocketmq-broker:4.2.1
ports:
- 11909:10909
- 11911:10911
volumes:
- "/rocketmq/broker/a-s/logs:/opt/logs"
- "/rocketmq/broker/a-s/stor/opt/store"
- "/rocketmq/broker/a-s/conf:/opt/conf"
links:
- namesrv:namesrv
borker配置文件为
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
console配置
这里参照Docker下部署RocketMQ(一)console配置就可以了。
消息生产及消息客户端配置
这里参照Docker下部署RocketMQ(一)消息生产及消息客户端配置就可以了。
*备注:
通过测试发现当broker-a-m服务无效状态的情况下,消息生产客户端无法将消息发送到broker中去。
会抛出如下异常。
org.apache.rocketmq.client.exception.MQClientException: No route info of this topic, TopicTest1
See http://rocketmq.apache.org/docs/faq/ for further details.
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(DefaultMQProducerImpl.java:559)
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1049)
at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.send(DefaultMQProducerImpl.java:1008)
at org.apache.rocketmq.client.producer.DefaultMQProducer.send(DefaultMQProducer.java:204)
at Producer.main(Producer.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
org.apache.rocketmq.client.exception.MQClientException: No route info of this topic, TopicTest1
通过诊断,消息生产客户端是能正常连接到namesrv的,并且通过console监控界面可以看到,TopicTest1对应的路由里面是有到broker-a-s的信息的。后面通过查证,当master挂掉以后,slave是不能接收消息的。所以不是配置问题。
最后给出完整Docker compose配置
version: '2'
services:
namesrv:
imag going/rocketmq-namesrv:4.2.0
ports:
- 9876:9876
volumes:
- "/rocketmq/namesrv/master/logs:/opt/logs"
- "/rocketmq/namesrv/master/stor/opt/store"
broker-a-m:
imag going/rocketmq-broker:4.2.1
ports:
- 10909:10909
- 10911:10911
volumes:
- "/rocketmq/broker/a-m/logs:/opt/logs"
- "/rocketmq/broker/a-m/stor/opt/store"
- "/rocketmq/broker/a-m/conf:/opt/conf"
links:
- namesrv:namesrv
broker-a-s:
imag going/rocketmq-broker:4.2.1
ports:
- 11909:10909
- 11911:10911
volumes:
- "/rocketmq/broker/a-s/logs:/opt/logs"
- "/rocketmq/broker/a-s/stor/opt/store"
- "/rocketmq/broker/a-s/conf:/opt/conf"
links:
- namesrv:namesrv
consol
imag styletang/rocketmq-console-ng:latest
ports:
- "8080:8080"
links:
- namesrv:namesrv
environment:
JAVA_OPTS: -Drocketmq.config.namesrvAddr=namesrv:9876 #-Dcom.rocketmq.sendMessageWithVIPChannel=false
producer:
imag going/rocketmq-producer:4.1.0
links:
- namesrv:namesrv
consumer:
imag going/rocketmq-consumer:4.1.0
links:
- namesrv:namesrv