Redis的发布及订阅
- 发布订阅(pub/sub)是一种消息通信模式,主要的目的是解决消息发布者和消息订阅者之间的耦合,这点和设计模式中的观察者模式比较相似;pub/sub不仅仅解决发布者和订阅者直接代码级别耦合也解决两者在物理部署上的耦合;
- redis作为一个pub/sub server,在订阅者和发布者之间起到了消息路由的功能,订阅者可以通过subscribe和psubscribe命令向redis server订阅自己感兴趣的消息类型,redis将消息类型称为通道(channel);
- 当发布者通过publish命令向redis server发送特定类型的消息时,订阅该消息类型的全部client都会收到此消息;这里消息的传递是多对多的,一个client可以订阅多个channel,也可以向多个channel发送消息;
- 发布/订阅的命令:
- SUBSCRIBE channel [channel …]:Subscribes the client to the specified channels,Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands;
- PSUBSCRIBE pattern [pattern …]:Subscribes the client to the given patterns;
- UNSUBSCRIBE [channel [channel …]]:Unsubscribes the client from the given channels, or from all of them if none is given,When no channels are specified, the client is unsubscribed from all the previously subscribed channels. In this case, a message for every unsubscribed channel will be sent to the client;
- PUNSUBSCRIBE [pattern [pattern …]]:Unsubscribes the client from the given patterns;
- PUBLISH channel message:Posts a message to the given channel; Integer reply:the number of clients that received the message;
- 开启三个redis-cli测试发布订阅的例子;
- redis的协议是文本类型的,具体链接为:http://redis.io/topics/protocol;
- 使用java客户端来订阅消息;
———————— 发布/订阅测试 ————————
— 1.打开第一个redis-cli的客户端订阅news:snda和news:taobao两个channels,收到redis-server返回订阅成功的消息;
redis 127.0.0.1:6379> subscribe news:snda news:taobao
Reading messages… (press Ctrl-C to quit)
1) “subscribe”
2) “news:snda”
3) (integer) 1
1) “subscribe”
2) “news:taobao”
3) (integer) 2
Reading messages… (press Ctrl-C to quit)
1) “subscribe”
2) “news:snda”
3) (integer) 1
1) “subscribe”
2) “news:taobao”
3) (integer) 2
.png)
— 2.打开第二个redis-cli客户端,使用psubscribe订阅news:*模式(*表示任意字符串)的channels;
redis 127.0.0.1:6379> psubscribe news:*
Reading messages… (press Ctrl-C to quit)
1) “psubscribe”
2) “news:*”
3) (integer) 1
Reading messages… (press Ctrl-C to quit)
1) “psubscribe”
2) “news:*”
3) (integer) 1
.png)
— 3.打开第三个redis-cli客户端,通过publish命令发布消息,返回值表示接收到此订阅的个数,通过订阅的窗口查看消息;
redis 127.0.0.1:6379> publish news:snda “www.snda.com”
(integer) 2
redis 127.0.0.1:6379> publish news:taobao “www.taobao.com”
(integer) 2
(integer) 2
redis 127.0.0.1:6379> publish news:taobao “www.taobao.com”
(integer) 2
.png)
.png)
.png)
———————— 发布/订阅测试 ————————
———————— 使用java客户端来订阅消息 ————————
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class PubSubTest
{
private static Socket socket;
public static void main(String[] args)
{
// String cmd = args[0] + “\r\n”;
String cmd = “subscribe news:snda news:taobao \r\n”;
try
{
socket = new Socket( “192.168.10.112”, 6379);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(cmd.getBytes());
// 发送订阅命令
byte[] buffer = new byte[1024];
while ( true)
{
int readCount = in.read(buffer);
System. out.write(buffer, 0, readCount);
System. out.println( “————————————–“ );
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
.png)
———————— 使用java客户端来订阅消息 ————————