码迷,mamicode.com
首页 > 其他好文 > 详细

Redis的发布和订阅

时间:2019-05-28 15:36:19      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:pattern   信息   一个   sage   global   quit   pat   VID   sub   

发布publish

订阅subscribe

Redis 通过 PUBLISH 、 SUBSCRIBE 等命令实现了订阅与发布模式。

技术图片

技术图片

 

 技术图片

 

举例1:qq群的公告,单个发布者,多个收听者

发布/订阅 实验

发布订阅的命令

PUBLISH channel msg
    将信息 message 发送到指定的频道 channel


SUBSCRIBE channel [channel ...]
    订阅频道,可以同时订阅多个频道


UNSUBSCRIBE [channel ...]
    取消订阅指定的频道, 如果不指定频道,则会取消订阅所有频道

PSUBSCRIBE pattern [pattern ...]
    订阅一个或多个符合给定模式的频道,每个模式以 * 作为匹配符,比如 it* 匹配所    有以 it 开头的频道( it.news 、 it.blog 、 it.tweets 等等), news.* 匹配所有   
 以 news. 开头的频道( news.it 、 news.global.today 等等),诸如此类

PUNSUBSCRIBE [pattern [pattern ...]]
    退订指定的规则, 如果没有参数则会退订所有规则

PUBSUB subcommand [argument [argument ...]]
    查看订阅与发布系统状态
注意:使用发布订阅模式实现的消息队列,当有客户端订阅channel后只能收到后续发布到该频道的消息,之前发送的不会缓存,必须Provider和Consumer同时在线。

  

发布订阅实测:

一个发布方,两个订阅方

发布方:   

127.0.0.1:6888> publish diantai nihao
(integer) 1
127.0.0.1:6888> 

  

一个订阅方:

127.0.0.1:6888> subscribe diantai
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "diantai"
3) (integer) 1
1) "message"
2) "diantai"
3) "nihao"

  

另外一个订阅方:

127.0.0.1:6888> subscribe zuqiu
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "zuqiu"
3) (integer) 1

一个发布方,两个订阅方,其中一个进行正则订阅

发布方:

127.0.0.1:6888> publish python1 nihao
(integer) 2

  

订阅方:

127.0.0.1:6888> psubscribe python*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "python*"
3) (integer) 1
1) "pmessage"
2) "python*"
3) "python1"
4) "nihao"

  

127.0.0.1:6888> psubscribe python1
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "python1"
3) (integer) 1
1) "pmessage"
2) "python1"
3) "python1"
4) "nihao"

  

Redis的发布和订阅

标签:pattern   信息   一个   sage   global   quit   pat   VID   sub   

原文地址:https://www.cnblogs.com/xinjie123/p/10937646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!