redis中的客户端可以订阅一个自定义的频道,接受来自该频道的消息

订阅

订阅指定频道-SUBSCRIBE

SUBSCRIBE channel [channel2]...

SUBSCRIBE 频道名 [频道名2]...

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

订阅匹配频道-PSUBSCRIBE

匹配模式可以订阅名称匹配符合的频道

PSUBSCRIBE channel*

代表订阅channel开头的频道

127.0.0.1:6379> psubscribe channel*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "channel*"
3) (integer) 1

发布

PUBLISH

PUBLISH channel message

向特定频道发送消息

127.0.0.1:6379> publish channel3 "test channel 3"
(integer) 1
127.0.0.1:6379> publish channel1 "test channel 1"
(integer) 3

Redis的发布订阅系统也要注意一些问题,网络传输可能掉线问题

05-27 07:49