1 概念
进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。
2 指令
2.1 可以一次性订阅多个,SUBSCRIBE c1 c2 c3
2.2 消息发布,PUBLISH c2 hello-redis
2.3 订阅多个,通配符*, PSUBSCRIBE new*
2.4 收取消息, PUBLISH new1 redis2015
代码片:
//端口1订阅频道:
127.0.0.1:6379> SUBSCRIBE c1 c2 c3
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "c1"
3) (integer) 1
1) "subscribe"
2) "c2"
3) (integer) 2
1) "subscribe"
2) "c3"
3) (integer) 3
//端口2发布消息
127.0.0.1:6379> PUBLISH c1 hello1
(integer) 1
127.0.0.1:6379> PUBLISH c2 hello2
(integer) 1
127.0.0.1:6379> PUBLISH c3 hello3
(integer) 1
//端口1接收消息
1) "message"
2) "c1"
3) "hello1"
1) "message"
2) "c2"
3) "hello2"
1) "message"
2) "c3"
3) "hello3"
//端口1订阅频道
127.0.0.1:6379> PSUBSCRIBE xx*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "xx*"
3) (integer) 1
//端口2发布消息
127.0.0.1:6379> PUBLISH xx1 lolololol
(integer) 1
127.0.0.1:6379> PUBLISH xx2 djdjdjdj
(integer) 1
//端口1接收消息
1) "pmessage"
2) "xx*"
3) "xx1"
4) "lolololol"
1) "pmessage"
2) "xx*"
3) "xx2"
4) "djdjdjdj"