Redis消息订阅发布

Redis 消息订阅发布

一、是什么

进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。

二、命令

序号命令描述
1PSUBSCRIBE pattern [pattern …]订阅一个或多个符合给定模式的频道
2PUBSUB subcommand [argument [argument …]]查看订阅与发布系统状态
3PUBLISH channel message将消息发送到指定的频道
4PUNSUBSCRIBE [pattern [pattern …]]退订所有给定模式的频道
5SUBSCRIBE channel [channel …]订阅给定的一个或多个频道的信息
6UNSUBSCRIBE [channel [channel …]]指退订给定的频道

三、案例

先订阅后发布后才能收到消息

1 可以一次性订阅多个,SUBSCRIBE c1 c2 c3

2 消息发布,PUBLISH c2 hello-redis

3 订阅多个,通配符 *, PSUBSCRIBE new*

4 收取消息, PUBLISH new1 redis2015

客户端 A

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

客户端 B

127.0.0.1:6379> PUBLISH c2 hello-redis
(integer) 1
127.0.0.1:6379> PUBLISH c1 hello1122
(integer) 1

客户端 A 接收到消息

1) "message"
2) "c2"
3) "hello-redis"
1) "message"
2) "c1"
3) "hello1122"

客户端 C

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

客户端 D

127.0.0.1:6379> PUBLISH new1 redis2015
(integer) 1
127.0.0.1:6379> PUBLISH new13 redisnews2016
(integer) 1

客户端 C 接收消息

1) "pmessage"
2) "new*"
3) "new1"
4) "redis2015"
1) "pmessage"
2) "new*"
3) "new13"
4) "redisnews2016"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值