瑞士军刀redis - 发布订阅

  • 角色
  • 模型
  • API
  • 发布订阅和消息队列

角色:

  • 发布者 publisher
  • 订阅者 subcriber
  • 频道

模型

  • redis server中有多个频道
  • 订阅者可以订阅多个频道
  • 发布者发布信息到其中一个频道
  • 无法获取消息的历史,

发布订阅

  • publish
  • unsubscribe
  • subscribe
  • 其他

publish 发布命令

publish channel message 
publish sohu:tv "helloworld"  
(integer)3 订阅者

subscribe 订阅

subscribe [channel ] # 一个或多个 
subscribe sohu:tv 
1)"subscribe"
2)"sohu:tv" 
3)(integer)1 
1) "message"
2)"sohu:tv"
3)"helloworld "

unsubscribe 取消订阅

unsubscribe #一个或多个 
unsubscribe sohu:tv 
1)"unsubscribe"
2) "sohu:tv" 
3) (integer) 0 

其他api

psubscribe[pattern...]:#订阅模式 
 punsubscribe[pattern...]#退订指定的模式 
 pubsub channels #列出至少有一个订阅者的频道  
 pubsub numsub [channel...]#列出给定频道的订阅者数量 
 pubsub numpat #列出被订阅模式的数量 

消息队列和发布订阅的区别和使用场景

消息队列 必然只有一个订阅者能get到
发布订阅 所有都可以订阅到

猜你喜欢

转载自blog.csdn.net/PYouLing123456789/article/details/83927263