Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 知乎专栏 | 视频教程 | About

34.15. Pub/Sub 订阅与发布

redis 提供基本的MQ 功能,下面我们做一个演示

开启第一个终端窗口,订阅first second

				$ redis-cli
				redis 127.0.0.1:6379> SUBSCRIBE first second
				Reading messages... (press Ctrl-C to quit)
				1) "subscribe"
				2) "first"
				3) (integer) 1
				1) "subscribe"
				2) "second"
				3) (integer) 2
			

开启第二个终端窗口,分别发送first second

				$ redis-cli
				redis 127.0.0.1:6379> PUBLISH second Hello
				(integer) 1
				redis 127.0.0.1:6379> PUBLISH first Helloworld!!!
				(integer) 1
				redis 127.0.0.1:6379> quit
			

现在切换到第一个终端窗口,应该能够看到发送过来的字符串

				$ redis-cli
				redis 127.0.0.1:6379> SUBSCRIBE first second
				Reading messages... (press Ctrl-C to quit)
				1) "subscribe"
				2) "first"
				3) (integer) 1
				1) "subscribe"
				2) "second"
				3) (integer) 2

				1) "message"
				2) "second"
				3) "Hello"

				1) "message"
				2) "first"
				3) "Helloworld!!!"