Redis 的发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息
Redis 客户端可以订阅任意数量的频道。
先订阅后发布才能收到消息
1 打开一个客户端订阅channel1
[chengwen@localhost redis]$ redis-server /etc/redis.conf
[chengwen@localhost redis]$ redis-cli
127.0.0.1:6379> subscribe channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
2 打开另一个客户端给chanel1 发布消息hello redis
[chengwen@localhost redis]$ redis-cli
127.0.0.1:6379> publish channel1 "hello redis"
(integer) 1
127.0.0.1:6379>
3 回看第一个客户端消息变为
[chengwen@localhost redis]$ redis-server /etc/redis.conf
[chengwen@localhost redis]$ redis-cli
127.0.0.1:6379> subscribe channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
1) "message"
2) "channel1"
3) "hello redis"
增加了如下消息
1) "message"
2) "channel1"
3) "hello redis"