Redis 教程 | 菜鸟教程  
阿里云**** 

Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。
Redis 客户端可以订阅任意数量的频道。

订阅者(subscriber):如客户端
发布者(publisher):如服务器
频道(channel):频道

通信模型
● RedisServer中可以创建若干channel
● 一个订阅者可以订阅多个channel
● 当发布者向一个频道中发布一条消息时,所有的订阅者都将会收到消息
● Redis的发布订阅模型没有消息积压功能,即新加入的订阅者收不到发布者之前发布的消息
● 当订阅者收到消息时,消息内容如下
   ● 第一行:固定内容message
   ● 第二行:channel的名称
   ● 第三行:收到的新消息

发布订阅的 API
Redis Pub/Sub模式 - 发布/订阅模式
Publish:发布消息
      PUBLISH channel message  将信息发送到指定的channel(频道)
Subscribe:订阅消息
      SUBSCRIBE channel [channel ...]  订阅一个或多个频道
UnSubscribe:取消订阅消息
      UNSUBSCRIBE channel [channel ...] 取消订阅一个或多个频道

Redis 模式订阅 PSUBSCRIBE/PUNSUBSCRIBE
● PSUBSCRIBE pattern [pattern ...]  订阅一个或多个符合pattern给定模式的频道
● PUNSUBSCRIBE pattern [pattern ...]  取消订阅一个或多个频道
● PUBSUB subcommand [argument [argument ...]]  查看订阅与发布系统状态

支持的模式有:?,*,[abc],[^abc],[a-b]
● h?llo matches hello,hallo and hxllo                         匹配包含 h 与 llo 之间,有“一个”任意字符 的频道
● h*llo matches hllo and heeeello                                匹配包含 h 与 llo 之间,有任意“多个”字符 的频道
● h[ae]llo matches hello and hallo,but not hillo         匹配包含 h 与 llo 之间,是a 或者 e 的频道
● h[^e]llo matches hallo,hbllo,... but not hello         匹配包含 h 与 llo 之间,有一个字母并且不能是 e 的频道
● h[a-b]llo matches hallo and hbllo                              匹配包含 h 与 llo 之间,a 到 b 之间的频道

查询订阅状态
PUBSUB CHANNELS [pattern]:返回符合模式(未指定情况下返回所有)的频道
PUBSUB NUMSUB [channel - 1 ... channel - N]:返回指定的频道的订阅数
PUBSUB NUMPAT:返回所有服务器中被订阅的模式数目
*
*
*
*

相关文章:

  • 2022-12-23
  • 2021-07-18
  • 2022-12-23
  • 2021-09-05
  • 2021-08-27
  • 2021-09-18
  • 2021-09-01
  • 2021-05-01
猜你喜欢
  • 2021-09-26
  • 2022-12-23
  • 2021-12-18
  • 2021-05-06
  • 2021-12-16
  • 2021-09-01
  • 2021-10-05
相关资源
相似解决方案