方案:
1 个 Name Server
2 个 Master Broker
Docker 使用不同端口号部署
10.168.. rocketMq 端口号:
rmqnamesrv-a 9876
rmqbroker-a 10909,10911,10912
rmqbroker-b 11909,11911,11912
rmqconsole 9000
http://id:9000/
https://www.likecs.com/show-62610.html
https://blog.csdn.net/qiaodaima0/article/details/125431771
docker pull rocketmqinc/rocketmq:4.3.0
docker pull styletang/rocketmq-console-ng
curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
mkdir docker_rocketmq_data
cd docker_rocketmq_data
mkdir -p ./logs/nameserver-a
mkdir -p ./store/nameserver-a
mkdir -p ./logs/broker-a
mkdir -p ./store/broker-a
mkdir -p ./logs/broker-b
mkdir -p ./store/broker-b
brokerClusterName = rocketmq-cluster
brokerName = broker-a
brokerId = 0
brokerIP1 = 10.168.2.14
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
namesrvAddr = 10.168.2.14:9876
autoCreateTopicEnable=true
listenPort = 10911
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
brokerClusterName = rocketmq-cluster
brokerName = broker-b
brokerId = 0
brokerIP1 = 10.168.2.14
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
namesrvAddr=10.168.2.14:9876
autoCreateTopicEnable=true
listenPort = 11911
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
![image.png](https://img-blog.csdnimg.cn/img_convert/54f85c6bdd907e367843425a17ca9b23.png#clientId=u77382857-7476-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=307&id=u4b7b1498&margin=[object Object]&name=image.png&originHeight=614&originWidth=930&originalType=binary&ratio=1&rotation=0&showTitle=false&size=119264&status=done&style=none&taskId=udfd625bc-dcea-4837-bf91-ab23a44ff56&title=&width=465)
注意修改 volumes 路径
version: '3.5'
services:
rmqnamesrv-a:
image: rocketmqinc/rocketmq:4.3.0
container_name: rmqnamesrv-a
ports:
- 9876:9876
volumes:
- /home/userName/docker_rocketmq_data/logs/nameserver-a:/opt/logs
- /home/userName/docker_rocketmq_data/store/nameserver-a:/opt/store
command: sh mqnamesrv
networks:
rmq:
aliases:
- rmqnamesrv-a
rmqbroker-a:
image: rocketmqinc/rocketmq:4.3.0
container_name: rmqbroker-a
ports:
- 10909:10909
- 10911:10911
- 10912:10912
volumes:
- /home/userName/docker_rocketmq_data/logs/broker-a:/opt/logs
- /home/userName/docker_rocketmq_data/store/broker-a:/opt/store
- /home/userName/docker_rocketmq_data/broker-a.conf:/opt/rocketmq-4.3.0/conf/broker.conf
environment:
TZ: Asia/Shanghai
NAMESRV_ADDR: "rmqnamesrv-a:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms256m -Xmx256m -Xmn256m"
command: sh mqbroker -c /opt/rocketmq-4.3.0/conf/broker.conf autoCreateTopicEnable=true &
links:
- rmqnamesrv-a:rmqnamesrv-a
networks:
rmq:
aliases:
- rmqbroker-a
rmqbroker-b:
image: rocketmqinc/rocketmq:4.3.0
container_name: rmqbroker-b
ports:
- 11909:11909
- 11911:11911
- 11912:11912
volumes:
- /home/userName/docker_rocketmq_data/logs/broker-b:/opt/logs
- /home/userName/docker_rocketmq_data/store/broker-b:/opt/store
- /home/userName/docker_rocketmq_data/broker-b.conf:/opt/rocketmq-4.3.0/conf/broker.conf
environment:
TZ: Asia/Shanghai
NAMESRV_ADDR: "rmqnamesrv-b:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms256m -Xmx256m -Xmn256m"
command: sh mqbroker -c /opt/rocketmq-4.3.0/conf/broker.conf autoCreateTopicEnable=true &
links:
- rmqnamesrv-a:rmqnamesrv-a
networks:
rmq:
aliases:
- rmqbroker-b
rmqconsole:
image: styletang/rocketmq-console-ng
container_name: rmqconsole
ports:
- 9000:8080
environment:
JAVA_OPTS: -Drocketmq.namesrv.addr=rmqnamesrv-a:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false
networks:
rmq:
aliases:
- rmqconsole
networks:
rmq:
name: rmq
driver: bridge
docker-compose -f docker-compose.yml up -d
10.168.2.14 rocketMq 端口号:
rmqnamesrv-a 9876
rmqbroker-a 10909,10911,10912
rmqbroker-b 11909,11911,11912
rmqconsole 9000
http://ip:9000/
brokerClusterName = rocketmq-cluster
brokerName = broker-a
brokerId = 0
#这个很有讲究 如果是正式环境 这里一定要填写内网地址(安全)
#如果是用于测试或者本地这里建议要填外网地址,因为你的本地代码是无法连接到阿里云内网,只能连接外网。
brokerIP1 = xxxxx
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# 内网的(阿里云有内网IP和外网IP)
namesrvAddr=172.18.0.5:9876;172.18.0.5:9877
autoCreateTopicEnable=true
#Broker 对外服务的监听端口,
listenPort = 10911
#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.3.0</version>
</dependency>
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
/**
* 消息生产者
* @author huangzr
* @date 2022/7/8 15:12
*/
public class Producer {
public static void main(String[] args) throws Exception {
DefaultMQProducer producer = new DefaultMQProducer("niwei_producer_group");
producer.setNamesrvAddr("ip:9876");
producer.setSendMsgTimeout(100000);
producer.start();
for (int i = 0; i < 100; i++) {
Message msg = new Message(
"topic_rocket_example" /* 消息主题名 */,
"TagA" /* 消息标签 */,
("Hello Java demo RocketMQ " +
i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* 消息内容 */
);
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
producer.shutdown();
}
}
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
/**
* 消费者
* @author huangzr
* @date 2022/7/8 15:13
*/
public class Consumer {
public static void main(String[] args) throws Exception {
DefaultMQPushConsumer consumer = new
DefaultMQPushConsumer("niwei_consumer_group");
consumer.setNamesrvAddr("ip:9876");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.subscribe("topic_rocket_example", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext context) {
if (list != null) {
for (MessageExt ext : list) {
try {
System.out.println(new Date() + new
String(ext.getBody(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.out.println("消息消费者已启动");
}
}