|NO.Z.00025|——————————|BigDataEnd|——|Hadoop&Redis.V01|——|Redis.v01|发布订阅机制|

一、Redis发布订阅机制
### --- 发布于订阅

~~~     Redis提供了发布订阅功能,可以用于消息的传输
~~~     Redis的发布订阅机制包括三个部分,publisher,subscriber和Channel
~~~     发布者和订阅者都是Redis客户端,Channel则为Redis服务器端。
~~~     发布者将消息发送到某个的频道,订阅了这个频道的订阅者就能接收到这条消息。
二、频道/模式的订阅与退订
### --- subscribe:订阅 subscribe channel1 channel2 ..
~~~     Redis客户端1订阅频道1和频道2

127.0.0.1:6379> subscribe ch1 ch2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "ch1"
3) (integer) 1
1) "subscribe"
2) "ch2"
3) (integer) 2
127.0.0.1:6379> publish ch1 hello
(integer) 1
127.0.0.1:6379> publish ch2 world
(integer) 1publish:发布消息 publish channel message
Redis客户端2将消息发布在频道1和频道2127.0.0.1:6379> publish ch1 hello
(integer) 1
127.0.0.1:6379> publish ch2 world
(integer) 1
### --- Redis客户端1接收到频道1和频道2的消息

1) "message"
2) "ch1"
3) "hello"
1) "message"
2) "ch2"
3) "world"
### --- unsubscribe:退订 channel
~~~     Redis客户端1退订频道1

127.0.0.1:6379> unsubscribe ch1
1) "unsubscribe"
2) "ch1"
3) (integer) 0
### --- psubscribe :模式匹配 psubscribe +模式
~~~     Redis客户端1订阅所有以ch开头的频道

127.0.0.1:6379> psubscribe ch*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "ch*"
3) (integer) 1
### --- Redis客户端2发布信息在频道5上    

127.0.0.1:6379> publish ch5 helloworld
(integer) 1
### --- Redis客户端1收到频道5的信息

1) "pmessage"
2) "ch*"
3) "ch5"
4) "helloworld"
### --- punsubscribe 退订模式

127.0.0.1:6379> punsubscribe ch*
1) "punsubscribe"
2) "ch*"
3) (integer) 0
三、发布订阅的机制
### --- 订阅某个频道或模式:

~~~     # 客户端(client):
~~~     属性为pubsub_channels,该属性表明了该客户端订阅的所有频道属性为pubsub_patterns,
~~~     该属性表示该客户端订阅的所有模式
~~~     # 服务器端(RedisServer):
~~~     属性为pubsub_channels,该服务器端中的所有频道以及订阅了这个频道的客户端
~~~     属性为pubsub_patterns,该服务器端中的所有模式和订阅了这些模式的客户端
typedef struct redisClient {
    ...
    dict *pubsub_channels;          // 该client订阅的channels,以channel为key用dict的方式组织
    list *pubsub_patterns;          // 该client订阅的pattern,以list的方式组织
    ...
    } redisClient;
    struct redisServer {
    ...
    dict *pubsub_channels;          // redis server进程中维护的channel dict,它以channel为key,订 阅channel的client list为value
    list *pubsub_patterns;          // redis server进程中维护的pattern list
    int notify_keyspace_events;
    ...
};
~~~     # 当客户端向某个频道发送消息时,
~~~     Redis首先在redisServer中的pubsub_channels中找出键为该频道的结点,
~~~     遍历该结点的值,即遍历订阅了该频道的所有客户端,将消息发送给这些客户端。

~~~     # 然后,
~~~     遍历结构体redisServer中的pubsub_patterns,找出包含该频道的模式的结点,
~~~     将消息发送给订阅了该模式的客户端。
四、使用场景:哨兵模式,Redisson框架使用
### --- 哨兵模式

~~~     # 在Redis哨兵模式中,
~~~     哨兵通过发布与订阅的方式与Redis主服务器和Redis从服务器进行通信。
~~~     这个我们将在后面的章节中详细讲解。
~~~     # Redisson是一个分布式锁框架,
~~~     在Redisson分布式锁释放的时候,是使用发布与订阅的方式通知的,
~~~     这个我们将在后面的章节中详细讲解。

 
 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(16)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示