当前位置:网站首页>Publish and subscribe to redis

Publish and subscribe to redis

2022-07-06 09:21:00 ~Pompeii

Redis Publish and subscribe to

1. Introduction to publication and subscription

Redis Publish subscribe (pub/sub) It's a message communication mode : sender (pub) Send a message , subscriber (sub) receive messages .
Redis Clients can subscribe to any number of channels .
Redis Provides the basis for 「 Release / subscribe 」 Message mechanism of pattern , In this mode , There is no direct communication between the publisher and the subscriber .
The picture below shows the channel channel1 , And three clients that subscribe to this channel —— client2 、 client5 and client1 The relationship between

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-QYPwOc9W-1656468585459)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092442701.png)]

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-GpVSVwBL-1656468585460)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092454345.png)]

When new news passes PUBLISH Command to channel channel1 when , This message will be sent to the three clients who subscribe to it :

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-BmRYSWOk-1656468585460)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092514496.png)]

2. Publish and subscribe application scenarios

Suppose we have such a business scenario , After placing an order on the website , Need to inform inventory service for shipment processing .
It's not difficult to implement the above business , We just need to make the inventory service available to the relevant customers , After the order is paid, just call the inventory service .

 Insert picture description here

If there is a new business later , For example, the points service , He needs to get the results of the order payment , Then increase the user's points .
It's not hard to implement , Let the points service also provide an interface , After the order is paid, just call the inventory service .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-B4RrhzqZ-1656468585461)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629092747075.png)]

If two businesses need to obtain the results of the order payment , That's OK , Program transformation is also fast . But with the continuous development of business , More and more new business is said to be the result of order payment .
At this time, we will find that there are many problems with the above system architecture :
First of all , The order payment business is heavily coupled with other businesses , Whenever a new business needs to pay for the result , You need to change the order payment business .
second , If there are too many calls , The response time of the order payment interface will be longer . in addition , If any downstream interface slows down , The synchronization will cause the response of the single payment interface to become longer .
Third , If any downstream interface fails , Conditions that may lead to data inconsistency . For example, the figure below , First call A, Call after success B, Last call C.
If you're calling B Interface exception , At this point, the order payment interface may fail to return , But at this time A The interface has actually been called successfully , This means that it has internally processed the result of successful order payment .
This will lead to A,B,C Three downstream interfaces ,A Get success get payment results , however B,C Didn't get , The situation that leads to the data inconsistency of the three systems .
Actually, let's think about it , For order payment business , It doesn't really need to care about the downstream call results , As long as there is some mechanism to inform them .
Here we are. , This requires the introduction of the publish subscribe mechanism today .

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WEWireSG-1656468585461)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629095040384.png)]

Use Redis Publish subscribe mechanism , For the above business , The order payment business only needs to send a message to the payment result channel , Other downstream businesses subscribe to the channel of payment results , You can get the message , Then make business processing .
In this way, the call relationship between upstream and downstream of the system can be decoupled .

3. example

The following example demonstrates how a publish subscription works . In our example, we created a subscription channel named redisChat:

 SUBSCRIBE redisChat

 Insert picture description here

Now? , Let's turn on a new one redis client , And then on the same channel redisChat Two news releases , Subscribers can receive messages .
The subscriber's client will display the following message

PUBLISH redisChat "Redis is a great caching technique"

 Insert picture description here

PUBLISH redisChat "Learn redis by w3cschool.cc"

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-liCreDDl-1656468585462)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629095644923.png)]

Pubsub The command is used to view subscription and publishing system status , You can see one
Be careful : Don't write as pubsub redisChat

 PUBSUB CHANNELS!

 Insert picture description here

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OibXwJKi-1656468585462)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629095811652.png)]

Put the left client ctrl+c After closing , When searching again on the right , Found that it was empty

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-MWIEDufB-1656468585462)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629100055201.png)]

4.publish

Publish The command is used to send information to the specified channel .
Return value : Number of subscribers who received information .

PUBLISH channel message

5.subscribe

Subscribe The command is used to subscribe to the information of a given channel or channels .
We're using subscription commands , The main points need to be :
First of all , After the client executes the subscription instruction , You'll be in a subscription state , After that, you can only receive subscribe、psubscribe、unsubscribe、punsubscribe These four orders .
second , New subscription clients , I can't receive messages before this channel , This is because Redis It doesn't persist on published messages .
Return value : Information received

Compared with many majors MQ, such as kafka、rocketmq Come on , redis The publish and subscribe function is a little crude . however redis Publish and subscribe is better than simple , If the current scenario can tolerate these shortcomings , You can still choose to use .

SUBSCRIBE channel [channel ...]

6.psubscribe

Psubscribe Command to subscribe to one or more channels that conform to a given pattern .
Each pattern is represented by * As a match , such as it* Match all to it The first channel ( it.news 、 it.blog 、 it.tweets wait ). news.* Match all to news. The first channel ( news.it 、 news.global.today wait ), And so on .
Return the received information

PSUBSCRIBE pattern [pattern ...]

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-a1EsdK2y-1656468585463)(C:/Users/86158/AppData/Roaming/Typora/typora-user-images/image-20220629100645723.png)]

7.pubsub

Pubsub The command is used to view subscription and publishing system status , It consists of several subcommands in different formats .
Return value : A list of active channels

PUBSUB <subcommand> [argument [argument ...]]

8.unsubscribe

The command is used to unsubscribe information of a given channel or channels .
Return value : This command behaves differently in different clients .

 UNSUBSCRIBE channel [channel ...]

9.punsubscribe

Punsubscribe The command is used to unsubscribe from all channels in a given mode .
Return value : This command behaves differently in different clients

PUNSUBSCRIBE [pattern [pattern ...]]
原网站

版权声明
本文为[~Pompeii]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060859159389.html