主要目的是搭建一个测试环境,同时使用了nodejs 客户端sdk
nsq 简单了解
nsqd:一个负责接收、排队、转发消息到客户端的守护进程
nsqlookupd:管理拓扑信息并提供最终一致性的发现服务的守护进程
nsqadmin:一套Web用户界面,可实时查看集群的统计数据和执行各种各样的管理任务
utilities:常见基础功能、数据流处理工具,如nsq_stat、nsq_tail、nsq_to_file、nsq_to_http、nsq_to_nsq、to_nsq
环境准备
具体参数的意思,参考官方文档
- docker-compose 文件
version: "3"
services:
ysdp-nsq-admin:
image: nsqio/nsq:v1.1.0
command: /nsqadmin -lookupd-http-address ysdp-nsq-nsqlookupd1:4161 -lookupd-http-address ysdp-nsq-nsqlookupd2:4261
ports:
- "4171:4171"
ysdp-nsq-nsqd1:
image: nsqio/nsq:v1.1.0
hostname: ysdp-nsq-nsqd1
command: /nsqd -tcp-address 0.0.0.0:4150 -data-path /usr/local/nsq/bin/data --http-address 0.0.0.0:4151 -lookupd-tcp-address ysdp-nsq-nsqlookupd1:4160 -lookupd-tcp-address ysdp-nsq-nsqlookupd2:4260 -broadcast-address ysdp-nsq-nsqd1
volumes:
- "./data1:/usr/local/nsq/bin/data"
ports:
- "4150:4150"
- "4151:4151"
ysdp-nsq-nsqd2:
image: nsqio/nsq:v1.1.0
hostname: ysdp-nsq-nsqd2
command: /nsqd -tcp-address 0.0.0.0:4250 -data-path /usr/local/nsq/bin/data -http-address 0.0.0.0:4251 -lookupd-tcp-address ysdp-nsq-nsqlookupd1:4160 -lookupd-tcp-address ysdp-nsq-nsqlookupd2:4260 -broadcast-address=ysdp-nsq-nsqd2
volumes:
- "./data2:/usr/local/nsq/bin/data"
ports:
- "4250:4250"
- "4251:4251"
ysdp-nsq-nsqd3:
image: nsqio/nsq:v1.1.0
hostname: ysdp-nsq-nsqd3
command: /nsqd -tcp-address 0.0.0.0:4350 -data-path /usr/local/nsq/bin/data --http-address 0.0.0.0:4351 -lookupd-tcp-address ysdp-nsq-nsqlookupd1:4160 -lookupd-tcp-address ysdp-nsq-nsqlookupd2:4260 -broadcast-address=ysdp-nsq-nsqd3
volumes:
- "./data3:/usr/local/nsq/bin/data"
ports:
- "4354:4350"
- "4355:4351"
ysdp-nsq-nsqlookupd1:
image: nsqio/nsq:v1.1.0
command: /nsqlookupd -http-address 0.0.0.0:4161 -tcp-address 0.0.0.0:4160 -broadcast-address ysdp-nsq-nsqlookupd1
ports:
- "4160:4160"
- "4161:4161"
ysdp-nsq-nsqlookupd2:
image: nsqio/nsq:v1.1.0
command: /nsqlookupd -http-address 0.0.0.0:4261 -tcp-address 0.0.0.0:4260 -broadcast-address ysdp-nsq-nsqlookupd2
ports:
- "4260:4260"
- "4261:4261"
启动集群
- 启动
docker-compose up -d
- 效果
使用
- 本地hosts 文件配置
因为基于nsqlookupd 的数据发现需要访问broadcast-address 暴露的地址,所以需要配置,同时都需要加上
127.0.0.1 ysdp-nsq-nsqd1
127.0.0.1 ysdp-nsq-nsqd2
127.0.0.1 ysdp-nsq-nsqd3
- nodejs 项目使用
初始化项目
yarn init -y
添加依赖
yarn add nsqjs
package.json
{
"name": "api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"nsqjs": "^0.12.1"
},
"scripts": {
"app":"node consumer"
}
}
调用代码
const nsq = require('nsqjs')
const reader = new nsq.Reader('sample_topic', 'test_channel', {
lookupdHTTPAddresses: ['127.0.0.1:4161','127.0.0.1:4261']
})
reader.connect()
reader.on('error',err => {
console.log(JSON.stringify(err))
})
reader.on('message', msg => {
console.log('Received message [%s]: %s', msg.id, msg.body.toString())
msg.finish()
})
测试
- 启动 nodejs 项目
yarn app
- 发送消息
curl -d "it really tied the room together" http://localhost:4151/pub?topic=sample_topic
- 效果
admin 效果
说明
以上是一个简单的测试,以及集群的搭建
参考资料
https://nsq.io/overview/design.html
https://github.com/rongfengliang/nsq-cluster-docker-compose