1、Docker安装RcoketMQ-4.9.4
- 在同级文件夹创建目录config,并在里面创建文件broker.conf,文件内容如下:
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
## 注意:brokerIP1 要设置成自己服务器的ip
brokerIP1=XXX.XXX.XXX.XXX
- docker-compose.yaml:
version: '3'
services:
# rocket mq name server
rmqnamesrv:
image: apache/rocketmq:4.9.4
restart: always
container_name: rocket-server
# environment:
# JAVA_OPT_EXT: "-server -Xms64m -Xmx64m -Xmn64m"
# volumes:
# 映射本地目录权限一定要设置为 777 权限,否则启动不成功
# - ../volumes/data/rocket/server/logs:/home/rocketmq/logs
networks:
- rocketmq
ports:
- 9876:9876
command: sh mqnamesrv
# rocket mq broker
rmqbroker:
image: apache/rocketmq:4.9.4
restart: always
container_name: rocket-broker
volumes:
# 映射本地目录权限一定要设置为 777 权限,否则启动不成功
# - ../volumes/data/rocket/broker/logs:/home/rocketmq/logs
# - ../volumes/data/rocket/broker/store:/home/rocketmq/store
- ./config/broker.conf:/opt/rocketmq-4.9.4/conf/broker.conf
environment:
- NAMESRV_ADDR=rmqnamesrv:9876
# - JAVA_OPTS:=-Duser.home=/opt
- JAVA_OPT_EXT=-server -Xms64m -Xmx64m -Xmn64m
depends_on:
- rmqnamesrv
networks:
- rocketmq
ports:
- 10909:10909
- 10911:10911
command: sh mqbroker -c /opt/rocketmq-4.9.4/conf/broker.conf
# rocket dashboard 这个可以不需要
rmqdashboard:
image: apacherocketmq/rocketmq-dashboard:1.0.0
restart: always
container_name: rocket-dashboard
environment:
- JAVA_OPTS=-Drocketmq.config.namesrvAddr=rmqnamesrv:9876 -Dserver.port=8080 -Drocketmq.config.isVIPChannel=false
# - JAVA_OPT_EXT=-Xms128m -Xmx128m -Xmn128m
depends_on:
- rmqnamesrv
networks:
- rocketmq
ports:
- 8081:8080
networks:
rocketmq:
driver: host
SDK测试:
提前在界面或者命令行新建一个Topic,随后使用如下代码进行测试,
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.4</version>
</dependency>
public class SyncProducer {
public static void main(String[] args) throws Exception {
//(1) 初始化一个producer并设置Producer group name
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
//(2)设置NameServer地址
producer.setNamesrvAddr("xxx.xxx.xxx.xxx:9876");
// 启动producer
producer.start();
for (int i = 0; i < 100; i++) {
//(3)创建一条消息,并指定topic、tag、body等信息,tag可以理解成标签,对消息进行再归类,RocketMQ可以在消费端对tag进行过滤
Message msg = new Message(
"test-topic" ,
"TagA" ,
("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET)
);
//(4)利用producer进行发送,并同步等待发送结果
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
// 一旦producer不再使用,关闭producer
producer.shutdown();
}
}
备注:以上测试时,使用的是公网IP,需要开放服务器端口(可能不安全),如设置内网集群部署,继续学习更新…;
2、Docker安装RcoketMQ-5.X
备注:暂未跟进官方镜像内容(不清楚每个文件的存储位置),后续了解后更新。
3、参考资料
RocketMQ4.9.4通过docker-compose安装:https://blog.csdn.net/wlddhj/article/details/129185607
docker 设置国内镜像源:https://blog.csdn.net/wlddhj/article/details/129185607
RocketMQ4.9.4官方文档-普通消息发送-消息发送-同步发送