redis的發佈訂閱

簡介

進程間的消息通訊模式: 發送者(pub)發送消息,訂閱者(sub)接收消息

示例,訂閱多個

127.0.0.1:8686[1]> SUBSCRIBE c1 c2 c3
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "c1"
3) (integer) 1
1) "subscribe"
2) "c2"
3) (integer) 2
1) "subscribe"
2) "c3"
3) (integer) 3

    # 另一個客戶端經過c1頻道發送消息
    remoteSelf:1> publish c1 "hello c1"
    "1"

1) "message"
2) "c1"
3) "hello c1"

經過正則匹配訂閱多個

[root@izm5e2q95pbpe1hh0kkwoiz ~]# redis-cli -p 8686
127.0.0.1:8686> auth ****(密碼)
OK
127.0.0.1:8686> select 1
OK
127.0.0.1:8686[1]> PSUBSCRIBE new*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "new*"
3) (integer) 1

    # 發送訂閱信息
    remoteSelf:1>publish new1 "hello new1"
    "1"

1) "pmessage"
2) "new*"
3) "new1"
4)
相關文章
相關標籤/搜索