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) 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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值