redis作为一个pub/sub server,在订阅者和发布者之间起到了消息路由的功能。订阅者可以通过subscribe和psubscribe命令向redis server订阅自己感兴趣的消息类型,redis将消息类型称为频道(channel)。当发布者通过publish命令向redis server发送特定类型的消息时。订阅该消息类型的全部client都会收到此消息。这里消息的传递是多对多的。一个client可以订阅多个 channel,也可以向多个channel发送消息。

终端A: 127.0.0.1:6379> subscribe comments Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "comments" 3) (integer) 1 终端B: 127.0.0.1:6379> subscribe comments Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "comments" 3) (integer) 1 终端C: 127.0.0.1:6379> publish comments good! (integer) 2 终端A: 127.0.0.1:6379> subscribe comments Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "comments" 3) (integer) 1 1) "message" 2) "comments" 3) "good!" 终端B: 127.0.0.1:6379> subscribe comments Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "comments" 3) (integer) 1 1) "message" 2) "comments" 3) "good!" 

可以使用psubscribe通过通配符进行多个channel的订阅