Loading

(八)、Redis发布订阅

进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。

下图展示了频道 channel1 , 以及订阅这个频道的三个客户端 —— client2 、 client5 和 client1 之间的关系:

img

当有新消息通过 PUBLISH 命令发送给频道 channel1 时, 这个消息就会被发送给订阅它的三个客户端:

img

客户端首先订阅三个频道c1、c2、c3

127.0.0.1:6379> subscribe c1 c2 c3
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "c1"
3) (integer) 1
1) "subscribe"
2) "c2"
3) (integer) 2
1) "subscribe"
2) "c3"
3) (integer) 3

服务端向客户端推送一条消息

127.0.0.1:6379> subscribe c1 c2 c3 
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "c1"
3) (integer) 1
1) "subscribe"
2) "c2"
3) (integer) 2
1) "subscribe"
2) "c3"
3) (integer) 3
1) "message"
2) "c1"
3) "hello"

订阅多个通配符 *

PSUBSCRIBE new*

收取消息,

PUBLISH new1 redis2015
posted @ 2021-08-24 16:25  炒焖煎糖板栗  阅读(32)  评论(0编辑  收藏  举报