一、环境:
win10 简体中文版
二、软件下载:
https://nsq.io/deployment/installing.html
https://s3.amazonaws.com/bitly-downloads/nsq/nsq-1.2.0.windows-amd64.go1.12.9.tar.gz
三、软件解压:
将文件解压到一个目录下,形成如下的目录(我的示例):
D:\APPLY\NSQ
│
└─bin
│
│nsqadmin.exe
│nsqd.exe
└nsqlookupd.exe
四、运行
将D:\APPLY\NSQ\BIN目录添加到PATH环境变量中
nsqlookupd
nsqd --lookupd-tcp-address=127.0.0.1:4160
nsqadmin --lookupd-http-address=127.0.0.1:4161
然后在浏览器中输入: http://127.0.0.1:4171
可以看到界面
五、以服务的形式运行(双nsqd,双nsqlookupd)
在bin目录下放置下面四个文件(使用gbk编码,不要使用utf-8):
1、reg_service.bat注册服务
@Echo Off
REM 以管理员运行
set now_str=%date:~0,4%-%date:~5,2%-%date:~8,2% %time%
echo cur dir: "%~dp0"
echo cur file:"%~f0"
echo cur time:"%now_str%"
set title_str=%~f0 %now_str%
title %title_str%
tasklist /v /fo csv | findstr /i /C:"%title_str%" | findstr /i 管理员 > "%~dp0..\null"
If errorlevel 1 (
echo "Please run with administrator privileges"
goto proc_end
)
mkdir "%~dp0..\data\nsqd01" 2> "%~dp0..\null"
mkdir "%~dp0..\data\nsqd02" 2> "%~dp0..\null"
echo create nsqlookupd01 service.
sc query nsqlookupd01 2 > "%~dp0..\null"
If errorlevel 1 (
SC create nsqlookupd01 binpath= "\"%~dp0.\nsqlookupd.exe\" -broadcast-address 192.168.121.64 -tcp-address 0.0.0.0:4160 -http-address 0.0.0.0:4161" DisplayName= "nsqlookupd01" start= demand
)
echo create nsqlookupd02 service.
sc query nsqlookupd02 2 > "%~dp0..\null"
If errorlevel 1 (
SC create nsqlookupd02 binpath= "\"%~dp0.\nsqlookupd.exe\" -broadcast-address 192.168.121.64 -tcp-address 0.0.0.0:4260 -http-address 0.0.0.0:4261" DisplayName= "nsqlookupd02" start= demand
)
echo create nsqd01 service.
SC query nsqd01 2 > "%~dp0..\null"
If errorlevel 1 (
SC create nsqd01 binpath= "\"%~dp0.\nsqd.exe\" -broadcast-address 192.168.121.64 -tcp-address 0.0.0.0:4150 -http-address 0.0.0.0:4151 -https-address 0.0.0.0:4152 -lookupd-tcp-address 127.0.0.1:4160 -lookupd-tcp-address 127.0.0.1:4260 -data-path \"%~dp0..\data\nsqd01\"" DisplayName= "nsqd01" depend= nsqlookupd01/nsqlookupd02 start= demand
)
echo create nsqd02 service.
SC query nsqd02 2 > "%~dp0..\null"
If errorlevel 1 (
SC create nsqd02 binpath= "\"%~dp0.\nsqd.exe\" -broadcast-address 192.168.121.64 -tcp-address 0.0.0.0:4250 -http-address 0.0.0.0:4251 -https-address 0.0.0.0:4252 -lookupd-tcp-address 127.0.0.1:4160 -lookupd-tcp-address 127.0.0.1:4260 -data-path \"%~dp0..\data\nsqd02\"" DisplayName= "nsqd02" depend= nsqlookupd01/nsqlookupd02 start= demand
)
echo create nsqadmin01 service.
SC query nsqadmin01 2 > "%~dp0..\null"
If errorlevel 1 (
SC create nsqadmin01 binpath= "\"%~dp0.\nsqadmin.exe\" -http-address 0.0.0.0:4171 -lookupd-http-address 127.0.0.1:4161 -lookupd-http-address 127.0.0.1:4261" DisplayName= "nsqadmin01" depend= nsqlookupd01/nsqlookupd02 start= demand
)
:proc_end
pause
echo Finish
2、unreg_service.bat注销服务
@Echo Off
REM 以管理员运行
set now_str=%date:~0,4%-%date:~5,2%-%date:~8,2% %time%
echo cur dir: "%~dp0"
echo cur file:"%~f0"
echo cur time:"%now_str%"
set title_str=%~f0 %now_str%
title %title_str%
tasklist /v /fo csv | findstr /i /C:"%title_str%" | findstr /i 管理员 > "%~dp0..\null"
If errorlevel 1 (
echo "Please run with administrator privileges"
goto proc_end
)
FOR %%i IN (nsqd01 nsqd02 nsqadmin01 nsqlookupd01 nsqlookupd02) DO (
sc query %%i 2 > "%~dp0..\null"
If errorlevel 1 (
echo The Service named %%i has been deleted!
) else (
SC delete %%i > "%~dp0..\null" && echo Deletion of service named %%i completed!
If errorlevel 1 echo Deletion of service named %%i error!
)
)
:proc_end
pause
echo Finish
3、start_service.bat启动服务
@Echo Off
REM 以管理员运行
set now_str=%date:~0,4%-%date:~5,2%-%date:~8,2% %time%
echo cur dir: "%~dp0"
echo cur file:"%~f0"
echo cur time:"%now_str%"
set title_str=%~f0 %now_str%
title %title_str%
tasklist /v /fo csv | findstr /i /C:"%title_str%" | findstr /i 管理员 > "%~dp0..\null"
If errorlevel 1 (
echo "Please run with administrator privileges"
goto proc_end
)
FOR %%i IN (nsqlookupd01 nsqlookupd02 nsqd01 nsqd02 nsqadmin01) DO (
sc query %%i|findstr -i STATE|findstr -i RUNNING > "%~dp0..\null"
If errorlevel 1 (
SC start %%i > "%~dp0..\null" && echo Start the service named %%i completed!
If errorlevel 1 echo Start the service named %%i error!
) else (
echo The Service named %%i is running!
)
)
:proc_end
pause
echo Finish
4、stop_service.bat停止服务
@Echo Off
REM 以管理员运行
set now_str=%date:~0,4%-%date:~5,2%-%date:~8,2% %time%
echo cur dir: "%~dp0"
echo cur file:"%~f0"
echo cur time:"%now_str%"
set title_str=%~f0 %now_str%
title %title_str%
tasklist /v /fo csv | findstr /i /C:"%title_str%" | findstr /i 管理员 > "%~dp0..\null"
If errorlevel 1 (
echo "Please run with administrator privileges"
goto proc_end
)
FOR %%i IN (nsqd01 nsqd02 nsqadmin01 nsqlookupd01 nsqlookupd02) DO (
sc query %%i|findstr -i STATE|findstr -i STOP > "%~dp0..\null"
If errorlevel 1 (
SC stop %%i > "%~dp0..\null" && echo Stop the service named %%i completed!
If errorlevel 1 echo Stop the service named %%i error!
) else (
echo The Service named %%i is stoped!
)
)
:proc_end
pause
echo Finish
5、技术分析:
(1)每一个bat文件开头的代码一样,是判断本程序是否使用了管理员权限
(2)获取本程序对应的窗口标题,使用本程序的全路径和系统当前时间作为标识。使用tasklist获取标题
set now_str=%date:~0,4%-%date:~5,2%-%date:~8,2% %time%
set title_str=%~f0 %now_str%
title %title_str%
tasklist /v /fo csv | findstr /i /C:"%title_str%"
(3)判断管理员权限,判断窗口标题是否含有“管理员”
set now_str=%date:~0,4%-%date:~5,2%-%date:~8,2% %time%
set title_str=%~f0 %now_str%
title %title_str%
tasklist /v /fo csv | findstr /i /C:"%title_str%" | findstr /i 管理员 > "%~dp0..\null"
If errorlevel 1 (
echo "Please run with administrator privileges"
goto proc_end
)
没有找到,则给出提示
(4)服务管理使用sc命令。
附录:
1、官方文档:
https://nsq.io/overview/quick_start.html
2、官方文档入门翻译,运行界面为window上的cmd
快速入门
下面将安装一个小型的nsq队列,逐步完成消息的生产、消费、并且将消息保存到磁盘。
-
按下面步骤进行安装。
-
打开一个cmd窗口,启动
nsqlookupd
,命令如下:D:\apply\nsq\bin> nsqlookupd
-
再打开一个cmd窗口,启动
nsqd
,命令如下:D:\apply\nsq\bin> nsqd --lookupd-tcp-address=127.0.0.1:4160
-
再打开一个cmd窗口,启动
nsqadmin
,命令如下:D:\apply\nsq\bin> nsqadmin --lookupd-http-address=127.0.0.1:4161
-
再打开一个cmd窗口,生产(产生)一条消息,(如果没有主题Topic,将会自动创建Topic。也可以在界面上手工创建Topic),执行如下命令:
curl -d "hello world 1" "http://127.0.0.1:4151/pub?topic=test"
(备注:window版本的curl下载路径: https://curl.haxx.se/windows/ )
-
最后,再打开一个cmd窗口,运行,命令如下:
D:\apply\nsq\bin> nsq_to_file --topic=test --output-dir=d:\temp --lookupd-http-address=127.0.0.1:4161
-
在第5步的窗口中产生更多的消息放入到队列中:
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"
-
要验证nsq工作是否正常,打开浏览器,在地址栏中输入
http://127.0.0.1:4171/
,通过nsqadmin
UI界面查看统计数据。也可以通过临时文件夹d:\temp\中的日志文件 (test.*.log
)来验证。
因为客户端软件nsq_to_file
链接的是nsqlookupd
,所以不知道具体的消息来源。虽然nsq_to_file
是定时查询,但是不会丢失消息。
[nsq_to_file] 2020/05/24 10:19:34.375575 INFO: [test/nsq_to_file] syncing 2 records to disk
2020/05/24 10:20:20 INF 1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2020/05/24 10:21:20 INF 1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2020/05/24 10:22:20 INF 1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2020/05/24 10:23:20 INF 1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2020/05/24 10:24:20 INF 1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
2020/05/24 10:25:20 INF 1 [test/nsq_to_file] querying nsqlookupd http://127.0.0.1:4161/lookup?topic=test
来源:oschina
链接:https://my.oschina.net/mengyoufengyu/blog/4288810