redis的订阅和发布简介
进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息。
原理图:
redis 消息订阅和发布命令
-
psubscribe pattern [pattern...]:订阅一个或多个给定模式的 频道 -
punsubscribe [pattern [pttern...]]:退订所有频道给定模式的频道 -
pubsub subcommand [argument [argument...]]:查看订阅与发布系统状态 -
publish channel message:将信息发送到指定的频道 -
subscribe channel [channel...]:订阅给定的一个或多个频道信息。 -
unsubscribe [channel [channel...]]:指退订给定的频道。
案例
先订阅后发布才能收到消息
-
一次性订阅多个频道从c1,c2,c3,subscribe c1 c2 c3
-
消息发布,向c2频道发布消息:publish c2 hello-redis
查看订阅者进程输出: -
订阅给定模式的频道,通配符* :psubscribe new*
-
发送消息:publish new1 redis-hello
收到消息截图: