“水杉在线”-RocketMQ
集成(一、部署及测试)
1、拉取镜像
这里我们选择了前两天刚刚发布的最新版本5.1.0版本,听说有很多提升,相比于版本4来说。
1 2 3 4
| # 拉取rocketmq docker pull apache/rocketmq:5.1.0 # 拉取rocketmq-dashboard docker pull styletang/rocketmq-console-ng:latest
|
2、环境配置
目前本地以及测试环境主要是单机部署 ,因此namesrv
和broker
都部署在一个机器上,同时请注意端口映射以及防火墙以及数据卷挂载的目录,以及broker
的配置文件,同时command
命令注意使broker.conf
配置生效。对于内存分配如果是生产环境尽量大一些,比如8G
起步。
**注意执行 chmod 777 -R your_rocketmq_dir
给你挂载的数据卷写入的权限,否则日志和数据等写不进来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| version: "3" services: namesrv: image: apache/rocketmq:5.1.0 container_name: rocketmq-namesrv ports: - "9876:9876" environment: JAVA_OPT: -server -Xms2g -Xmx2g -Xmn1g command: sh mqnamesrv volumes: - your_namesrv_logs_dir:/home/rocketmq/logs/rocketmqlogs
broker: image: apache/rocketmq:5.1.0 container_name: rocketmq-broker ports: - "10911:10911" - "10909:10909" - "10912:10912" environment: JAVA_OPT_EXT: -server -Xms2g -Xmx2g -Xmn1g command: sh mqbroker -n rocketmq-namesrv:9876 -c /home/rocketmq/rocketmq-5.1.0/conf/broker.conf depends_on: - namesrv volumes: - .your_broker_conf_dir:/home/rocketmq/rocketmq-5.1.0/conf/broker.conf - your_namesrv_logs_dir:/home/rocketmq/logs/rocketmqlogs - your_namesrv_store_dir:/home/rocketmq/store
dashboard: image: styletang/rocketmq-console-ng:latest container_name: rocketmq-dashboard ports: - "8086:8080" environment: - rocketmq.config.namesrvAddr=rocketmq-namesrv:9876
|
3、启动RocketMQ
请注意使用docker-compose up -d
来启动,在broker.conf
中建议明确brokerIP
broker.conf
配置如下:
1 2 3 4 5 6 7 8 9 10 11
| brokerClusterName = DefaultCluster brokerName = broker-a brokerId = 0 deleteWhen = 04 fileReservedTime = 48 brokerRole = ASYNC_MASTER flushDiskType = ASYNC_FLUSH brokerIP1 = your_broker_ip # 这个ip一般就是你宿主机的外网ip 不要localhost/127.0.0.1 可能在springboot测试 时无法连接 listenPort = 10911 autoCreateTopicEnable = true autoCreateSubscriptionGroup = true
|
启动成功后可以通过docker logs -f [container_id]
查看相关日志 确定是否成功
也可以通过dashboard
来进行测试,如下图所示:
你可以通过这个dashboard
来进行一些消息的生产和消费以确定是否可以正常收发消息。
4、SpringCloud
整合RocketMQ
4.1、pom.xml引入依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.9.4</version> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency>
|
4.2、application.yml中配置基本变量
1 2 3 4 5 6 7 8 9 10 11 12 13
| mq: topic: your_topic consumer: group: your-consumer-group retry-times-when-send-failed: 5 send-message-timeout: 50000
rocketmq: name-server: namesrvIP:9876 producer: group: your-producer-group retry-times-when-send-failed: 5 send-message-timeout: 50000
|
4.3、生产者连接并发送消息
1 2 3 4 5 6
| @Test public void testMQProducer() { String message = "Hello World"; rocketMQTemplate.convertAndSend("shuishan-dev",message); System.out.println("消息发送成功"); }
|
4.4、消费者消费消息(最好新建一个module or server)
1 2 3 4 5 6 7 8 9 10
| @Component @RocketMQMessageListener(consumerGroup = "shuishan-consumer-group",topic = "shuishan-dev") public class MyConsumer implements RocketMQListener<String> {
@Override public void onMessage(String s) { System.out.println("收到的消息" + s); } }
|
至此 最基本的RocketMQ
部署并测试完成。
祝大家五一节快乐!!!