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!!!"