commit 6fd02ba9f8e7f77ef481e72a9f45a93dd1bf41d0 Author: yyboo Date: Tue Aug 12 10:07:18 2025 +0800 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e98d022 --- /dev/null +++ b/README.md @@ -0,0 +1,123 @@ +# NSQ 服务安装指南 + +本目录包含了为NSQ服务创建systemd服务文件的配置,用于设置开机自启动。 + +## 文件说明 + +- `nsqlookupd.service` - NSQ查找服务配置 +- `nsqd.service` - NSQ守护进程服务配置 +- `nsqadmin.service` - NSQ管理界面服务配置 +- `install-services.sh` - 自动安装脚本 + +## 安装步骤 + +### 方法1:使用自动安装脚本(推荐) + +1. 确保二进制文件已放置在 `/usr/share/nsq/` 目录下 +2. 给脚本添加执行权限: + ```bash + chmod +x install-services.sh + ``` +3. 以root权限运行安装脚本: + ```bash + sudo ./install-services.sh + ``` + +### 方法2:手动安装 + +1. 创建nsq用户(如果不存在): + ```bash + sudo useradd -r -s /bin/false -d /var/lib/nsq nsq + ``` + +2. 创建必要的目录: + ```bash + sudo mkdir -p /var/lib/nsq /var/log/nsq + sudo chown -R nsq:nsq /var/lib/nsq /var/log/nsq + ``` + +3. 复制服务文件到systemd目录: + ```bash + sudo cp *.service /etc/systemd/system/ + ``` + +4. 重新加载systemd配置: + ```bash + sudo systemctl daemon-reload + ``` + +5. 启用服务开机自启动: + ```bash + sudo systemctl enable nsqlookupd.service + sudo systemctl enable nsqd.service + sudo systemctl enable nsqadmin.service + ``` + +## 服务管理 + +### 启动服务 +```bash +sudo systemctl start nsqlookupd nsqd nsqadmin +``` + +### 停止服务 +```bash +sudo systemctl stop nsqlookupd nsqd nsqadmin +``` + +### 重启服务 +```bash +sudo systemctl restart nsqlookupd nsqd nsqadmin +``` + +### 查看服务状态 +```bash +sudo systemctl status nsqlookupd nsqd nsqadmin +``` + +### 查看服务日志 +```bash +# 查看nsqlookupd日志 +sudo journalctl -u nsqlookupd -f + +# 查看nsqd日志 +sudo journalctl -u nsqd -f + +# 查看nsqadmin日志 +sudo journalctl -u nsqadmin -f +``` + +## 服务配置说明 + +### nsqlookupd.service +- 默认端口:TCP 4160, HTTP 4161 +- 依赖:网络服务 +- 自动重启:是 + +### nsqd.service +- 默认端口:TCP 4150, HTTP 4151 +- 依赖:nsqlookupd服务 +- 自动重启:是 +- 启动参数:`--lookupd-tcp-address=127.0.0.1:4160` + +### nsqadmin.service +- 默认端口:HTTP 4171 +- 依赖:nsqlookupd服务 +- 自动重启:是 +- 启动参数:`--lookupd-http-address=127.0.0.1:4161` + +## 注意事项 + +1. 确保二进制文件路径正确(`/usr/share/nsq/`) +2. 服务将以`nsq`用户身份运行,确保该用户有适当的权限 +3. 服务启动顺序:nsqlookupd → nsqd → nsqadmin +4. 所有服务都配置了自动重启功能 +5. 日志输出到systemd journal + +## 故障排除 + +如果服务启动失败,请检查: +1. 二进制文件是否存在且有执行权限 +2. 端口是否被占用 +3. 用户权限是否正确 +4. 查看服务日志:`journalctl -u 服务名 -f` \ No newline at end of file diff --git a/bin/nsq_stat b/bin/nsq_stat new file mode 100755 index 0000000..7cac234 Binary files /dev/null and b/bin/nsq_stat differ diff --git a/bin/nsq_tail b/bin/nsq_tail new file mode 100755 index 0000000..4dfb679 Binary files /dev/null and b/bin/nsq_tail differ diff --git a/bin/nsq_to_file b/bin/nsq_to_file new file mode 100755 index 0000000..ed3f697 Binary files /dev/null and b/bin/nsq_to_file differ diff --git a/bin/nsq_to_http b/bin/nsq_to_http new file mode 100755 index 0000000..a4cca02 Binary files /dev/null and b/bin/nsq_to_http differ diff --git a/bin/nsq_to_nsq b/bin/nsq_to_nsq new file mode 100755 index 0000000..9593148 Binary files /dev/null and b/bin/nsq_to_nsq differ diff --git a/bin/nsqadmin b/bin/nsqadmin new file mode 100755 index 0000000..2d3094f Binary files /dev/null and b/bin/nsqadmin differ diff --git a/bin/nsqd b/bin/nsqd new file mode 100755 index 0000000..af22043 Binary files /dev/null and b/bin/nsqd differ diff --git a/bin/nsqd.dat b/bin/nsqd.dat new file mode 100644 index 0000000..b143960 --- /dev/null +++ b/bin/nsqd.dat @@ -0,0 +1 @@ +{"topics":[{"name":"core.device.online","paused":false,"channels":[{"name":"default","paused":false},{"name":"DeviceManagement","paused":false}]},{"name":"core.device.offline","paused":false,"channels":[{"name":"DeviceManagement","paused":false}]},{"name":"core.device.alarm","paused":false,"channels":[{"name":"default","paused":false},{"name":"DeviceManagement","paused":false}]}],"version":"1.3.0"} \ No newline at end of file diff --git a/bin/nsqlookupd b/bin/nsqlookupd new file mode 100755 index 0000000..3a2b48b Binary files /dev/null and b/bin/nsqlookupd differ diff --git a/bin/to_nsq b/bin/to_nsq new file mode 100755 index 0000000..89e5b24 Binary files /dev/null and b/bin/to_nsq differ diff --git a/install-services.sh b/install-services.sh new file mode 100644 index 0000000..ab7c041 --- /dev/null +++ b/install-services.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# NSQ服务安装脚本 +# 此脚本将安装NSQ服务并设置开机自启动 + +echo "开始安装NSQ服务..." + +# 检查是否为root用户 +if [ "$EUID" -ne 0 ]; then + echo "请使用root权限运行此脚本" + exit 1 +fi + + +# 拷贝二进制文件 +echo "拷贝二进制文件..." +# 创建nsq目录 +if [ ! -d "/usr/share/nsq" ]; then + echo "创建nsq目录..." + mkdir -p /usr/share/nsq +fi +cp ./bin/nsqlookupd /usr/share/nsq/ +cp ./bin/nsqd /usr/share/nsq/ +cp ./bin/nsqadmin /usr/share/nsq/ + + +# 复制服务文件到systemd目录 +echo "安装服务文件..." +cp nsqlookupd.service /etc/systemd/system/ +cp nsqd.service /etc/systemd/system/ +cp nsqadmin.service /etc/systemd/system/ + + +# 重新加载systemd配置 +echo "重新加载systemd配置..." +systemctl daemon-reload + +# 启用服务开机自启动 +echo "启用服务开机自启动..." +systemctl enable nsqlookupd.service +systemctl enable nsqd.service +systemctl enable nsqadmin.service + + +echo "启动服务..." +systemctl restart nsqlookupd nsqd nsqadmin \ No newline at end of file diff --git a/nsqadmin.service b/nsqadmin.service new file mode 100644 index 0000000..264fe6a --- /dev/null +++ b/nsqadmin.service @@ -0,0 +1,14 @@ +[Unit] +Description=NSQ Admin Web Interface +After=network.target nsqlookupd.service +Requires=nsqlookupd.service + +[Service] +User=root +WorkingDirectory=/usr/share/nsq +ExecStart=/usr/share/nsq/nsqadmin --lookupd-http-address=127.0.0.1:4161 +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/nsqd.service b/nsqd.service new file mode 100644 index 0000000..ee68ce3 --- /dev/null +++ b/nsqd.service @@ -0,0 +1,14 @@ +[Unit] +Description=NSQ Daemon +After=network.target nsqlookupd.service +Requires=nsqlookupd.service + +[Service] +User=root +WorkingDirectory=/usr/share/nsq +ExecStart=/usr/share/nsq/nsqd --lookupd-tcp-address=127.0.0.1:4160 +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/nsqlookupd.service b/nsqlookupd.service new file mode 100644 index 0000000..6bcc928 --- /dev/null +++ b/nsqlookupd.service @@ -0,0 +1,13 @@ +[Unit] +Description=NSQ Lookup Daemon +After=network.target + +[Service] +User=root +WorkingDirectory=/usr/share/nsq +ExecStart=/usr/share/nsq/nsqlookupd +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target \ No newline at end of file