java netty 客户端 断线重连报错

public boolean connect(EventLoopGroup eventLoopGroup) {
		ChannelFuture future = null;
		bootstrap = new Bootstrap();
		try {
//			eventLoopGroup = new NioEventLoopGroup();
			bootstrap.group(eventLoopGroup);
			// 同步非阻塞
			bootstrap.channel(NioSocketChannel.class);
			bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
//			// 字节缓冲区
//			bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
//			// 发送缓冲区
//			bootstrap.option(ChannelOption.SO_SNDBUF, 1024);
//			// 接收缓冲区
//			bootstrap.option(ChannelOption.SO_RCVBUF, 1024);
//			// 组合包
//			bootstrap.option(ChannelOption.TCP_NODELAY, true);
//			bootstrap.option(ChannelOption.SO_LINGER, 0);
//			bootstrap.handler(new ClientChannelInitializer());
			bootstrap.handler(new ChannelInitializer<Channel>() {
				@Override
				protected void initChannel(Channel channel) throws Exception {
					// TODO Auto-generated method stub
//					channel.pipeline().addLast(new LengthFieldPrepender(4));//int 定义包长度 在包前面写入 服务端需要提前读取4个字节长度
//					channel.pipeline().addLast(new ChannelHandler[] { new LengthFieldBasedFrameDecoder(10240, 0, 1) });
					channel.pipeline().addLast(new ChannelHandler[] { handler });
				}
			});
			bootstrap.remoteAddress(new InetSocketAddress("127.0.0.1", DistConfig.that.getDist_global_port()));
			future = bootstrap.connect();
			future.addListener(conn_handler);//连接失败走此处handler
			if(init_start) {
				init_start = false;
			}
			future.sync();
			MyMap dist = new MyMap();
			dist.put("line_name", DistConfig.that.getLine_name());
			dist.put("http_port", DistConfig.that.getHttp_port());
			dist.put("tcp_port", DistConfig.that.getTcp_port());
			GlobalServer.send(baseClientCmd.C_MSG_CONNECT, dist);// 直接发送连接信息
			
			return true;
		} catch (Exception e) {
			log.error("连接到全局服务失败!");
			e.printStackTrace();
//			eventLoopGroup.shutdownGracefully();
			
		}

		return false;
	}
@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		log.error("全局服务断开");
		final EventLoop eventLoop = ctx.channel().eventLoop();
		log.error("尝试重新连接...");
		client.connect(eventLoop);
		super.channelInactive(ctx);
	}
@Override
	public void operationComplete(ChannelFuture channelFuture) throws Exception {
		final EventLoop eventLoop = channelFuture.channel().eventLoop();
		if (channelFuture.isSuccess()) {
			return;
		}
		
		eventLoop.schedule(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				client.connect(eventLoop);
			}
		}, 10, TimeUnit.SECONDS);
	}
io.netty.util.concurrent.BlockingOperationException: DefaultChannelPromise@46929a0f(incomplete)
	at io.netty.util.concurrent.DefaultPromise.checkDeadLock(DefaultPromise.java:395)
	at io.netty.channel.DefaultChannelPromise.checkDeadLock(DefaultChannelPromise.java:159)
	at io.netty.util.concurrent.DefaultPromise.await(DefaultPromise.java:225)
	at io.netty.channel.DefaultChannelPromise.await(DefaultChannelPromise.java:131)
	at io.netty.channel.DefaultChannelPromise.await(DefaultChannelPromise.java:30)
	at io.netty.util.concurrent.DefaultPromise.sync(DefaultPromise.java:337)
	at io.netty.channel.DefaultChannelPromise.sync(DefaultChannelPromise.java:119)
	at io.netty.channel.DefaultChannelPromise.sync(DefaultChannelPromise.java:30)
	at org.linlinjava.litemall.client_server.client_netty.NettyClient.connect(NettyClient.java:80)
	at org.linlinjava.litemall.client_server.client_netty.ClientHandler.channelInactive(ClientHandler.java:55)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1429)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)
	at io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:947)
	at io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:822)
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:464)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:745)

重连报错

有可能是你的代码有问题,也有可能仅是客户端主动关闭了连接,导致服务端的写失败。
比如,你如果在浏览器里按住F5不停刷新,就会出现大量这样的错误;但用curl/wget高并发地做压测,却并没有这种问题。
如果无聊的用户拼命刷你导致服务端出现大量这种异常怎么办? 一是限流,二是让服务端在写数据之前判断一下channel是否已关
 

if (!channel.isConnected()) {  
    if (logger.isWarnEnabled()) {  
        logger.warn("Failed to write any response because the channel is not connected any more. Maybe the client has closed the connection? ");  
    }  
    return;  
} 

情况二、
在开发过程中,进行单元测试时,你的另一个项目在跑,占用了连接,造成nio读写操作失败,将另一个项目停止解决。

参考https://blog.csdn.net/qq_37192800/article/details/80016241

创建新的 NioEventLoopGroup 就会提示

2021-四月-23 15:53:04.460 ERROR [pool-10-thread-1] o.l.l.c.c.NettyClient - 
java.nio.channels.ClosedChannelException: null
 at io.netty.channel.AbstractChannel$AbstractUnsafe.ensureOpen(...)(Unknown Source)

 

用旧的就提示死锁。求大佬解决