对等连接异常?
我这个程序采用的是netty,此程序中有客户端和服务端。
程序思路:服务端接受消息后在利用客户端发给别的服务端。
出现问题:时不时会出现这个异常,也不知道是那报的异常,经过查资料此异常是由某一端断开,发生的连接异常 ,我的客户端做了断开重连 但是并没发现重连的log ,难道是上游的客户端断开,那我的服务端也不应该报异常啊。不是很清楚? 有知道的吗?或者提供解决思路。

客户端重连代码
@Component
public class BdspNettySocketClient {
    private Logger log = LoggerFactory.getLogger(BdspNettySocketClient.class);
    /**
     * 线程组
     */
    private NioEventLoopGroup eventExecutors;
    /**
     * 存放通道  BdspNettySocketClientInitializer中的通道为最新通道
     */
    public static final ConcurrentHashMap<String, Channel> mChannel = new ConcurrentHashMap<>();
    /**
     * 启动netty客户端
     */
    @PostConstruct
    public void start() {
        //初始化信息
        try {
            this.init();
        } catch (Exception e) {
            log.error("启动 netty 客户端出现异常", e);
        }
    }
    /**
     * 当Bean在容器销毁之前,调用被@PreDestroy注解的方法
     */
    @PreDestroy
    public void destroy() {
        this.eventExecutors.shutdownGracefully();
    }
    /**
     * 初始化nettyClient配置
     */
    private void init() {
        //每次都需要重置通道
        if (mChannel.get("channel") != null) {
            mChannel.clear();
        }
        this.eventExecutors = new NioEventLoopGroup(1);
        //配置客户端的各种信息
        Bootstrap bootstrap = new Bootstrap();
        //设置线程组
        bootstrap.group(eventExecutors);
        //设置通道 此通道是异步非堵塞
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        //初始化通道 其中在里面配置逻辑
        bootstrap.handler(new BdspNettySocketClientInitializer());
        //连接设备信息
        log.info("netty client start!");
 
        /**
         * 连接好的容器
         */
        ChannelFuture channelFuture = bootstrap.connect("", );
        //这事防止客户端掉线 掉线后重连
        channelFuture.addListener(new ConnectionListener());
    }
    public void send(String msg) {
        try {
            mChannel.get("channel").writeAndFlush(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
       /*     System.out.println(mChannel.get("channel"));
            System.out.println(mChannel.get("channel"));
            channelFuture.channel().writeAndFlush(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));*/
        } catch (Exception e) {
            log.error(this.getClass().getName().concat(".send has error"), e);
        }
    }
    public void send(Object msg) {
        try {
            mChannel.get("channel").writeAndFlush(msg);
        } catch (Exception e) {
            log.error(this.getClass().getName().concat(".send has error"), e);
        }
    }
}断开后进入重连
@Component
public class ConnectionListener implements ChannelFutureListener {
    private BdspNettySocketClient bdspNettySocketClient = new BdspNettySocketClient();
    /**
     * 处理第一次连接服务器是否成功的
     * @param channelFuture  the source {@link Future} which called this callback
     * @throws Exception
     */
    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        if (!channelFuture.isSuccess()) {
            final EventLoop loop = channelFuture.channel().eventLoop();
            loop.schedule(new Runnable() {
                @Override
                public void run() {
                    System.err.println("服务端链接不上,开始重连操作...");
                    bdspNettySocketClient.start( );
                }
            }, 2L, TimeUnit.SECONDS);
        } else {
            System.err.println("服务端链接成功...");
        }
    }
}回复
1个回答

test
2024-06-28
这个应该是你的客户端断开连接了,客户端怎么做的断开重连,贴下代码看看
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容
