欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

redis的发布订阅

程序员文章站 2022-03-03 10:43:35
简介 示例,订阅多个 通过正则匹配订阅多个 ......

简介

进程间的消息通信模式: 发送者(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)