
nsq消息队列简介
1.消息队列
- 异步化
- 解耦
- 消除峰值
使用场景:
•业务系统触发短信发送
•任务处理类,先把任务请求存到消息队列中,然后后端开启多个应用程序从队列中取任务进行处理
2.nsq概述
nsq特点
•实时分布式消息平台
•分布式,去中心化
•支持水平扩展
•开箱即用,安装方便
•丰富的客户端支持,go/python
nsq官网
https://nsq.io/deployment/installing.html
nsq组件
•nsqlookup: 消费者查找nsqd
•nsqd:消息队列服务
•nsqadmin:webui
•nsq_to_file:dump工具

3.单服务器解耦

4.水平扩展

5.集群

6.启动服务
启动nsqlookupd
D:\nsq>bin\nsqlookupd.exe [nsqlookupd] 2020/03/31 00:19:44.791601 INFO: nsqlookupd v1.1.0 (built w/go1.10.3) [nsqlookupd] 2020/03/31 00:19:44.817605 INFO: TCP: listening on [::]:4160 [nsqlookupd] 2020/03/31 00:19:44.817605 INFO: HTTP: listening on [::]:4161监听
D:\nsq>bin\nsqd --lookupd-tcp-address=127.0.0.1:4160 [nsqd] 2020/03/31 00:21:48.139685 INFO: nsqd v1.1.0 (built w/go1.10.3) [nsqd] 2020/03/31 00:21:48.168681 INFO: ID: 471 [nsqd] 2020/03/31 00:21:48.168681 INFO: NSQ: persisting topic/channel metadata to nsqd.dat [nsqd] 2020/03/31 00:21:48.183680 INFO: TCP: listening on [::]:4150 [nsqd] 2020/03/31 00:21:48.184681 INFO: HTTP: listening on [::]:4151 [nsqd] 2020/03/31 00:21:48.184681 INFO: LOOKUP(127.0.0.1:4160): adding peer [nsqd] 2020/03/31 00:21:48.184681 INFO: LOOKUP connecting to 127.0.0.1:4160 [nsqd] 2020/03/31 00:21:48.186685 INFO: LOOKUPD(127.0.0.1:4160): peer info {TCPPort:4160 HTTPPort:4161 Version:1.1.0 BroadcastAddress:LPT4547}nsqadmin
D:\nsq>bin\nsqadmin --lookupd-http-address=127.0.0.1:4161 [nsqadmin] 2020/03/31 00:23:57.191388 INFO: nsqadmin v1.1.0 (built w/go1.10.3) [nsqadmin] 2020/03/31 00:23:57.231393 INFO: HTTP: listening on [::]:4171
7.nsq发送数据
没有channel消费时,消息暂存,有channel消费时,消息发送并删除
channel和topic都是动态创建
有多个channel时,每个channel会收到消息发送数据
curl -d "hello world 1" http://127.0.0.1:4151/pub?topic=test OK nsqd [nsqd] 2020/04/15 23:46:30.059044 INFO: TOPIC(test): created [nsqd] 2020/04/15 23:46:30.059044 INFO: CI: querying nsqlookupd http://LPT4547:4161/channels?topic=test [nsqd] 2020/04/15 23:46:30.059044 INFO: NSQ: persisting topic/channel metadata to nsqd.dat [nsqd] 2020/04/15 23:46:30.059044 INFO: LOOKUPD(127.0.0.1:4160): topic REGISTER test消费数据
bin\nsq_to_file.exe --topic=test --output-dir=tmp --lookupd-http-address=127.0.0.1:4161 2020/04/15 23:55:05 INF 1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test 2020/04/15 23:55:05 INF 1 [test/nsq_to_file] (LPT4547:4150) connecting to nsqd 2020/04/15 23:55:06 INFO: opening tmp/test.LPT4547.2020-04-15_23.log 2020/04/15 23:55:06 syncing 1 records to disk