Pub/sub is a message communication mode. The main purpose is to decouple the coupling between the message publisher and the message subscriber, which is similar to the observer mode in the design mode. Pub/sub not only solves the Direct Problem of publishers and subscribers Code Level coupling also solves the coupling between the two in physical deployment. As a pub/sub server, redis enables message routing between subscribers and publishers. The subscribe and psubscribe commands subscribe to the redis server to subscribe to the Message type that you are interested in. redis calls the message type channel ). When the publisher sends a specific type of message to the redis server through the publish command. All clients that subscribe to this message type will receive this message. Here, messages are delivered in many-to-many ways. A client can subscribe to multiple channels or send messages to multiple channels.
The following is an experiment. Here we use two different clients: redis-CLI, which comes with redis, and simple client written in Java. The Code is as follows:
Import java.net .*;
Import java. Io .*;
Public class pubsubtest {
Public static void main (string [] ARGs ){
String cmd = ARGs [0] + "\ r \ n ";
Try {
Socket socket = new socket ("192.168.56.55", 6379 );
Inputstream in = socket. getinputstream ();
Outputstream out = socket. getoutputstream ();
Out. Write (CMD. getbytes (); // sends the subscription command
Byte [] buffer = new byte [1024];
While (true ){
Int readcount = in. Read (buffer );
System. Out. Write (buffer, 0, readcount );
System. Out. println ("--------------------------------------");
}
} Catch (exception e ){
}
}
}
The Code simply reads the SUBSCRIBE command from the command line and sends it to the redis server through a socket connection. Then, the while loop reads the messages subscribed by the redis server. And print it to the console
1. Compile and run the JavaProgram(I am running under win7)
D: \> javac pubsubtest. Java
D: \> JAVA pubsubtest"Subscribe news. Share news. Blog"
* 3
$9
Subscribe
$10
News. Share
: 1
* 3
$9
Subscribe
$9
News. Blog
: 2
--------------------------------------
2. Start redis-cli
Redis>Psubscribe news .*
Reading messages... (press Ctrl-C to quit)
1. "psubscribe"
2. "news .*"
3. (integer) 1
3. Start another redis-CLI to publish two messages.
Redis>Publish news. Share "share a link http://www.google.com"
(Integer) 2
Redis>Publish news. blog "I post a blog"
(Integer) 2
4. view the output of two subscription clients
In this case, the Java client prints the following content:
* 3
$7
Message
$10
News. Share
$34
Share a link http://www.google.com
--------------------------------------
* 3
$7
Message
$9
News. Blog
$13
I post a blog
--------------------------------------
The output of another redis-CLI is as follows:
1. "pmessage"
2. "news .*"
3. "news. Share"
4. "share a link http://www.google.com"
1. "pmessage"
2. "news .*"
3. "news. blog"
4. "I post a blog"
Under Analysis
Java client uses the SUBSCRIBE command to subscribe to news. share and news. blog two channels, and then immediately receive the successful subscription message returned by the server. It can be seen that the redis Protocol is of the text type. Here we do not explain the specific Protocol content. For details, refer to dig. This packet has two parts. The first part indicates that the socket connection uses subscribe to news. after a successful share, the number of subscription channels for this connection is 1, and the last part indicates that the subscribe is used to subscribe to news. after the blog is successful, the total number of connections for reading is 2.
Redis client uses psubscribe to subscribe to a channel that uses wildcards (* represents any string). This subscription will receive all channel messages that match news. A successful message printed from redis-CLI to the Console indicates that after the message is successfully subscribed using the psubscribe command, the total number of connected subscription channels is 1.
When publish is used in a client to send two messages to the news. Share and news. Blog channels. Redis returns (integer) 2, indicating that two connections have received the message. It can be verified by observing the output of two subscribers. The specific format is simple.
After reading a small example, we should have a perceptual knowledge of the pub/sub function. It should be noted that when a connection is subscribed through the subscribe or psubscribe channel, it enters the subscription mode. In this mode, you cannot send other commands except subscribe to additional channels or exit the subscription mode using the unsubscribe or punsubscribe commands. In addition, the psubscribe command is used to subscribe to multiple wildcard channels. If a message matches multiple channel modes, the same message is received multiple times.
The current version of jredis does not support pub/sub, but it should be quite simple to implement it. The entire application can share the same connection. Because the message returned by redis includes message-related channel information in addition to the message content. After receiving the message, you can call different callback methods based on different channel information.
In addition, I personally think the pub/sub of redis is a little too thin (only 150 lines of code are used for implementation ). There is not much support for security, authentication, and reliability.