Redis的发布订阅模式
介绍
Redis是一款基于内存的非关系型数据库,提供了多种数据结构存储数据,存取数据的速度还很快,除了这些优点,它还提供了其他特色功能,比如:管道、发布订阅模式。
本文主要描述发布订阅模式的使用。
发布订阅模式
发布订阅是一种消息通信模式。
发布者发送消息到频道(channel),订阅者接受频道的消息
其中发送者和订阅者都是客户端,频道维护在服务端
Redis提供订阅频道、模式两种方式,模式可以理解为匹配正则的频道(比如 act.create匹配act.*模式)
使用命令
subscribe channel ...
订阅一个或多个频道unsubscribe channel ...
退订一个或多个频道psubscribe pattern ...
订阅一个或多个符合模式的频道publish channel message
将消息发送到指定频道pubsub subcommand argument...
查看订阅与发布系统状态punsubscribe pattern...
退订所有给定模式的频道- 以下是使用示例
#testchannel为频道 hello!caicai为消息 #客户端A发送消息 返回0 表示0个客户端接收 此时还没订阅频道 127.0.0.1:6379> publish testchannel hello!caicai (integer) 0 #客户端B订阅testchannel频道 127.0.0.1:6379> subscribe testchannel Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "testchannel" 3) (integer) 1 #客户端A发送消息 返回1 说明1个客户端接收到 127.0.0.1:6379> publish testchannel hello!caicai (integer) 1 #客户端B收到消息 1) "message" 2) "testchannel" 3) "hello!caicai" #客户端C使用模式进行订阅 订阅符合test*模式的频道(*为通配) testchannel符合test* 127.0.0.1:6379> psubscribe test* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "test*" 3) (integer) 1 #客户端A发送消息 返回2 说明2个客户端接收到 127.0.0.1:6379> publish testchannel hello!caicai2 (integer) 2 #客户端B显示 1) "message" 2) "testchannel" 3) "hello!caicai2" #客户端C显示 1) "pmessage" 2) "test*" 3) "testchannel" 4) "hello!caicai2"
原理
subscribe channel
订阅频道维护字典,Key为频道,Value为链表,节点为订阅频道的客户端。客户端多月或退订频道则是在字典中添加/删除。
psubscribe pattern
订阅模式维护链表,节点为模式名与客户端。客户端订阅或退订模式则是在链表上遍历添加/删除。
publish
发送消息时,先找到字典中的Key频道遍历链表发送消息,再去模式中的链表上遍历是否与节点上的模式名匹配,匹配啧发送消息给对应客户端。
通过发布订阅模型能够实现订阅、通知系统,哨兵模式中也使用发布订阅模式,哨兵订阅主节点,主节点收到某个哨兵命令后发送返回信息,各个哨兵收到消息后能够感知其他哨兵的存在。