Storm Worker端口冲突

本文介绍了当Storm任务启动过程中遇到Worker端口被占用的问题时的解决方案。通过调整内核参数,预留特定端口供Worker使用,避免与其他服务冲突。

 

           storm任务启动过程中一个worker启动失败,通过supervisor日志发现一直在尝试启动,最后定位到时worker的端口被其他进程占用了导致启动失败,尤其是当机器上有大量短连接时会增加worker端口被占的几率。

 

解决办法

  • 其他服务避开worker端口
  • 通过内核参数配置将storm使用到的端口配置成以监听的方式启动

 

三种查看随机端口范围

sysctl  net.ipv4.ip_local_port_range
net.ipv4.ip_local_port_range = 1024     65000


cat /etc/sysctl.conf |grep local
net.ipv4.ip_local_port_range = 1024 65000


cat /proc/sys/net/ipv4/ip_local_port_range
1024    65000

   如果storm使用的端口在这个范围内就有冲突的几率。

 

配置预留端口

 

echo "5710-5714,15710-15714" > /proc/sys/net/ipv4/ip_local_reserved_ports


shell> vim /etc/sysctl.conf
net.ipv4.ip_local_reserved_ports = 5710-5714,15710-15714
shell> sysctl -p
 

 

  net.ipv4.ip_local_reserved_ports描述

ip_local_reserved_ports - list of comma separated ranges Specify the ports which are reserved for known third-party applications. 

These ports will not be used by automatic port assignments (e.g. when calling connect() or bind() with port number 0). Explicit port allocation behavior is unchanged.

The format used for both input and output is a comma separated list of ranges (e.g. "1,2-4,10-10" for ports 1, 2, 3, 4 and 10). Writing to the file will clear all previously reserved ports and update the current list with the one given in the input.

Note that ip_local_port_range and ip_local_reserved_ports settings are independent and both are considered by the kernel when determining which ports are available for automatic port assignments.

You can reserve ports which are not in the current
ip_local_port_range, e.g.:
$ cat /proc/sys/net/ipv4/ip_local_port_range 32000        61000
$ cat /proc/sys/net/ipv4/ip_local_reserved_ports 8080,9148
although this is redundant. However such a setting is useful if later the port range is changed to a value that will include the reserved ports.
Default: Empty

 

 

 

 

 

 

在使用 PM2 监听(watch)模式运行 Node.js 应用时,端口冲突是一个常见问题。当多个实例尝试绑定到同一端口时,会出现 `EADDRINUSE` 错误,提示地址已被占用。 ### 端口冲突的原因 - **多实例启动**:PM2 在 watch 模式下检测到文件变化时会重启应用,但如果未正确关闭旧实例,可能会导致多个进程同时运行。 - **残留进程**:某些情况下,PM2 可能未能清理所有旧的子进程,造成端口仍被占用。 - **配置不当**:如果应用中硬编码了端口号,并且未启用负载均衡或集群模式下的端口动态分配,也可能引发冲突。 ### 解决方案 #### 1. 使用随机端口分配 在开发环境中,可以通过让操作系统分配随机可用端口来避免冲突。Node.js 的 `http` 模块支持传入 `0` 作为端口号,系统将自动选择一个空闲端口: ```javascript const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello from dynamic port!'); }); const server = app.listen(0, () => { const actualPort = server.address().port; console.log(`Server running on port ${actualPort}`); }); ``` #### 2. 清理残留进程 确保每次重启前旧进程都被正确终止。可以在 `ecosystem.config.js` 中设置 `killTimeout` 和 `restartDelay` 来优化进程清理逻辑: ```javascript module.exports = { apps: [{ name: 'my-app', script: 'app.js', watch: true, ignore_watch: ['node_modules', 'logs'], kill_timeout: 5000, restart_delay: 3000, env: { PORT: 3000 } }] }; ``` #### 3. 使用集群模式并指定不同端口范围 在集群模式下运行时,可以为每个工作进程指定不同的端口范围,避免冲突: ```javascript const cluster = require('cluster'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { for (let i = 0; i < numCPUs; i++) { cluster.fork({ PORT: 3000 + i }); } cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.process.pid} died`); }); } else { const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send(`Worker ${process.pid} serving on port ${process.env.PORT}`); }); app.listen(process.env.PORT, () => { console.log(`Worker ${process.pid} started on port ${process.env.PORT}`); }); } ``` #### 4. 自动检测并释放端口 使用脚本或工具自动查找并释放被占用的端口。例如,在 Linux/macOS 上可以使用以下命令查看和终止占用端口的进程: ```bash lsof -i :3000 kill -9 <PID> ``` 在 Windows 上: ```cmd netstat -ano | findstr :3000 taskkill /F /PID <PID> ``` #### 5. 合理配置 watch 模式 限制 watch 模式监控的目录,避免频繁重启导致状态混乱: ```javascript module.exports = { apps: [{ name: 'my-app', script: 'app.js', watch: true, ignore_watch: ['node_modules', '.git', 'logs'], // 减少不必要的重启 watch_options: { followSymlinks: false, usePolling: true, interval: 1000 } }] }; ``` 通过以上方法,可以有效解决 PM2 在 watch 模式下可能遇到的端口冲突问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值