参考资料: http://redis.io/topics/pubsub
3订阅Subscribe
127.0.0.1:6379> SUBSCRIBE "muc-1" // muc-1 为channel号 Reading messages... (press Ctrl-C to quit) // 从 redis 服务端返回的订阅结果 1) "subscribe" 2) "muc-1" 3) (integer) 1 // 接收下面 Publish的消息 1) "message" 2) "muc-1" 3) "hello, everyone" |
3发布Publish(注:是广播给所有的 subscriber)
127.0.0.1:6379> PUBLISH muc-b "hello, every muc-b friends" (integer) 2 // 表示接收到此发布消息的 subscriber 的数目 |
3订阅多个channel
127.0.0.1:6379> SUBSCRIBE muc-a muc-b muc-c Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "muc-a" 3) (integer) 1 1) "subscribe" 2) "muc-b" 3) (integer) 2 1) "subscribe" 2) "muc-c" 3) (integer) 3 // 接收 muc-a 通道的消息 1) "message" 2) "muc-a" 3) "hello, muc-a friends" // 接收 muc-b 通道的消息 1) "message" 2) "muc-b" 3) "hello, muc-b friends" |
3Pattern-matching订阅
127.0.0.1:6379> PSUBSCRIBE news.* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "news.*" 3) (integer) 1 // 接收发布至 news.1 channel的消息 1) "pmessage" 2) "news.*" 3) "news.1" 4) "hello, news-1" // 接收发布至 news-sport channel的消息 1) "pmessage" 2) "news.*" 3) "news.sport" 4) "hello, news-sport" |
3列出当前所有的channels
127.0.0.1:6379> PUBSUB CHANNELS 1) "muc-c" 2) "muc-b" 3) "muc-a" 127.0.0.1:6379> |
3获取指定channel上的subscriber数目
127.0.0.1:6379> PUBSUB NUMSUB muc-a 1) "muc-a" 2) "1" // subscriber number = 1 127.0.0.1:6379> PUBSUB NUMSUB muc-b 1) "muc-b" 2) "2" // subscriber number = 2 127.0.0.1:6379> |
3使用Jedis实现简单PubSub的示例
JedisPool pool = new JedisPool(new JedisPoolConfig(), "192.168.28.153"); Jedis jedis = pool.getResource(); String channel = "muc-a"; jedis.subscribe(new JedisPubSub() { @Override publicvoid onUnsubscribe(String channel, int subscribedChannels) { System.out.println("onUnsubscribe - " + channel + "|" + subscribedChannels); } @Override publicvoid onSubscribe(String channel, int subscribedChannels) { System.out.println("onSubscribe - " + channel + "|" + subscribedChannels); } @Override publicvoid onPUnsubscribe(String pattern, int subscribedChannels) { System.out.println("onPUnsubscribe - " + pattern + "|" + subscribedChannels); } @Override publicvoid onPSubscribe(String pattern, int subscribedChannels) { System.out.println("onPSubscribe - " + pattern + "|" + subscribedChannels); } @Override publicvoid onPMessage(String pattern, String channel, String message) { System.out.println("onPMessage - " + pattern + "|" + channel + "|" + message); } @Override publicvoid onMessage(String channel, String message) { System.out.println("onMessage - " + channel + "|" + message); } }, channel); pool.returnResource(jedis); pool.destroy(); |
使用redis-cli命令PUBLISH消息:
127.0.0.1:6379> PUBLISH muc-a "hello, every muc-a friends" (integer) 1 127.0.0.1:6379> |
在Java程序的输出中可以看到如下日志:
onSubscribe - muc-a|1 // 在初始订阅成功后触发了onSubscribe方法 onMessage - muc-a|hello, every muc-a friends // 在接收发布的消息后触发onMessage方法 |
3Python示例(基于redis-py库)
>>> import redis >>> r = redis.StrictRedis(host='localhost', port=6379, db=0) >>> ps = r.pubsub() >>> ps.subscribe('muc-a') >>> for msg in ps.listen(): ... print msg['data'] ... 1 hello, every muc-a friends |