目录
前言
RocketMQ官网:Quick Start - Apache RocketMQ
下载安装:windows下RocketMQ下载安装教程 - darendu - 博客园
分布式事务消息:SpringCloud Alibaba微服务实战三十二 - 集成RocketMQ实现分布式事务_飘渺Jam的博客-CSDN博客
MQ选型:关于ActiveMQ、RocketMQ、RabbitMQ、Kafka一些总结和区别 - Alano的自嘲 - 博客园
异步消息
1、引入依赖
<!--rocketMQ-->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>
2、异步消息与异步延迟消息生产者工具类
package com.szzz.catering.order.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
/**
* 消息生产者
*
* @author: cyl
* @date: 2021-12-08 09:45
*/
@Component
@Slf4j
public class RocketMQProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 发送异步消息
* @param topic
* @param tag
* @param msg
*/
public <T> void sendAsyncMsg (String topic, String tag, T msg){
rocketMQTemplate.asyncSend(topic + ":" + tag,
msg
, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
log.info("发送成功的Topic={},tag={},内容={}", topic ,tag, msg);
}
@Override
public void onException(Throwable throwable) {
log.info("发送失败的Topic={},tag={},内容={}", topic ,tag, msg);
}
});
}
/**
* 发送延时消息
* @param topic
* @param tag
* @param msg
* Level:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
* @param delayLevel messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
*/
public <T> void sendAsyncDelayMsg(String topic, String tag, T msg, int delayLevel){
Message<T> message= MessageBuilder.withPayload(msg).build();
log.info("发送的Topic={},tag={},内容={}", topic ,tag, msg);
rocketMQTemplate.asyncSend(topic + ":" + tag, message, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
log.info("发送延时消息成功的Topic={},tag={},内容={}", topic ,tag, msg);
}
@Override
public void onException(Throwable throwable) {
log.info("发送延时消息失败的Topic={},tag={},内容={}", topic ,tag, msg);
}
},3000L,delayLevel);
}
/**延时30分钟消费*/
public final static int THIRTY_MINUTES_DELAY_LEVEL=16;
}
3、消息消费者
package com.szzz.catering.order.listener.mq;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息监听器
*
* @author: cyl
* @date: 2021-12-08 15:40
*/
@Component
@RocketMQMessageListener(
topic = "${mq.topic}",
selectorExpression = "tag-to-print",
consumerGroup = "${mq.consumer-group}")
@Slf4j
public class PrintListener implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
log.info("消费了一条消息>>>>>>>>>>>>>>>>>:{}",message);
}
}
4、调用
rocketMQProducer.sendAsyncMsg(mqProperties.getTopic(), MQContants.TAG_TO_PRINT, p);
此时配置文件yaml需配置mqProperties所需要的TOPIC和消费组;
MQContants常量中定义不同的TAG;
5、注意事项
1、测试环境与预发布生成环境需要监听不同的topic时,将topic放入配置文件中,并使用topic = "${mq.topic}"监听;
2、若是监听实体类,注意将实体类序列化;
implements Serializable { private static final long serialVersionUID = 1L;
3、Topic表示一类消息的集合,每个主题包含若干个消息,每个消息只属于一个主题,是RocketMQ进行消息订阅的基本单位。topic:message=1:n; message:topic=1:1
4、一个生产者可以同时发送多种topic消息,而一个消费者只对某种特定的Topic感兴趣,即只可以订阅和消费一种Topic的消息。producer:topic=1:n; consumer:topic=1:1