目录
-
- [一、Redis Pub/Sub](#一、Redis Pub/Sub)
- [二、Redisson Topic](#二、Redisson Topic)
一、Redis Pub/Sub
Redis Pub/Sub实现了发布/订阅消息传递范式,发送端和订阅端通过channel进行关联。订阅端订阅一个或多个channel,而发送端向指定的channel发送消息。
Redis的Pub/Sub仅支持最多一次at-most-once
消息传递语义,Redis Pub/Sub采取的是发送即忘(fire and forget)策略,这意味着一条消息将被传递一次(如果有的话),一旦Redis服务器发送了消息,就没有机会再次发送。如果订阅者无法处理消息(例如,出错、断线、网络断开后重新连接),则客户端断开时传递的所有消息都将丢失。同时Redis Sub无法在多客户端间负载均衡(即所有客户端都会收到同一条Redis消息,需要应用做去重(幂等)、分片处理等)。如果需要更强的交付保证,参见Redis Streams,Stream中的消息是持久化的,并且支持最多一次at-most-once
和至少一次at-least-once
传递语义。
Redis Pub/Sub具体命令示例如下:
bash
# 订阅频道
SUBSCRIBE myChannel1 myChannel2
# 发送消息
PUBLISH myChannel1 myMessage
# 取消订阅
UNSUBSCRIBE myChannel1 myChannel2
Redis Pub/Sub支持Glob-style模式匹配的订阅,具体命令示例如下:
bash
# 订阅频道
PSUBSCRIBE myChannel.*
# 发送消息
PUBLISH myChannel.weather myMessage
# 取消订阅
PUNSUBSCRIBE myChannel.*
Redis Pub/Sub支持的统计命令示例如下:
bash
# 查询所有活动频道
PUBSUB CHANNELS
# 查询所有匹配模式的活动频道
PUBSUB CHANNELS myChannel.*
# 查询所有订阅者数量(支持SUBSCRIBE命令)
PUBSUB NUMSUB
# 查询所有订阅的模式数(调用PSUBSCRIBE的不重复模式数)
PUBSUB NUMPAT
二、Redisson Topic
在Redisson中可通过Topic实现Redis Pub/Sub,示例代码如下:
注:
如下topicName即对应channel名称,
MyMessage为自定义POJO对象(包括属性和getter/setter)
java
@Resource
private RedissonClient redisson;
@Test
void testTopic() throws InterruptedException {
String topicName = "myTopic";
MyMessage myMessage = this.buildMyMessage();
//获取Topic
RTopic topic = this.redisson.getTopic(topicName);
//订阅消息
topic.addListener(MyMessage.class, (channel, msg) -> {
log.info("topic[{}] receive message: {}", channel, msg);
});
//发布消息
long receiveClientCount = topic.publish(myMessage);
log.info("topic[{}] publish success, receiveClientCount: {}", topicName, receiveClientCount);
this.sleep5Secs();
}
在Redisson中可通过PatternTopic实现Redis Pub/Sub模式匹配的订阅,示例代码如下:
java
@Resource
private RedissonClient redisson;
@Test
void testPatternTopic() throws InterruptedException {
String topicName = "myTopic";
String topicPattern = "my*";
MyMessage myMessage = this.buildMyMessage();
//获取PatternTopic
RPatternTopic patternTopic = this.redisson.getPatternTopic(topicPattern);
//订阅消息
patternTopic.addListener(MyMessage.class, (pattern, channel, msg) -> {
log.info("PatternTopic[pattern={}, channel={}] receive message: {}", pattern, channel, msg);
});
//发布消息
RTopic topic = this.distributedObjectService.getTopic(topicName);
long receiveClientCount = topic.publish(myMessage);
log.info("topic[{}] publish success, receiveClientCount: {}", topicName, receiveClientCount);
this.sleep5Secs();
}
参考:
Redis pub/sub
https://redis.io/docs/latest/commands/subscribe/
https://redis.io/docs/latest/develop/interact/pubsub/
https://redis.io/docs/latest/develop/data-types/streams/
Redisson Topic
https://github.com/redisson/redisson/wiki/6.-distributed-objects#67-topic