Redis之发布与订阅
订阅者可以通过subscribe和psubscribe命令向redis server订阅自己感兴趣的消息类型。redis将信息类型称为通道(channel)。当发布者通过publish命令向redis server发送特定类型的信息时,订阅该信息类型的全备client都会收到此消息。
会话1:监听频道1
127.0.0.1:6379> subscribe tv1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
会话2:监听频道1和2
127.0.0.1:6379> subscribe tv1 tv2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "subscribe"
2) "tv2"
3) (integer) 2
会话3:在频道1发布信息
127.0.0.1:6379> publish tv1 stone
(integer) 2
查看会话1:
127.0.0.1:6379> subscribe tv1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "message"
2) "tv1"
3) "stone"
查看会话2:
127.0.0.1:6379> subscribe tv1 tv2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "subscribe"
2) "tv2"
3) (integer) 2
1) "message"
2) "tv1"
3) "stone"
会话3:在频道2发布信息
127.0.0.1:6379> publish tv2 stone2
(integer) 1
查看会话2:
127.0.0.1:6379> subscribe tv1 tv2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "subscribe"
2) "tv2"
3) (integer) 2
1) "message"
2) "tv1"
3) "stone"
1) "message"
2) "tv2"
3) "stone2
网页标题:Redis之发布与订阅
文章来源:http://cdiso.cn/article/gscjdo.html
会话1:监听频道1
127.0.0.1:6379> subscribe tv1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
会话2:监听频道1和2
127.0.0.1:6379> subscribe tv1 tv2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "subscribe"
2) "tv2"
3) (integer) 2
会话3:在频道1发布信息
127.0.0.1:6379> publish tv1 stone
(integer) 2
查看会话1:
127.0.0.1:6379> subscribe tv1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "message"
2) "tv1"
3) "stone"
查看会话2:
127.0.0.1:6379> subscribe tv1 tv2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "subscribe"
2) "tv2"
3) (integer) 2
1) "message"
2) "tv1"
3) "stone"
会话3:在频道2发布信息
127.0.0.1:6379> publish tv2 stone2
(integer) 1
查看会话2:
127.0.0.1:6379> subscribe tv1 tv2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "tv1"
3) (integer) 1
1) "subscribe"
2) "tv2"
3) (integer) 2
1) "message"
2) "tv1"
3) "stone"
1) "message"
2) "tv2"
3) "stone2
网页标题:Redis之发布与订阅
文章来源:http://cdiso.cn/article/gscjdo.html