发布订阅
命令
- 订阅
subscribe 频道名 [频道名 ...]
例如:创建一个频道名为animal_channel
127.0.0.1:6379> subscribe animal_channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "animal_channel"
3) (integer) 1
- 发布消息
publish 频道 消息
例如:重新开一个终端,给animal_channel发送消息
127.0.0.1:6379> publish animal_channel 'panda is cute '
(integer) 1
127.0.0.1:6379> publish animal_channel 'I like panda'
(integer) 1
开启了订阅的anim_channel频道的终端接收到的数据
127.0.0.1:6379> subscribe animal_channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "animal_channel"
3) (integer) 1
1) "message"
2) "animal_channel"
3) "panda is cute "
1) "message"
2) "animal_channel"
3) "I like panda"
- 取消订阅
- 如果不写频道名,则表示取消所有频道
unsubscribe 频道名 [频道名 ...]
例如:取消已经订阅的animal_channel
127.0.0.1:6379> unsubscribe animal_channel
1) "unsubscribe"
2) "animal_channel"
3) (integer) 0

本文详细介绍Redis的发布订阅功能,包括如何使用subscribe命令订阅频道,publish命令发布消息到指定频道,以及unsubscribe命令取消订阅。通过实例演示了频道animal_channel的订阅与消息发布过程,展示了消息的实时接收与处理。
720

被折叠的 条评论
为什么被折叠?



