keepTravel 2022-04-27 15:54 采纳率: 0%
浏览 436
已结题

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

我想要达到的结果

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

  • 写回答

8条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-04-27 16:24
    关注
    获得7.50元问题酬金

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

    评论

报告相同问题?

问题事件

  • 系统已结题 5月5日
  • 赞助了问题酬金50元 4月27日
  • 创建了问题 4月27日

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图