由于我们SpringCloud Alibaba 选择的版本是 2.2.6.RELEASE ,所以对应 RocketMQ 版本就暂定 4.4.0
Docker 安装 RocketMQ 步骤(记得指定版本):传送门
二、引入 RocketMQ 2.1、创建子模块项目:传送门 2.2、pom 文件 添加引用rocketmq: name-server: **.**.**.**:**** producer: group: wangjingGroup
2.4、新建 topic 2.5、发送消息代码package com.wangjing.servicenacos.controller;import org.apache.rocketmq.spring.core.RocketMQTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/test")public class TestController { @Autowired private RocketMQTemplate rocketMQTemplate; @RequestMapping("/test") public String test() { rocketMQTemplate.convertAndSend("wangjing-topic","哈哈哈"); return "测试service-nacos,端口:8801"; }}
2.6、监听消息代码package com.wangjing.servicenacos.listener;import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;import org.apache.rocketmq.spring.core.RocketMQListener;import org.springframework.stereotype.Component;@Component@RocketMQMessageListener( topic = "wangjing-topic", //topic:和消费者发送的topic相同 consumerGroup = "wjConsumerGroup") //group:不用和生产者group相同public class MqConsumer implements RocketMQListener
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!