当前位置:网站首页>Redis getting started complete tutorial: publish and subscribe
Redis getting started complete tutorial: publish and subscribe
2022-07-04 22:53:00 【Gu Ge academic】
Redis Provides the basis for “ Release / subscribe ” Message mechanism of pattern , In this mode , News release
The subscriber and the subscriber do not communicate directly , The publisher client sends the specified channel (channel) Release cancellation
Rest , Every client subscribing to this channel can receive this message , Pictured 3-16 Shown .Redis carry
Several commands are provided to support this function , In practical application development , Can provide implementers for such problems
Law .
3.7.1 command
Redis It mainly provides information release 、 Subscribed Channels 、 Unsubscribe and subscribe to and in accordance with the pattern
Unsubscribe and other commands .
1. Release the news
publish channel message
The following operation will give channel:sports The channel has a message “Tim won the
championship”, The return result is the number of subscribers , Because there is no subscription at this time , So return the result
by 0:
127.0.0.1:6379> publish channel:sports "Tim won the championship"
(integer) 0
2. Subscribe to news
subscribe channel [channel ...]
Subscribers can subscribe to one or more channels , The following operation subscribes for the current client
channel:sports channel :
127.0.0.1:6379> subscribe channel:sports
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel:sports"
3) (integer) 1
At this time, another client releases a message :
127.0.0.1:6379> publish channel:sports "James lost the championship"
(integer) 1
The current subscriber client will receive the following message :
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"
If there are multiple clients subscribed to channel:sports, The whole process is as shown in the figure 3-17 the
in .
There are two points to note about subscription commands :
· The client enters the subscription state after executing the subscription command , receive calls only subscribe、
psubscribe、unsubscribe、punsubscribe The four orders of .
· New subscription client , Unable to receive previous messages from this channel , because Redis It won't be right
Persistence of published messages .
Development tips
And many professional message queuing systems ( for example Kafka、RocketMQ) comparison ,Redis The hair of
The cloth is slightly rough , For example, message stacking and backtracking cannot be realized . But it's simple enough , If so
These shortcomings can be tolerated in the previous scenario , It's a good choice .
3. Unsubscribe
unsubscribe [channel [channel ...]]
The client can use the unsubscribe Command to unsubscribe from the specified channel , After the cancellation is successful ,
No more announcements from this channel :
127.0.0.1:6379> unsubscribe channel:sports
1) "unsubscribe"
2) "channel:sports"
3) (integer) 0
4. Subscribe and unsubscribe according to the pattern
psubscribe pattern [pattern...]
punsubscribe [pattern [pattern ...]]
except subcribe and unsubscribe command ,Redis The command also supports glob Style subscription command
psubscribe And unsubscribe commands punsubscribe, For example, the following operation subscribes to it All of the beginning
channel :
127.0.0.1:6379> psubscribe it*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "it*"
3) (integer) 1
5. Query subscription
(1) View active channels
pubsub channels [pattern]
The so-called active channel means that the current channel has at least one subscriber , among [pattern] Yes.
Specify the specific mode :
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) View channel subscriptions
pubsub numsub [channel ...]
At present channel:sports The number of subscriptions for the channel is 2:
127.0.0.1:6379> pubsub numsub channel:sports
1) "channel:sports"
2) (integer) 2
(3) View the number of mode subscriptions
pubsub numpat
At present, only one client subscribes through the mode :
127.0.0.1:6379> pubsub numpat
(integer) 1
3.7.2 Use scenarios
The chat room 、 Bulletin board 、 Using message decoupling between services can use publish subscribe mode , Next
The surface is illustrated by simple service decoupling . Pictured 3-18 Shown , There are two sets of business , The above is regarded as
Frequency management system , Responsible for managing video information ; Here is the video service for customers , The user can go through
Various clients ( mobile phone 、 browser 、 Interface ) Get video information .
If the video administrator changes the video information in the video management system , Hope to get through in time
Tell the video server , You can use publish subscribe mode , Release the message of video information change to
Specify channel , Video service subscribes to this channel to update video information in time , In this way, there can be
Effectively solve the coupling of two services .
· Video service subscription video:changes The channels are as follows :
subscribe video:changes
· The video management system publishes a message to video:changes The channels are as follows :
publish video:changes "video1,video3,video5"
· When the video service receives a message , Update the video information , As shown below :
for video in video1,video3,video5
update {video}
边栏推荐
- 华泰证券是国家认可的券商吗?开户安不安全?
- Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
- 模拟摇杆控制舵机
- 新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
- Unity-VScode-Emmylua配置报错解决
- UML diagram memory skills
- Feature scaling normalization
- Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
- Test will: bug classification and promotion solution
- 【OpenGL】笔记二十九、抗锯齿(MSAA)
猜你喜欢
常用技术指标之一文读懂BOLL布林线指标
质量体系建设之路的分分合合
Redis入门完整教程:发布订阅
LOGO特训营 第四节 字体设计的重要性
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
Attack and defense world misc advanced area can_ has_ stdio?
Logo special training camp section III initial creative techniques
攻防世界 MISC 高手进阶区 001 normal_png
Redis入门完整教程:集合详解
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
随机推荐
Create Ca and issue certificate through go language
Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
The overview and definition of clusters can be seen at a glance
String类中的常用方法
剑指Offer 68 - II. 二叉树的最近公共祖先
【烹饪记录】--- 青椒炒千张
业务太忙,真的是没时间搞自动化理由吗?
Redis入门完整教程:HyperLogLog
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
On-off and on-off of quality system construction
Wake up day, how do I step by step towards the road of software testing
Business is too busy. Is there really no reason to have time for automation?
攻防世界 misc 高手进阶区 a_good_idea
Redis入门完整教程:Pipeline
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
Analysis of the self increasing and self decreasing of C language function parameters
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
POM in idea XML dependency cannot be imported
Gnawing down the big bone - sorting (II)
Breakpoint debugging under vs2019 c release