SpringBoot整合RocketMQ
文章目录
- SpringBoot整合RocketMQ
- 下载安装
- SpringBoot整合RocketMQ
- 导坐标
- 改配置
- 实现消息生产与消费
下载安装
教程地址:https://www.bilibili.com/video/BV15b4y1a7yG/?p=132&spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=f6debc5a79e3f424f9dde2f13891b158
下载地址:
https://rocketmq.apache.org/download
先启动命名服务器
启动后可以测试是否启动成功
SpringBoot整合RocketMQ
导坐标
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.3</version>
</dependency>
改配置
rocketmq:
name-server: localhost:9876
producer:
group: group_rocketmq
实现消息生产与消费
普通的生产消息
异步的生产消息
@Service
public class MessageServiceRocketmqImpl implements MessageService {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Override
public void sendMessage(String id) {
System.out.println("待发送短信的订单已纳入处理队列 (roeket topic),id:" + id);
// rocketMQTemplate.convertAndSend("order_id",id); // 同步消息
SendCallback callback = new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println("消息发送成功");
}
@Override
public void onException(Throwable throwable) {
System.out.println("消息发送失败");
}
};
rocketMQTemplate.asyncSend("order_id",id,callback); // 异步消息
}
@Override
public String doMessage() {
return null;
}
}
消费消息就是用监听
@Component
@RocketMQMessageListener(topic = "order_id",consumerGroup = "group_rocketmq")
public class MessageListener implements RocketMQListener<String> {
@Override
public void onMessage(String id) {
System.out.println("已完成短信发送业务(rocketmq):id:" + id);
}
}