当前位置:网站首页>Redis入门完整教程:发布订阅
Redis入门完整教程:发布订阅
2022-07-04 22:29:00 【谷哥学术】
Redis提供了基于“发布/订阅”模式的消息机制,此种模式下,消息发布
者和订阅者不进行直接通信,发布者客户端向指定的频道(channel)发布消
息,订阅该频道的每个客户端都可以收到该消息,如图3-16所示。Redis提
供了若干命令支持该功能,在实际应用开发时,能够为此类问题提供实现方
法。
3.7.1 命令
Redis主要提供了发布消息、订阅频道、取消订阅以及按照模式订阅和
取消订阅等命令。
1.发布消息
publish channel message
下面操作会向channel:sports频道发布一条消息“Tim won the
championship”,返回结果为订阅者个数,因为此时没有订阅,所以返回结果
为0:
127.0.0.1:6379> publish channel:sports "Tim won the championship"
(integer) 0
2.订阅消息
subscribe channel [channel ...]
订阅者可以订阅一个或多个频道,下面操作为当前客户端订阅了
channel:sports频道:
127.0.0.1:6379> subscribe channel:sports
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel:sports"
3) (integer) 1
此时另一个客户端发布一条消息:
127.0.0.1:6379> publish channel:sports "James lost the championship"
(integer) 1
当前订阅者客户端会收到如下消息:
127.0.0.1:6379> subscribe channel:sports
Reading messages... (press Ctrl-C to quit)
...
1) "message"
2) "channel:sports"
3) "James lost the championship"
如果有多个客户端同时订阅了channel:sports,整个过程如图3-17所
示。
有关订阅命令有两点需要注意:
·客户端在执行订阅命令之后进入了订阅状态,只能接收subscribe、
psubscribe、unsubscribe、punsubscribe的四个命令。
·新开启的订阅客户端,无法收到该频道之前的消息,因为Redis不会对
发布的消息进行持久化。

开发提示
和很多专业的消息队列系统(例如Kafka、RocketMQ)相比,Redis的发
布订阅略显粗糙,例如无法实现消息堆积和回溯。但胜在足够简单,如果当
前场景可以容忍的这些缺点,也不失为一个不错的选择。
3.取消订阅
unsubscribe [channel [channel ...]]
客户端可以通过unsubscribe命令取消对指定频道的订阅,取消成功后,
不会再收到该频道的发布消息:
127.0.0.1:6379> unsubscribe channel:sports
1) "unsubscribe"
2) "channel:sports"
3) (integer) 0
4.按照模式订阅和取消订阅
psubscribe pattern [pattern...]
punsubscribe [pattern [pattern ...]]
除了subcribe和unsubscribe命令,Redis命令还支持glob风格的订阅命令
psubscribe和取消订阅命令punsubscribe,例如下面操作订阅以it开头的所有
频道:
127.0.0.1:6379> psubscribe it*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "it*"
3) (integer) 1
5.查询订阅
(1)查看活跃的频道
pubsub channels [pattern]
所谓活跃的频道是指当前频道至少有一个订阅者,其中[pattern]是可以
指定具体的模式:
127.0.0.1:6379> pubsub channels
1) "channel:sports"
2) "channel:it"
3) "channel:travel"
127.0.0.1:6379> pubsub channels channel:*r*
1) "channel:sports"
2) "channel:travel"
(2)查看频道订阅数
pubsub numsub [channel ...]
当前channel:sports频道的订阅数为2:
127.0.0.1:6379> pubsub numsub channel:sports
1) "channel:sports"
2) (integer) 2
(3)查看模式订阅数
pubsub numpat
当前只有一个客户端通过模式来订阅:
127.0.0.1:6379> pubsub numpat
(integer) 1
3.7.2 使用场景
聊天室、公告牌、服务之间利用消息解耦都可以使用发布订阅模式,下
面以简单的服务解耦进行说明。如图3-18所示,图中有两套业务,上面为视
频管理系统,负责管理视频信息;下面为视频服务面向客户,用户可以通过
各种客户端(手机、浏览器、接口)获取到视频信息。

假如视频管理员在视频管理系统中对视频信息进行了变更,希望及时通
知给视频服务端,就可以采用发布订阅的模式,发布视频信息变化的消息到
指定频道,视频服务订阅这个频道及时更新视频信息,通过这种方式可以有
效解决两个业务的耦合性。
·视频服务订阅video:changes频道如下:
subscribe video:changes
·视频管理系统发布消息到video:changes频道如下:
publish video:changes "video1,video3,video5"
·当视频服务收到消息,对视频信息进行更新,如下所示:
for video in video1,video3,video5
update {video}
边栏推荐
- MySQL Architecture - logical architecture
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
- 蓝队攻防演练中的三段作战
- 质量体系建设之路的分分合合
- Locust performance test - environment construction and use
- SQL中MAX与GREATEST的区别
- 9 - 类
- Why is Dameng data called the "first share" of domestic databases?
- Attack and defense world misc advanced area can_ has_ stdio?
- Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
猜你喜欢
Serial port data frame
SPSS installation and activation tutorial (including network disk link)
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
Concurrent network modular reading notes transfer
Advanced area a of attack and defense world misc Masters_ good_ idea
PMO: compare the sample efficiency of 25 molecular optimization methods
攻防世界 misc 进阶区 2017_Dating_in_Singapore
LOGO特训营 第二节 文字与图形的搭配关系
Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
攻防世界 MISC 进阶区 3-11
随机推荐
Redis的持久化机制
Breakpoint debugging under vs2019 c release
Challenges faced by virtual human industry
LOGO special training camp section I identification logo and Logo Design Ideas
MySQL storage data encryption
Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
The proofreading activity of data science on the command line second edition was restarted
BigFilter全局交易防重组件的介绍与应用
Close system call analysis - Performance Optimization
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
繁華落盡、物是人非:個人站長該何去何從
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
攻防世界 MISC 进阶区 can_has_stdio?
Deployment of JVM sandbox repeater
剑指 Offer 65. 不用加减乘除做加法
The difference between Max and greatest in SQL
攻防世界 MISC 进阶区 Ditf
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
PHP short video source code, thumb animation will float when you like it