Redis是一个支持消息队列功能的内存数据库,可以作为消息队列系统使用。
消息队列(Message Queue),字面意思就是存放消息的队列。而Redis的list数据结构是一个双向链表,很容易模拟出队列效果。队列是入口和出口不在一边,因此我们可以利用:LPUSH 结合 RPOP、或者 RPUSH 结合 LPOP来实现。 不过要注意的是,当队列中没有消息时RPOP或LPOP操作会返回null,并不像JVM的阻塞队列那样会阻塞并等待消息。因此这里应该使用BRPOP或者BLPOP来实现阻塞效果。
架构如下:
cn.hutool
hutool-all
5.8.10
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-aop
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=10s
spring.redis.password=123
消费者注解
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
/**
* 消费者注解
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MessageConsumer {
@AliasFor(annotation = Component.class)
String value() default "";
}
处理器注解
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
/**
* 处理器注解,不同的类型使用不同的注解标准
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MessageHandler {
MessageListener.Mode value() default MessageListener.Mode.TOPIC;
}
监听注解
import java.lang.annotation.*;
/**
* 监听注解
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MessageListener {
String value() default "";
String topic() default "";
String channel() default "";
String streamKey() default "";
/**
* group-name是关联到流的消费者组的名称。
*/
String consumerGroup() default "";
/**
* consumer-name是客户端用于在消费者组内标识自己的字符串
*/
String consumerName() default "";
/**
* 读取未ack
*
*/
boolean pending() default false;
Mode mode() default Mode.TOPIC;
enum Mode {
/**
* topic 模式,主题订阅
*/
TOPIC(),
/**
* pub/sub 模式 订阅发布 广播
*/
PUBSUB(),
/**
* stream 模式
*/
STREAM()
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
/**
* 消费者
*/
@MessageConsumer
@Slf4j
public class RedisMqConsumer {
@Resource
RedisTemplate
处理接口
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;
/**
* 处理接口
*/
@Slf4j
public abstract class AbstractMessageHandler implements ApplicationContextAware {
// 用于获取应用程序上下文的成员变量。
protected ApplicationContext applicationContext;
protected RedisTemplate
工作队列
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* topic处理方式 实现 工作队列
* 基于 list 的 lPush/brPop
*/
@MessageHandler(value = MessageListener.Mode.TOPIC)
public class TopicMessageHandler extends AbstractMessageHandler {
public TopicMessageHandler(RedisTemplate redisTemplate) {
super(redisTemplate);
}
@Override
public void invokeMessage(Method method) {
Set consumers = new HashSet<>();
// 获取了MessageListener注解的相关信息
MessageListener annotation = method.getAnnotation(MessageListener.class);
// 主题(topic)
String topic = getTopic(annotation);
// 获取到一个RedisConnection对象
RedisConnection connection = getConnection();
// 通过反射获取到方法所在的类和方法所在的Bean对象。
Class> declaringClass = method.getDeclaringClass();
Object bean = applicationContext.getBean(declaringClass);
/**
* 通过调用connection.bRPop(1, topic.getBytes())方法阻塞地等待从Redis的列表中获取消息。
* 方法参数中的1表示阻塞时间为1秒,topic.getBytes()将主题转换为字节数组形式。
*/
while (true) {
List bytes = connection.bRPop(1, topic.getBytes());
if (CollectionUtil.isNotEmpty(bytes)) {
if (null != bytes.get(1)) {
consumer(method, consumers, bean, bytes.get(1));
}
}
}
}
private String getTopic(MessageListener annotation) {
// 获取注解中的 value 属性值
String value = annotation.value();
// 获取注解中的 topic 属性值
String topic = annotation.topic();
// 如果 topic 为空,则使用 value,否则使用 topic
return StrUtil.isBlank(topic) ? value : topic;
}
}
import cn.hutool.core.util.ArrayUtil;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 启动配置,在项目启动时自动运行,主要用于消费者的注册
*/
@Component
@Slf4j
public class MessageConsumerStater implements ApplicationRunner, ApplicationContextAware {
private ApplicationContext applicationContext;
/**
* 项目启动时,获取所有的 标注 MessageConsumer 注解的方法 ,开启消息监听逻辑
*
* @param args ApplicationArguments
*/
@Override
public void run(ApplicationArguments args) {
// 通过 getInvokers() 方法获取所有的 AbstractMessageHandler 实例
Map invokers = getInvokers();
/**
* 使用 applicationContext.getBeansWithAnnotation(MessageConsumer.class) 获取所有标注了 MessageConsumer 注解的 Bean,
* 并使用 values() 方法获取这些 Bean 的集合。
*/
applicationContext.getBeansWithAnnotation(MessageConsumer.class).values().parallelStream().forEach(consumer -> {
/**
* 通过 consumer.getClass().getMethods() 获取该 Bean 类的所有方法,并存储在 methods 数组中。
* 如果 methods 数组非空,就通过并行流遍历这些方法,并对每个方法执行以下操作:
* 调用 startMessageListener(method, invokers) 方法,传入该方法和之前获取的消息处理器映射对象 invokers。这个方法用于启动消息监听逻辑。
*/
Method[] methods = consumer.getClass().getMethods();
if (ArrayUtil.isNotEmpty(methods)) {
Arrays.stream(methods).parallel().forEach(method -> startMessageListener(method, invokers));
}
});
}
/**
* 找到对应的处理方式来处理消息的消费逻辑
*
* @param method 消费者方法 -> 本案例对应的是 MqConsumer 中的方法
* @param handlerMap 所有的处理方式集合
*/
private void startMessageListener(Method method, Map handlerMap) {
MessageListener listener = method.getAnnotation(MessageListener.class);
if (null == listener) {
return;
}
MessageListener.Mode mode = listener.mode();
AbstractMessageHandler handler = handlerMap.get(mode);
if (handler == null) {
log.error("invoker is null");
return;
}
handler.invokeMessage(method);
}
private Map getInvokers() {
// 通过调用 getBeansWithAnnotation() 方法从应用程序上下文中获取所有带有 MessageHandler 注解的 Bean
Map beansWithAnnotation = applicationContext.getBeansWithAnnotation(MessageHandler.class);
/**
* 使用 .collect(Collectors.toMap(k -> k.getClass().getAnnotation(MessageHandler.class).value(), k -> (AbstractMessageHandler) k)) 进行收集操作。具体来说:
* k -> k.getClass().getAnnotation(MessageHandler.class).value() 是用于获取每个 Bean 的 MessageHandler 注解上的 value 属性值作为 Map 的键。
* k -> (AbstractMessageHandler) k 是将 Bean 强制转换为 AbstractMessageHandler 类型后作为 Map 的值。
*/
Map collect =
beansWithAnnotation.values().stream().collect(Collectors
.toMap(k -> k.getClass().getAnnotation(MessageHandler.class).value(), k -> (AbstractMessageHandler) k));
return collect;
}
public MessageConsumerStater() {
}
@Override
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.time.Duration;
@Configuration
public class RedisConfig{
@Bean
@SuppressWarnings("all")
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
RedisSerializer redisSerializer = RedisSerializer.string();
redisTemplate.setValueSerializer(redisSerializer);
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
redisTemplate.setDefaultSerializer(redisSerializer);
redisTemplate.setStringSerializer(redisSerializer);
return redisTemplate;
}
@Bean
public StreamMessageListenerContainer> redisListenerContainer(ThreadPoolTaskExecutor executor, RedisTemplate redisTemplate) {
StreamMessageListenerContainer.StreamMessageListenerContainerOptions> options =
StreamMessageListenerContainer.StreamMessageListenerContainerOptions
.builder()
// 拉取消息超时时间
.pollTimeout(Duration.ofSeconds(5))
// 批量抓取消息
.batchSize(1)
// 传递的数据类型
.targetType(String.class)
.executor(executor)
.build();
RedisConnectionFactory connectionFactory = redisTemplate.getConnectionFactory();
assert connectionFactory != null;
StreamMessageListenerContainer> container = StreamMessageListenerContainer
.create(connectionFactory, options);
container.start();
return container;
}
}
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 消息实体
*
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Message implements Serializable {
private String id;
private T content;
}
生产者
import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.UUID;
@RestController
@RequestMapping("/test")
public class ProductController {
@Resource
RedisTemplate redisTemplate;
@GetMapping("/listMessage")
public void listMessage() {
Message message = new Message<>();
// 随机生成id
message.setId(UUID.randomUUID().toString());
// 消息内容
message.setContent("0001");
redisTemplate.opsForList().leftPush("topic1", JSONUtil.toJsonStr(message));
}
}
获取消息成功,工作者1获取消息。
再次发送消息, 工作者2获取消息,这个结果表明只能有其中一个工作者去消费消息。
优点:
利用Redis存储,不受限于JVM内存上限
基于Redis的持久化机制,数据安全性有保证
可以满足消息有序性
缺点:
无法避免消息丢失
只支持单消费者
PubSub(发布订阅)是Redis2.0版本引入的消息传递模型。顾名思义,消费者可以订阅一个或多个channel,生产者向对应channel发送消息后,所有订阅者都能收到相关消息。
在原有的基础上实现。
架构如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
/**
* 消费者
*/
@MessageConsumer
@Slf4j
public class RedisMqConsumer {
@Resource
RedisTemplate redisTemplate;
@MessageListener(topic = "topic1", mode = MessageListener.Mode.TOPIC)
public void testTopic1(Message> message) {
log.info("工作者1===> " + message);
}
@MessageListener(topic = "topic1", mode = MessageListener.Mode.TOPIC)
public void testTopic11(Message> message) {
log.info("工作者2===> " + message);
}
@MessageListener(topic = "topic2", mode = MessageListener.Mode.TOPIC)
public void testTopic2(Message> message) {
log.info("topic2===> " + message);
}
@MessageListener(topic = "topic3", mode = MessageListener.Mode.TOPIC)
public void testTopic3(Message> message) {
log.info("topic3===> " + message);
}
@MessageListener(channel = "pubsub", mode = MessageListener.Mode.PUBSUB)
public void testPubsub1(Message> message) {
log.info("pubsub1===> " + message);
}
@MessageListener(channel = "pubsub", mode = MessageListener.Mode.PUBSUB)
public void testPubsub2(Message> message) {
log.info("pubsub2===> " + message);
}
}
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import cn.hutool.core.util.StrUtil;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
/**
* Pub/sub 处理方式 实现 广播消息
*/
@MessageHandler(value = MessageListener.Mode.PUBSUB)
public class PubsubMessageHandler extends AbstractMessageHandler {
public PubsubMessageHandler(RedisTemplate redisTemplate) {
super(redisTemplate);
}
@Override
public void invokeMessage(Method method) {
Set consumers = new HashSet<>();
MessageListener listener = method.getAnnotation(MessageListener.class);
String channel = getChannel(listener);
RedisConnection connection = getConnection();
/**
* 模式匹配订阅
*/
if (listener.channel().contains("*")) {
connection.pSubscribe((message, pattern) -> {
Class> declaringClass = method.getDeclaringClass();
Object bean = applicationContext.getBean(declaringClass);
byte[] body = message.getBody();
consumer(method, consumers, bean, body);
}, channel.getBytes());
}
/**
* 普通订阅
*/
else {
connection.subscribe((message, pattern) -> {
Class> declaringClass = method.getDeclaringClass();
Object bean = applicationContext.getBean(declaringClass);
byte[] body = message.getBody();
consumer(method, consumers, bean, body);
}, channel.getBytes());
}
}
private String getChannel(MessageListener annotation) {
String value = annotation.value();
String channel = annotation.channel();
return StrUtil.isBlank(channel) ? value : channel;
}
}
import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.UUID;
@RestController
@RequestMapping("/test")
public class ProductController {
@Resource
RedisTemplate redisTemplate;
@GetMapping("/listMessage")
public void listMessage() {
Message message = new Message<>();
// 随机生成id
message.setId(UUID.randomUUID().toString());
// 消息内容
message.setContent("0001");
redisTemplate.convertAndSend("pubsub", JSONUtil.toJsonStr(message));
// redisTemplate.opsForList().leftPush("topic1", JSONUtil.toJsonStr(message));
}
}
发送普通消息,消费者1和消费者2都接收到信息了
发送模式匹配消息,只有消费者2接收到信息。
优点:
采用发布订阅模型,支持多生产、多消费
缺点:
不支持数据持久化
无法避免消息丢失
消息堆积有上限,超出时数据丢失
Stream 是 Redis 5.0 引入的一种新数据类型,可以实现一个功能非常完善的消息队列。
架构如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
/**
* 消费者
*/
@MessageConsumer
@Slf4j
public class RedisMqConsumer {
@Resource
RedisTemplate redisTemplate;
@MessageListener(topic = "topic1", mode = MessageListener.Mode.TOPIC)
public void testTopic1(Message> message) {
log.info("工作者1===> " + message);
}
@MessageListener(topic = "topic1", mode = MessageListener.Mode.TOPIC)
public void testTopic11(Message> message) {
log.info("工作者2===> " + message);
}
@MessageListener(topic = "topic2", mode = MessageListener.Mode.TOPIC)
public void testTopic2(Message> message) {
log.info("topic2===> " + message);
}
@MessageListener(topic = "topic3", mode = MessageListener.Mode.TOPIC)
public void testTopic3(Message> message) {
log.info("topic3===> " + message);
}
@MessageListener(channel = "pubsub.test", mode = MessageListener.Mode.PUBSUB)
public void testPubsub1(Message> message) {
log.info("pubsub1===> " + message);
}
@MessageListener(channel = "pubsub.*", mode = MessageListener.Mode.PUBSUB)
public void testPubsub2(Message> message) {
log.info("pubsub2===> " + message);
}
@MessageListener(streamKey = "streamKey",mode = MessageListener.Mode.STREAM)
public void testStream1(ObjectRecord message) {
log.info("testStream消费消息 ===> " + message);
// 只获取最新一条
redisTemplate.opsForStream().trim("streamKey",1);
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Stream方式 实现消息队列
*/
@MessageHandler(value = MessageListener.Mode.STREAM)
@Slf4j
public class StreamMessageHandler extends AbstractMessageHandler {
@Resource
StreamMessageListenerContainer> redisListenerContainer;
public StreamMessageHandler(RedisTemplate redisTemplate) {
super(redisTemplate);
}
@Override
public synchronized void invokeMessage(Method method) {
// 获取注解中的相关信息
MessageListener annotation = method.getAnnotation(MessageListener.class);
String streamKey = annotation.streamKey();
StreamMessageListenerContainer.StreamReadRequest streamReadRequest =
StreamMessageListenerContainer.StreamReadRequest.builder(
StreamOffset.create(streamKey, ReadOffset.lastConsumed()))
.errorHandler((error) -> log.error("redis stream 异常 :{}", error.getMessage()))
.cancelOnError(e -> false)
.build();
// 指定消费者对象注册到容器中去
redisListenerContainer.register(streamReadRequest, (message) -> {
Class> declaringClass = method.getDeclaringClass();
Object bean = applicationContext.getBean(declaringClass);
try {
method.invoke(bean, message);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
});
}
}
import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.UUID;
@RestController
@RequestMapping("/test")
public class ProductController {
@Resource
RedisTemplate redisTemplate;
@GetMapping("/listMessage")
public void listMessage() {
Message message = new Message<>();
// 随机生成id
message.setId(UUID.randomUUID().toString());
// 消息内容
message.setContent("0001");
redisTemplate.convertAndSend("pubsub.122", JSONUtil.toJsonStr(message));
// redisTemplate.opsForList().leftPush("topic1", JSONUtil.toJsonStr(message));
}
@GetMapping("/test-stream")
public String stream() {
Message message = new Message<>();
message.setId(UUID.randomUUID().toString());
message.setContent("0001");
ObjectRecord record = ObjectRecord.create("streamKey", JSONUtil.toJsonStr(message));
// 将消息添加至消息队列中
RecordId recordId = redisTemplate.opsForStream().add(record);
return recordId == null ? "null" : recordId.getValue();
}
}
发送消息,消费者获取消息成功
STREAM类型消息队列的XREAD命令特点:
消息可回溯
一个消息可以被多个消费者读取
可以阻塞读取
有消息漏读的风险
消费者组(Consumer Group):将多个消费者划分到一个组中,监听同一个队列。具备下列特点:
架构如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
/**
* 消费者
*/
@MessageConsumer
@Slf4j
public class RedisMqConsumer {
@Resource
RedisTemplate redisTemplate;
@MessageListener(topic = "topic1", mode = MessageListener.Mode.TOPIC)
public void testTopic1(Message> message) {
log.info("工作者1===> " + message);
}
@MessageListener(topic = "topic1", mode = MessageListener.Mode.TOPIC)
public void testTopic11(Message> message) {
log.info("工作者2===> " + message);
}
@MessageListener(topic = "topic2", mode = MessageListener.Mode.TOPIC)
public void testTopic2(Message> message) {
log.info("topic2===> " + message);
}
@MessageListener(topic = "topic3", mode = MessageListener.Mode.TOPIC)
public void testTopic3(Message> message) {
log.info("topic3===> " + message);
}
@MessageListener(channel = "pubsub.test", mode = MessageListener.Mode.PUBSUB)
public void testPubsub1(Message> message) {
log.info("pubsub1===> " + message);
}
@MessageListener(channel = "pubsub.*", mode = MessageListener.Mode.PUBSUB)
public void testPubsub2(Message> message) {
log.info("pubsub2===> " + message);
}
@MessageListener(streamKey = "streamKey", consumerGroup = "consumerGroup", consumerName = "consumerName", mode = MessageListener.Mode.STREAM)
public void testStream1(ObjectRecord message) {
log.info("testStream1 组 consumerGroup 名 consumerName 消费消息 ===> " + message);
// 手动 ack
redisTemplate.opsForStream().acknowledge("consumerGroup", message);
}
/**
* 同一个组,不同的 consumerName 都可以进行消息的消费,与上面 testStream() 为竞争关系,消息仅被其中一个消费
*
* @param message msg
*/
@MessageListener(streamKey = "streamKey", consumerGroup = "consumerGroup", consumerName = "consumerName2", mode = MessageListener.Mode.STREAM)
public void testStream2(ObjectRecord message) {
log.info("testStream2 组 consumerGroup 名 consumerName2 消费消息 ===> " + message);
// redisTemplate.opsForStream().acknowledge("consumerGroup", message);
}
/**
* 不同组的消息,可以正常消费
*
* @param message message
*/
@MessageListener(streamKey = "streamKey", consumerGroup = "consumerGroup3", consumerName = "consumerName3", mode = MessageListener.Mode.STREAM)
public void testStream3(ObjectRecord message) {
log.info("testStream3 组 consumerGroup3 名 consumerName3 消费消息 ===> " + message);
}
/**
* 消费 streamKey 中,没有进行 ack 的消息
*
* @param message message
*/
@MessageListener(
streamKey = "streamKey",
consumerGroup = "consumerGroup",
consumerName = "consumerName",
pending = true,
mode = MessageListener.Mode.STREAM)
public void testStreamPending(ObjectRecord message) {
log.info("testStreamPending 组 consumerGroup 名 consumerName 消费未 ack 消息 ===> " + message);
redisTemplate.opsForStream().acknowledge("consumerGroup", message);
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.stream.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.data.redis.stream.StreamMessageListenerContainer;
import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Stream方式 实现消息队列
*/
@MessageHandler(value = MessageListener.Mode.STREAM)
@Slf4j
public class StreamMessageHandler extends AbstractMessageHandler {
@Resource
StreamMessageListenerContainer> redisListenerContainer;
public StreamMessageHandler(RedisTemplate redisTemplate) {
super(redisTemplate);
}
@Override
public synchronized void invokeMessage(Method method) {
// 获取注解中的相关信息
MessageListener annotation = method.getAnnotation(MessageListener.class);
String streamKey = annotation.streamKey();
String consumerGroup = annotation.consumerGroup();
String consumerName = annotation.consumerName();
boolean pending = annotation.pending();
// 检测组是否存在
this.checkAndCreatGroup(streamKey, consumerGroup);
// 获取 StreamOffset
StreamOffset offset = this.getOffset(streamKey, pending);
// 创建消费者
Consumer consumer = Consumer.from(consumerGroup, consumerName);
StreamMessageListenerContainer.StreamReadRequest streamReadRequest =
StreamMessageListenerContainer
.StreamReadRequest.builder(offset)
.errorHandler((error) -> log.error("redis stream 异常 :{}", error.getMessage()))
.cancelOnError(e -> false)
.consumer(consumer)
//关闭自动ack确认
.autoAcknowledge(false)
.build();
// 指定消费者对象注册到容器中去
redisListenerContainer.register(streamReadRequest, (message) -> {
Class> declaringClass = method.getDeclaringClass();
Object bean = applicationContext.getBean(declaringClass);
try {
method.invoke(bean, message);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
});
}
private StreamOffset getOffset(String streamKey, boolean pending) {
if (pending) {
// 获取尚未 ack 的消息
return StreamOffset.create(streamKey, ReadOffset.from("0"));
}
// 指定消费最新的消息
return StreamOffset.create(streamKey, ReadOffset.lastConsumed());
}
/**
* 校验消费组是否存在,不存在则创建,否则会出现异常
* Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: NOGROUP No such key 'streamKey' or consumer group 'consumerGroup' in XREADGROUP with GROUP option
*
* @param streamKey streamKey
* @param consumerGroup consumerGroup
*/
private void checkAndCreatGroup(String streamKey, String consumerGroup) {
if (Boolean.TRUE.equals(redisTemplate.hasKey(streamKey))) {
StreamOperations streamOperations = redisTemplate.opsForStream();
StreamInfo.XInfoGroups groups = streamOperations.groups(streamKey);
if (groups.isEmpty() || groups.stream().noneMatch(data -> consumerGroup.equals(data.groupName()))) {
creatGroup(streamKey, consumerGroup);
} else {
groups.stream().forEach(g -> {
log.info("XInfoGroups:{}", g);
StreamInfo.XInfoConsumers consumers = streamOperations.consumers(streamKey, g.groupName());
log.info("XInfoConsumers:{}", consumers);
});
}
} else {
creatGroup(streamKey, consumerGroup);
}
}
private void creatGroup(String key, String group) {
StreamOperations streamOperations = redisTemplate.opsForStream();
String groupName = streamOperations.createGroup(key, group);
log.info("创建组成功:{}", groupName);
}
}
import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.UUID;
@RestController
@RequestMapping("/test")
public class ProductController {
@Resource
RedisTemplate redisTemplate;
@GetMapping("/listMessage")
public void listMessage() {
Message message = new Message<>();
// 随机生成id
message.setId(UUID.randomUUID().toString());
// 消息内容
message.setContent("0001");
redisTemplate.convertAndSend("pubsub.122", JSONUtil.toJsonStr(message));
// redisTemplate.opsForList().leftPush("topic1", JSONUtil.toJsonStr(message));
}
@GetMapping("/test-stream")
public String stream() {
Message message = new Message<>();
message.setId(UUID.randomUUID().toString());
message.setContent("0001");
ObjectRecord record = ObjectRecord.create("streamKey", JSONUtil.toJsonStr(message));
// 将消息添加至消息队列中
RecordId recordId = redisTemplate.opsForStream().add(record);
return recordId == null ? "null" : recordId.getValue();
}
}
生产者发送消息,消费组1和消费者2获取到消息,并指派一个消费者去消费
STREAM类型消息队列的XREADGROUP命令特点: