问题遇到的现象和发生背景
因业务需要,我们netty服务端需要新监听一个端口号 8866,用来接收新报文格式的报文,测试环境正常,可投产以后偶发性的会给客户端报connection refused by remote host,十次有两次这种情况。
问题相关代码,请勿粘贴截图
public class App
{
public static void main( String[] args ) throws Exception
{
CodeServer codeServer = SpringBeanUtil.getBean("codeServer",CodeServer.class);
//用来监听新端口的报文
NewCodeServer newCodeServer = SpringBeanUtil.getBean("newCodeServer",NewCodeServer.class);
try {
new Thread(new Runnable() {
@Override
public void run() {
try {
codeServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
newCodeServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
NewCodeServer :
@Service("newCodeServer")
public class NewCodeServer {
private static final Logger logger = LoggerFactory.getLogger(NewCodeServer .class);
public NewCodeServer () {
}
@Autowired
private NewCodeServerInitializer newCodeServerInitializer;
public void start() throws Exception {
EventLoopGroup bossGroup =new NioEventLoopGroup();
EventLoopGroup work =new NioEventLoopGroup();
try{
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup,work).channel(NioServerSocketChannel.class).localAddress(8866)
.childHandler(newCodeServerInitializer);
ChannelFuture cf = b.bind().sync(); // 服务器异步创建绑定
logger.info(NewCodeServer.class + " started and listen on " + cf.channel().localAddress());
cf.channel().closeFuture().sync(); // 关闭服务器通道
}finally{
bossGroup.shutdownGracefully().sync();
work.shutdownGracefully().sync();
}
}
}
NewCodeServerInitializer :
@Service("newCodeServerInitializer")
public class NewCodeServerInitializer extends ChannelInitializer<SocketChannel>{
@Autowired
private NewCodeServerHandler newCodeServerHandler;
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline cp = ch.pipeline();
ByteBuf delimiter = Unpooled.copiedBuffer("/r/n".getByte());
cp.addLast( new DelimiterBasedFrameDecoder(10240,delimiter));
cp.addLast( new StringEncoder());
cp.addLast( new StringDecoder( ));
cp.addLast("chunkedWriteHandler", new ChunkedWriteHandler());
cp.addLast("NewCodeServerHandler ",newCodeServerHandler);
}
}
运行结果及报错内容
这个是报错现象:

我的解答思路和尝试过的方法
我原先以为是网络问题,可是网络抓包分析发现并不是;
网上说可能是连接数过多了,cat /proc/sys/net/ipv4/tcp_max_syn_backlog 服务器最大连接数:2048
当前连接数:

我想要达到的结果
希望能给点思路,已经愁的快秃顶了