4 Redis的发布订阅
生活随笔
收集整理的这篇文章主要介绍了
4 Redis的发布订阅
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
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) 12 打开另一个客户端给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"总结
以上是生活随笔为你收集整理的4 Redis的发布订阅的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 3 Redis 配置文件
- 下一篇: 7 Redis 事务