1. 程式人生 > >nsq多播分發和負載均衡實驗

nsq多播分發和負載均衡實驗

從NSQ的設計文件中得知,單個nsqd被設計為一次能夠處理多個流資料,NSQ中的資料流模型是由stream和consumer組成。Topic是一種獨特的stream,Channel是一個訂閱了給定Topic的consumer邏輯分組。NSQ的資料流模型結構如下圖所示:

從上圖可以看出,單個nsqd可以有多個Topic,每個Topic又可以有多個Channel。Channel能夠接收Topic所有訊息的副本,從而實現了多播分發;而Channel上的每個訊息被分發給它的一個訂閱者,從而實現負載均衡。所有這些東西,就組成了一個可以表示各種簡單和複雜拓撲結構的強大框架。下面對nsq的多播分發和負載均衡進行實驗。

一、實驗nsq多播分發

1.在第一個shell中,啟動nsqlookupd

$ nsqlookupd

2.在第二個shell中,啟動nsqd

$ nsqd --lookupd-tcp-address=127.0.0.1:4160  

3.在第三個shell中,啟動nsqadmin

$ nsqadmin --lookupd-http-address=127.0.0.1:4161  

4.在第四個shell中,釋出第一個訊息(同時建立topic)

$ curl -d 'hello world 1' 'http://127.0.0.1:4151/pub?topic=test' 

注意:在這一步中,可以指定channel的值,但是訊息依然會發送給所有訂閱topic的消費者。

5.在第五個shell中,使用nsq_to_file啟動一個client來接收訊息(訊息儲存目錄為/tmp/nsq1)

$ nsq_to_file --topic=test --channel=ch_test1 --output-dir=/tmp/nsq1 --lookupd-http-address=127.0.0.1:4161

6.在第六個shell中,使用nsq_to_file啟動一個client來接收訊息(訊息儲存目錄為/tmp/nsq2)

$ nsq_to_file --topic=test --channel=ch_test2 --output-dir=/tmp/nsq2 --lookupd-http-address=127.0.0.1:4161

7.在第七個shell中,使用nsq_to_file啟動一個client來接收訊息(訊息儲存目錄為/tmp/nsq2)

$ nsq_to_file --topic=test --channel=ch_test3 --output-dir=/tmp/nsq3 --lookupd-http-address=127.0.0.1:4161

8.在第四個shell中,釋出幾條訊息

  1. $ curl -d 'hello world 2' 'http://127.0.0.1:4151/pub?topic=test'
  2. $ curl -d 'hello world 3' 'http://127.0.0.1:4151/pub?topic=test'
  3. $ curl -d 'hello world 4' 'http://127.0.0.1:4151/pub?topic=test'

9.驗證資料

  1. $ cd /tmp
  2. $ ls nsq*/
  3. $ cat nsq*/*

轉到/tmp/nsq1目錄下,檢視符合test.*.log模式的檔案,發現其中已經有三條訊息,分別為“hello world 2”、“hello world 3”、“hello world 4”。檢視/tmp/nsq2和/tmp/nsq3目錄下符合test.*.log模式的檔案,亦是如此。但是,這些log檔案中並沒有“hello world 1”,因為訊息“hello world1”是在啟動client之前傳送的,所以沒有被client接收到。

二、實驗nsq負載均衡

1.在第一個shell中,啟動nsqlookupd

$ nsqlookupd

2.在第二個shell中,啟動nsqd

$ nsqd --lookupd-tcp-address=127.0.0.1:4160  

3.在第三個shell中,啟動nsqadmin

$ nsqadmin --lookupd-http-address=127.0.0.1:4161  

4.在第四個shell中,釋出第一個訊息(同時建立topic)

$ curl -d 'hello world 1' 'http://127.0.0.1:4151/pub?topic=test' 

注意:在這一步中,可以指定channel的值,但是訊息依然會發送給所有訂閱topic的消費者。

5.在第五個shell中,使用nsq_to_http啟動一個client來接收訊息(http介面的uri為/happy/nsqPop1)

$ nsq_to_http --topic=test --channel=ch_test --lookupd-http-address=127.0.0.1:4161 --post=http://local.nsq.cn:8080/happy/nsqPop1 --content-type=application/x-www-form-urlencoded

6. 在第六個shell中,使用nsq_to_http啟動一個client來接收訊息(http介面的uri為/happy/nsqPop2)

$ nsq_to_http --topic=test --channel=ch_test --lookupd-http-address=127.0.0.1:4161 --post=http://local.nsq.cn:8080/happy/nsqPop2 --content-type=application/x-www-form-urlencoded

7. 在第七個shell中,使用nsq_to_http啟動一個client來接收訊息(http介面的uri為/happy/nsqPop3)

$ nsq_to_http --topic=test --channel=ch_test --lookupd-http-address=127.0.0.1:4161 --post=http://local.nsq.cn:8080/happy/nsqPop3 --content-type=application/x-www-form-urlencoded

注意:/happy/nsqPop1、/happy/nsqPop2、/happy/nsqPop3三個http請求的介面執行的動作不能一樣,否則無法區分哪個介面具體接收了訊息,執行了指定動作。

8. 在第四個shell中,釋出幾條訊息

  1. $ curl -d 'topic=test&cmd=action1' 'http://127.0.0.1:4151/pub?topic=test'
  2. $ curl -d 'topic=test&cmd=action2' 'http://127.0.0.1:4151/pub?topic=test'
  3. $ curl -d 'topic=test&cmd=action3' 'http://127.0.0.1:4151/pub?topic=test'
  4. $ curl -d 'topic=test&cmd=action1' 'http://127.0.0.1:4151/pub?topic=test'
  5. $ curl -d 'topic=test&cmd=action2' 'http://127.0.0.1:4151/pub?topic=test'
  6. $ curl -d 'topic=test&cmd=action3' 'http://127.0.0.1:4151/pub?topic=test'

9.驗證資料

可以看到,/happy/nsqPop1、/happy/nsqPop2、/happy/nsqPop3三個http請求的介面接收到的訊息總數,就是第8步中傳送的訊息總數,並且每個介面都是接收到兩條訊息。

至此,驗證完畢,實驗結果符合理論預期。