netty新监听的端口,偶发性报connection refused

问题遇到的现象和发生背景

因业务需要,我们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);
    }
}
运行结果及报错内容

这个是报错现象:

img

我的解答思路和尝试过的方法

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

img

我想要达到的结果

希望能给点思路,已经愁的快秃顶了

这样很正常吧,客户端有时候断开连接,由于网络问题,连接超时哈,服务端处理不了过多请求等。
服务端客户端通信做一个心跳检测,断链重连机制,以及连接池避免过多的资源消耗。忘对你有帮助

确认下客户端与服务端是不是都是使用的NIO,如果都是NIO的话,会产生很多Selector,而Selector在window上的实现是基于连接的,一个selector会占用两个本地端口
还有就是异步返回后回调

// ...
.childHandler(newCodeServerInitializer)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.TCP_NODELAY, true);

cf.channel().closeFuture().sync(); // 关闭服务器通道
放在finally块中。

指定netty线程数,调大一点,需要调整参数

出现Connection refused的问题原因-般有三种:
1.服务器的端口没有打开这种直 接就是-直会Connection refused,不会间歇出现,可以直接排除;
2.服务器的防火墙没有开白名单很多 跟外部对接的时候,是需要将公司出口ip加到对方防火墙白名单,这种也会直接Connection
refused,不会间歇出现,可以直接排除;
3.服务器上的backlog设置的太小,导致连接队列满了,服务器可能会报Connection refused, 或者Connecttion reset by peer,这个看服务
器上的连接队列满时的设置;
还有就是忘记写关闭通道channel().closeFuture().sync();

远程主机的连接被拒绝,可以参考一下这篇文章
https://blog.csdn.net/fmyzc/article/details/81389704

netty是你这么玩儿的么?瞎玩儿