nsq多播分发和负载均衡实验

5 篇文章 0 订阅

什么是nsq?请参考实时分布式消息平台nsq

本地如何搭建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

执行结果如下:

[nsqlookupd] 2018/12/15 15:22:49.916366 nsqlookupd v1.0.0-compat (built w/go1.9.1)
[nsqlookupd] 2018/12/15 15:22:49.917850 TCP: listening on [::]:4160
[nsqlookupd] 2018/12/15 15:22:49.917964 HTTP: listening on [::]:4161

可以看出,nsqlookupd默认使用4160端口监听tcp连接,使用4161端口监听http连接。

2.在第二个shell中,启动nsqd

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

执行结果如下:

[nsqd] 2018/12/15 15:56:34.115489 nsqd v1.0.0-compat (built w/go1.9.1)
[nsqd] 2018/12/15 15:56:34.115586 ID: 516
[nsqd] 2018/12/15 15:56:34.115879 NSQ: persisting topic/channel metadata to nsqd.dat
[nsqd] 2018/12/15 15:56:34.116807 TCP: listening on [::]:4150
[nsqd] 2018/12/15 15:56:34.116892 HTTP: listening on [::]:4151
[nsqd] 2018/12/15 15:56:34.116963 LOOKUP(127.0.0.1:4160): adding peer
[nsqd] 2018/12/15 15:56:34.116970 LOOKUP connecting to 127.0.0.1:4160
[nsqlookupd] 2018/12/15 15:56:34.118144 TCP: new client(127.0.0.1:49332)
[nsqlookupd] 2018/12/15 15:56:34.118261 CLIENT(127.0.0.1:49332): desired protocol magic '  V1'
[nsqlookupd] 2018/12/15 15:56:34.120412 CLIENT(127.0.0.1:49332): IDENTIFY Address:wangfeideMacBook-Pro.local TCP:4150 HTTP:4151 Version:1.0.0-compat
[nsqlookupd] 2018/12/15 15:56:34.120473 DB: client(127.0.0.1:49332) REGISTER category:client key: subkey:
[nsqd] 2018/12/15 15:56:34.120888 LOOKUPD(127.0.0.1:4160): peer info {TCPPort:4160 HTTPPort:4161 Version:1.0.0-compat BroadcastAddress:wangfeideMacBook-Pro.local}
[nsqd] 2018/12/15 15:56:49.119924 LOOKUPD(127.0.0.1:4160): sending heartbeat
[nsqlookupd] 2018/12/15 15:56:49.120154 CLIENT(127.0.0.1:49332): pinged (last ping 14.999743s)
[nsqd] 2018/12/15 15:57:04.117373 LOOKUPD(127.0.0.1:4160): sending heartbeat
[nsqlookupd] 2018/12/15 15:57:04.117563 CLIENT(127.0.0.1:49332): pinged (last ping 14.997414s)

可以看出,nsqd默认使用4150端口监听tcp连接,使用4151端口监听http连接。

3.在第三个shell中,启动nsqadmin

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

执行结果如下:

[nsqadmin] 2018/12/15 15:54:15.803470 nsqadmin v1.0.0-compat (built w/go1.9.1)
[nsqadmin] 2018/12/15 15:54:15.803839 HTTP: listening on [::]:4171

在浏览器中,打开链接http://127.0.0.1:4171/,查看nsqadmin用户界面,观察统计数据。

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中,发布几条消息

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

9.验证数据

$ cd /tmp
$ ls nsq*/
$ 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中,发布几条消息

$ curl -d 'topic=test&cmd=action1' 'http://127.0.0.1:4151/pub?topic=test' 
$ curl -d 'topic=test&cmd=action2' 'http://127.0.0.1:4151/pub?topic=test' 
$ curl -d 'topic=test&cmd=action3' 'http://127.0.0.1:4151/pub?topic=test' 
$ curl -d 'topic=test&cmd=action1' 'http://127.0.0.1:4151/pub?topic=test' 
$ curl -d 'topic=test&cmd=action2' 'http://127.0.0.1:4151/pub?topic=test' 
$ curl -d 'topic=test&cmd=action3' 'http://127.0.0.1:4151/pub?topic=test'

9.验证数据

可以看到,/happy/nsqPop1、/happy/nsqPop2、/happy/nsqPop3三个http请求的接口接收到的消息总数,就是第8步中发送的消息总数,并且每个接口都是接收到两条消息。

至此,验证完毕,实验结果符合理论预期。
 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值