1、生产者消费者模式(基于队列)
多个消费者同时监听一个队列,但是一个消息只能被最先抢到消息的消费者消费,即消息任务是一次性读取和处理。
1.1、生产者发布消息
127.0.0.1:6379> LPUSH channel1 msg1 #从管道的左侧写入
(integer) 1
127.0.0.1:6379> LPUSH channel1 msg2
(integer) 2
127.0.0.1:6379> LPUSH channel1 msg3
(integer) 3
127.0.0.1:6379> LPUSH channel1 msg4
(integer) 4
127.0.0.1:6379> LPUSH channel1 msg5
(integer) 5
1.2、消费者消费消息
127.0.0.1:6379> RPOP channel1 #从管道的右侧消费,用于消息的先进先出
"msg1"
127.0.0.1:6379> RPOP channel1
"msg2"
127.0.0.1:6379> RPOP channel1
"msg3"
127.0.0.1:6379> RPOP channel1
"msg4"
127.0.0.1:6379> RPOP channel1
"msg5"
127.0.0.1:6379> RPOP channel1
(nil)
2、发布者订阅模式(先订阅)
127.0.0.1:6379> SUBSCRIBE channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
1) "message"
2) "channel1"
3) "test1"
1) "message"
2) "channel1"
3) "test2"127.0.0.1:6379> PUBLISH channel1 test1
(integer) 1
127.0.0.1:6379> PUBLISH channel1 test2
(integer) 1# 取消订阅
127.0.0.1:6379> unsubscribe channel2
1) "unsubscribe"
2) "channel2"