Redis的发布订阅
发布日期:2021-07-12 08:49:44 浏览次数:6 分类:技术文章

本文共 1194 字,大约阅读时间需要 3 分钟。

 是什么:

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

  订阅/发布消息图

 

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

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

2.消息发布,PUBLISH c2 hello-redis

 先订阅

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
1) "message"

发布

127.0.0.1:6379> PUBLISH c2 hello-redis

(integer) 1
127.0.0.1:6379> PUBLISH c1 hello1122
(integer) 1
127.0.0.1:6379>

 

收到消息

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
1) "message"
2) "c2"
3) "hello-redis"
1) "message"
2) "c1"
3) "hello1122"

 

======================

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

4.收取消息,PUBLISH new1 redis2015

127.0.0.1:6379> PSUBSCRIBE new*

Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "new*"
3) (integer) 1
1) "pmessage"
2) "new*"
3) "new1"
4) "redis2015"
1) "pmessage"
2) "new*"
3) "new2"
4) "redis2016"

 

127.0.0.1:6379> PUBLISH new1 redis2015

(integer) 1
127.0.0.1:6379> PUBLISH new2 redis2016
(integer) 1
127.0.0.1:6379>

 

转载于:https://www.cnblogs.com/wq3435/p/6116080.html

转载地址:https://blog.csdn.net/ankan7400/article/details/102088985 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Redis的复制(Master/Slave)
下一篇:Redis的事务

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月05日 21时37分55秒