NSQ是一个基于Go语言的分布式实时消息队列中间件,它基于MIT开源协议发布,由bitly公司开源出来的一款简单易用的消息中间件,它有以下特点:
- 分布式,去中心化的拓扑结构,具有无单点故障、故障容错、高可用性以及能够保证消息的可靠传递的特征
- 可扩展,自带服务发现和负载均衡策略,可在线扩容缩容
- 运维友好,部署binary即可,无外部依赖,自带admin管理集群
- 高度集成,官方支持Go和Python的Client,社区也为大多数语言提供了Client
NSQ由以下3种守护进程组成:
- Nsqd
- 负责接收,排队和传送消息到clients的守护进程,nsqd在内存中维护topic和channel的队列,队列大小通过参数mem-queue-size配置,超过大小的消息会记录在磁盘中。另外,nsqd也可以独立运行,不过通常它是配合nsqlookupd一起使用,这样是为了在集群中可以让clients动态发现注册的nsqd。
- Nsqlookupd
- 管理集群拓扑信息的作为服务发现的守护进程,它可以部署多个实例,实例之间不互相通信且做到数据最终一致;它接受nsqd节点广播的topic和channel信息,clients通过查询nsqlookupd来发现指定topic的producer。它有两个接口:
- tcp接口,nsqd用它来广播
- http接口,clients用它来发现和管理
- Nsqadmin
- 一个Web UI来实时监控NSQ集群,并执行各种管理任务,比如暂停/清空/删除某个Topic/Channel,Metrics的意义可以参考nsqadmin
部署NSQ
下载解压NSQ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $ cd /data/ $ wget -c 'https://github.com/nsqio/nsq/releases/download/v1.2.1/nsq-1.2.1.linux-amd64.go1.16.6.tar.gz' $ tar xf nsq-1.2.1.linux-amd64.go1.16.6.tar.gz $ tree nsq-1.2.1.linux-amd64.go1.16.6 nsq-1.2.1.linux-amd64.go1.16.6 └── bin ├── nsqadmin ├── nsqd ├── nsqlookupd ├── nsq_stat ├── nsq_tail ├── nsq_to_file ├── nsq_to_http ├── nsq_to_nsq └── to_nsq 1 directory, 9 files $ mv nsq-1.2.1.linux-amd64.go1.16.6 nsq $ mkdir -p /data/nsq/data/{node1,node2} $ mkdir -p /data/nsq/logs
|
supervisor部署
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #!/bin/bash install_supervisor () { if [ ! -f "/etc/supervisord.conf" ]; then yum install -y python-setuptools easy_install "supervisor==3.3.5" if [ $? -ne 0 ]; then echo "supervisor安装失败" exit 1 fi echo_supervisord_conf > /etc/supervisord.conf echo -e "[include]\nfiles = /etc/supervisor/*.conf" >> /etc/supervisord.conf if [ ! -d "/etc/supervisord.conf" ]; then mkdir /etc/supervisor fi supervisord -c /etc/supervisord.conf else echo "supervisord.conf配置文件已经存在,请确认supervisord是否已经安装" fi } install_supervisor
|
nsq1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| $ cd /etc/supervisor $ nsqd1.conf [program:nsqd1] command=/data/nsq/bin/nsqd -broadcast-address=10.71.5.67 -http-address=10.71.5.67:4152 -node-id=1 -tcp-address=10.71.5.67:4153 -lookupd-tcp-address=10.71.5.67:4160 -lookupd-tcp-address=10.71.5.67:4163 -data-path=/data/nsq/data/node1 -mem-queue-size 100000 ;process_name=%(program_name)s numprocs=1 directory=/data/nsq/ umask=022 ;priority=999 autostart=false autorestart=true startsecs=5 startretries=3 exitcodes=0,2 stopsignal=TERM stopwaitsecs=10 stopasgroup=false killasgroup=false user=root redirect_stderr=true stdout_logfile=/data/nsq/logs/nsqd1.log stdout_logfile_maxbytes=10MB stdout_logfile_backups=10 stdout_capture_maxbytes=10MB stdout_events_enabled=false stderr_logfile=/data/nsq/logs/nsqd1_stderr.log stderr_logfile_maxbytes=10MB stderr_logfile_backups=10 stderr_capture_maxbytes=10MB stderr_events_enabled=false ;environment=A="1",B="2" serverurl=AUTO
|
nsq2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| $ cat nsqd2.conf [program:nsqd2] command=/data/nsq/bin/nsqd -broadcast-address=10.71.5.67 -http-address=10.71.5.67:4154 -node-id=2 -tcp-address=10.71.5.67:4155 -lookupd-tcp-address=10.71.5.67:4160 -lookupd-tcp-address=10.71.5.67:4163 -data-path=/data/nsq/data/node2 -mem-queue-size 100000 ;process_name=%(program_name)s numprocs=1 directory=/data/nsq/ umask=022 ;priority=999 autostart=false autorestart=true startsecs=5 startretries=3 exitcodes=0,2 stopsignal=TERM stopwaitsecs=10 stopasgroup=false killasgroup=false user=root redirect_stderr=true stdout_logfile=/data/nsq/logs/nsqd2.log stdout_logfile_maxbytes=10MB stdout_logfile_backups=10 stdout_capture_maxbytes=10MB stdout_events_enabled=false stderr_logfile=/data/nsq/logs/nsqd2_stderr.log stderr_logfile_maxbytes=10MB stderr_logfile_backups=10 stderr_capture_maxbytes=10MB stderr_events_enabled=false ;environment=A="1",B="2" serverurl=AUTO
|
部署Nsqlookupd
nsqlookup1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| $ cat nsqlookupd1.conf [program:nsqlookupd1] command=/data/nsq/bin/nsqlookupd -tcp-address=10.21.248.7:4160 -http-address=10.21.248.7:4161 -broadcast-address=10.21.248.7 ;process_name=%(program_name)s numprocs=1 directory=/data/nsq/ umask=022 ;priority=999 autostart=false autorestart=true startsecs=5 startretries=3 exitcodes=0,2 stopsignal=TERM stopwaitsecs=10 stopasgroup=false killasgroup=false user=root redirect_stderr=true stdout_logfile=/data/nsq/logs/nsqlookupd1.log stdout_logfile_maxbytes=10MB stdout_logfile_backups=10 stdout_capture_maxbytes=10MB stdout_events_enabled=false stderr_logfile=/data/nsq/logs/nsqlookupd1_stderr.log stderr_logfile_maxbytes=10MB stderr_logfile_backups=10 stderr_capture_maxbytes=10MB stderr_events_enabled=false ;environment=A="1",B="2" serverurl=AUTO
|
nsqlookup2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| $ cat nsqlookupd2.conf [program:nsqlookupd2] command=/data/nsq/bin/nsqlookupd -tcp-address=10.21.248.7:4163 -http-address=10.21.248.7:4162 -broadcast-address=10.21.248.7 ;process_name=%(program_name)s numprocs=1 directory=/data/nsq/ umask=022 ;priority=999 autostart=false autorestart=true startsecs=5 startretries=3 exitcodes=0,2 stopsignal=TERM stopwaitsecs=10 stopasgroup=false killasgroup=false user=root redirect_stderr=true stdout_logfile=/data/nsq/logs/nsqlookupd2.log stdout_logfile_maxbytes=10MB stdout_logfile_backups=10 stdout_capture_maxbytes=10MB stdout_events_enabled=false stderr_logfile=/data/nsq/logs/nsqlookupd2_stderr.log stderr_logfile_maxbytes=10MB stderr_logfile_backups=10 stderr_capture_maxbytes=10MB stderr_events_enabled=false ;environment=A="1",B="2" serverurl=AUTO
|
部署Nsqadmin
nsqadmin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| $ cat nsqadmin.conf [program:nsqadmin] command=/data/nsq/bin/nsqadmin --lookupd-http-address=10.21.248.7:4161 --lookupd-http-address=10.21.248.7:4162 ;process_name=%(program_name)s numprocs=1 directory=/data/nsq/ umask=022 ;priority=999 autostart=true autorestart=false startsecs=10 startretries=3 exitcodes=0,2 stopsignal=TERM stopwaitsecs=10 stopasgroup=false killasgroup=false user=root redirect_stderr=true stdout_logfile=/data/nsq/logs/nsqadmin.log stdout_logfile_maxbytes=10MB stdout_logfile_backups=10 stdout_capture_maxbytes=10MB stdout_events_enabled=false stderr_logfile=/data/nsq/logs/nsqadmin_stderr.log stderr_logfile_maxbytes=10MB stderr_logfile_backups=10 stderr_capture_maxbytes=10MB stderr_events_enabled=false ;environment=A="1",B="2" serverurl=AUTO
|
启动服务
1 2 3 4 5 6 7 8 9
| $ supervisorctl reread $ supervisorctl update $ supervisorctl start nsq1 nsq2 nsqlookupd1 nsqlookupd2 nsqadmin $ supervisorctl status |grep -i 'runn' nsqadmin RUNNING pid 210281, uptime 0:00:40 nsqd1 RUNNING pid 208306, uptime 0:13:09 nsqd2 RUNNING pid 208344, uptime 0:13:04 nsqlookupd1 RUNNING pid 208255, uptime 0:13:24 nsqlookupd2 RUNNING pid 208282, uptime 0:13:19
|
访问Nsqadmin
http://ip:port:4171
本文出自”Jack Wang Blog”:http://www.yfshare.vip/2022/05/07/部署nsq/