1、增加TCP连接过程打印,以便查看连接失败原因

This commit is contained in:
weidong 2025-07-19 09:49:26 +08:00
parent cef333b56b
commit 87d6f6073d

View File

@ -54,7 +54,7 @@ public class TCPClient {
}
public void connect() {
logger.info("netty client starting");
logger.info("{}:{} tcp connecting...",host,port);
//启动客户端去连接服务器端
try {
ChannelFuture cf = bootstrap.connect(host, port);
@ -62,9 +62,10 @@ public class TCPClient {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
logger.info("{}:{} tcp connect failed",host,port);
//重连交给后端线程执行
future.channel().eventLoop().schedule(() -> {
logger.info("tcp client reconnect");
logger.info("{}:{} tcp client reconnect",host,port);
try {
connect();
} catch (Exception e) {
@ -72,7 +73,7 @@ public class TCPClient {
}
}, 5000, TimeUnit.MILLISECONDS);
} else {
logger.info("tcp client start success!");
logger.info("{}:{} tcp client start success!",host,port);
}
}
});
@ -80,7 +81,7 @@ public class TCPClient {
this.channel = cf.channel();
this.channel.closeFuture().sync();
} catch (Exception e) {
logger.error("netty client error:", e);
logger.error(host+":"+port+" tcp connect error:", e);
}
}