zoukankan      html  css  js  c++  java
  • nsq多播分发和负载均衡实验

    什么是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

    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步中发送的消息总数,并且每个接口都是接收到两条消息。

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

  • 相关阅读:
    [Bzoj2120]数颜色
    [Bzoj2049][Sdoi2008]Cave 洞穴勘测
    [2019上海网络赛F题]Rhyme scheme
    [2019上海网络赛J题]Stone game
    Codeforces Round #688 (Div. 2) C
    Educational Codeforces Round 99 (Rated for Div. 2) D
    Educational Codeforces Round 99 (Rated for Div. 2) B
    Codeforces Round #685 (Div. 2) D
    Codeforces Round #685 (Div. 2) C
    Codeforces Round #685 (Div. 2) B
  • 原文地址:https://www.cnblogs.com/mafeng/p/9597762.html
Copyright © 2011-2022 走看看