Original: Redis's publish subscription and. NET client implementation
Preface
Publish a subscription in design mode can also be said to be the observer pattern, for this pattern is to handle a one-to-many dependencies between objects, when an object changes, other dependent on his objects are notified and updated.
However, it also has its own shortcomings, that is, when the theme of a series of changes, the observer to do batch updates, if such a high cost of updating, then the solution is based on the type of demand notification, and can not blindly notify all observers.
That against this disadvantage, in general, you do not need to subscribe to a message that has nothing to do with their own push? This also means that the push message needs to be collated and not swarm what messages are thrown into a channel, to divide and conquer, reasonable design of the use of the release channel, but also a reasonable subscription channel.
So, to upgrade to the system project level, he does not bring us again, the great advantage is: stripping system coupling, reduce the dependence of single-line function, and is catering to high cohesion, loosely coupled system architecture design.
Pull so much, only nonsense, because I do not know the preface of this article what to write, so it.
Publish/Subscribe features in Redis
This section refers to the official documentation: HTTPS://REDIS.IO/TOPICS/PUBSUB
First I prepared 1 Redis services, 3 clients, as shown in:
Then open the official document, the first can see the following 6 commands, yes, only these 6 commands, as long as you can grasp the understanding, divergent thinking flexible use. Spit, Spit, then Tao in this, give birth to a, born two, born three, living all things, nothing!! C,C,C,WC, Boy, save the world and see you later.
Let's use these commands to make a presentation that you understand.
1, 2 clients subscribe to the Order.create channel message, as follows:
2. The last client publishes a message to the Order.create channel. As follows:
3, you will immediately find that the other 2 clients subscribing to this channel have information output, as follows:
Simple no, one of the basic functions of publishing a subscription and it's finished.
Well, if you know more about other publishing subscription management systems, you'll immediately think of a feature similar to the topic type in RABBITMQ. Is there any in the Redis, the answer to these 6 commands? The command used is psubscribe.
127.0. 0.1:6379> Psubscribe * --- subscribe to all channels 127.0. 0.1:6379> Psubscribe order.* ---Subscription channel name all channel messages beginning with order.
So how do you unsubscribe from a subscribed channel?
127.0. 0.1:6379> Unsubscribe order.create --- unsubscribe 127.0. 0.1:6379> Punsubscribe order.* ---Unsubscribe channel name with order. All channel messages beginning with
How do I view my subscription information?
127.0. 0.1:6379> PubSub channels --- view all channel 127.0 for the current server subscription . 0.1:6379> PubSub channels order.* --- view the subscription channel name with order. All channels beginning with 127.0. 0.1:6379> PubSub numsub order.create user ---to see the number of subscribers to the Order.create and user channels, Supports querying multiple channels
Well, so far, 6 orders have been exhausted. It's so willful, yes, you've been practicing for more than 10 minutes you've learned the top-level publishing subscription skills in Redis. You can go out and beat the world without a rival.
Stackexchange.redis Implementing the Publish subscription feature in Redis
In this section, I can not really say how to make more reasonable point, I just the last example, you copy the code to play it. On the code.
Static voidMain (string[] args) {Console.WriteLine ("Please enter a publication subscription type?"); var type=Console.ReadLine (); if(Type = ="Publish") { while(true) {Console.WriteLine ("Please enter which channel to publish to? "); var channel=Console.ReadLine (); Console.WriteLine ("Please enter the message content to be published."); var message=Console.ReadLine (); Sub. Publish (channel, message); } } Else{Console.WriteLine ("Please enter the information of which channel you want to subscribe to? "); var Channelkey=Console.ReadLine (); Sub. Subscribe (Channelkey, (channel, message)={Console.WriteLine ("The content that is accepted for publication is:"+message); }); Console.WriteLine ("the channel you subscribed to is:<<"+ Channelkey +">>! Everything is ready, wait for the release message! Don't move, no move!! "); Console.readkey (); } }
Run up several instances to play a game. Below, 5, 1 release information, 4 subscription information, 2 subscriptions Zhanglonghao channel, 2 subscription Bokeyuan channel.
The first time I posted a message to the Zhanglonghao channel, the message was published as: Hello Shuaige!! As follows:
It can be seen that only subscribe to the Zhanglonghao channel to receive the message.
Then send it to the Bokeyuan channel, hello Bokeyuan!
Let's play it by myself.
Summary
Next is everyone's favorite summary content, the content is two, as follows:
1, I hope to pay attention to other articles.
2, the blog there is no clear white, or you have a better way, then welcome to join the top left 2 communication groups, we learn to explore.
Redis's publish subscriptions and. NET client implementations