当前位置:网站首页>Redis learning notes - publish and subscribe

Redis learning notes - publish and subscribe

2022-06-23 09:11:00 Love Guoba

Redis Provides the basis for “ Release / subscribe ” Message mechanism of pattern , In this mode , Message publishers and subscribers do not communicate directly , The publisher client sends the specified channel (channel) Release the news , Every client subscribing to this channel can receive this message

And many professional message queuing systems ( for example Kafka、RocketMQ) comparison ,Redis Publish / subscribe is a bit rough , For example, message stacking and backtracking cannot be realized . But it's simple enough , If the current scene can tolerate these shortcomings , It's a good choice

Redis It mainly provides information release 、 Subscribed Channels 、 Unsubscribe and subscribe and unsubscribe according to the mode ;

Release the news

publish channel message

Release a "channel:sports" Sports Channel News "James in Los Angeles"

127.0.0.1:6379> publish channel:sports "James in Los Angeles"
(integer) 0

Subscribe to news

subscribe channel [channel ...]

You can subscribe to multiple messages

Subscribe to this 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 point, a new client will release a message to this channel

127.0.0.1:6379> publish channel:sports "Harden in Houston"
(integer) 1

At this time, the subscriber will be pushed by the news just released

127.0.0.1:6379> subscribe channel:sports
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel:sports"
3) (integer) 1
1) "message"
2) "channel:sports"
3) "Harden in Houston"
  • 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 No persistence of published messages

Unsubscribe

unsubscribe channel [channel ...]

Unsubscribe from the sports channel

127.0.0.1:6379> unsubscribe channel:sports
1) "unsubscribe"
2) "channel:sports"
3) (integer) 0

Subscribe and unsubscribe according to the pattern

psubscribe pattern [pattern...]
punsubscribe [pattern [pattern ...]]

Subscribe to it All the channels at the beginning

127.0.0.1:6379> psubscribe it*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "it*"
3) (integer) 1

Query subscription

1. View active channels

pubsub channels [pattern]

Being active means that at least one person subscribes , among [pattern] Yes, you can specify specific patterns

127.0.0.1:6379> pubsub channels
1) "channel:sports"

2. View channel subscriptions

pubsub numsub [channel ...]

Inquire about channel:sports Number of channel subscriptions is 1 individual

127.0.0.1:6379> pubsub numsub channel:sports
1) "channel:sports"
2) (integer) 1

3. View the number of mode subscriptions

pubsub numpat

Currently subscribed through the mode are 1 A client

127.0.0.1:6379> pubsub numpat
(integer) 1
原网站

版权声明
本文为[Love Guoba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230900184465.html