当前位置:网站首页>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}
边栏推荐
- 攻防世界 MISC 进阶区 can_has_stdio?
- 特征缩放 标准化 归一化
- Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
- Introducing QA into the software development lifecycle is the best practice that engineers should follow
- Interview essential leetcode linked list algorithm question summary, whole process dry goods!
- Advanced area of attack and defense world misc 3-11
- LOGO特訓營 第一節 鑒別Logo與Logo設計思路
- Attack and Defense World MISC Advanced Area Erik baleog and Olaf
- Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
- leetcode 72. Edit Distance 编辑距离(中等)
猜你喜欢
Why is Dameng data called the "first share" of domestic databases?
Domestic database chaos
How to send a reliable request before closing the page
BigFilter全局交易防重组件的介绍与应用
Unity-VScode-Emmylua配置报错解决
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
Logo special training camp section 1 Identification logo and logo design ideas
Breakpoint debugging under vs2019 c release
Business is too busy. Is there really no reason to have time for automation?
LOGO特训营 第三节 首字母创意手法
随机推荐
Postgresqlql advanced skills pivot table
环境加密技术解析
Why is Dameng data called the "first share" of domestic databases?
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Recommendation of mobile app for making barcode
It is said that software testing is very simple, but why are there so many dissuasions?
Solana chain application crema was shut down due to hacker attacks
Deployment of JVM sandbox repeater
蓝队攻防演练中的三段作战
Attack and defense world misc advanced grace-50
微服务--开篇
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
MYSQL架构——逻辑架构
MySQL Architecture - logical architecture
Mysql root 账号如何重置密码
LOGO特训营 第五节 字体结构与设计常用技法
攻防世界 misc 高手进阶区 a_good_idea
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Sobel filter
Lost in the lock world of MySQL