欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Error starting ApplicationContext. To display the conditions report re-run your application with de

程序员文章站 2022-01-30 20:46:52
...

netty9092端口号重复,改为9096就好了

package com.reachauto.vspcloud.common.netty.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class TimeServer {

    public TimeServer(){}

    public void bind(InetSocketAddress address) {

        // 配置服务端的 NIO 线程池,用于网络事件处理,实质上他们就是 Reactor 线程组
        // bossGroup 用于服务端接受客户端连接,workerGroup 用于进行 SocketChannel 网络读写
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            // ServerBootstrap 是 Netty 用于启动 NIO 服务端的辅助启动类,用于降低开发难度
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(address)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childHandler(new ChildChannelHandler());


            ServerBootstrap sb = new ServerBootstrap();
            sb.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .localAddress(9092)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .childHandler(
                    new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch){
                            ch.pipeline().addLast(new HealthExaminationHandler());
                        }
                    }
                );


            // 服务器启动辅助类配置完成后,调用 bind 方法绑定监听端口,调用 sync 方法同步等待绑定操作完成
            ChannelFuture f = b.bind(address).sync();
            ChannelFuture fb = sb.bind(9092).sync();

            if (log.isInfoEnabled()) {
                log.info(Thread.currentThread().getName() + ",服务器开始监听端口,等待客户端连接.........");
            }
            // 下面会进行阻塞,等待服务器连接关闭之后 main 方法退出,程序结束

            f.channel().closeFuture().sync();
            fb.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            if (log.isErrorEnabled()) {
                log.error("TimeServer-bind报错:" + e);
            }
        } finally {
            // 优雅退出,释放线程池资源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    private class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
        private ByteBuf[] lineDelimiter() {
            return new ByteBuf[]{Unpooled.wrappedBuffer(new byte[]{'@', '!', 13, 10})};
        }

        @Override
        protected void initChannel(SocketChannel arg0) {
            if (log.isInfoEnabled()) {
                log.info("初始化通道.........." + arg0.remoteAddress());
            }
            //改为三分钟心跳
            arg0.pipeline().addLast(new IdleStateHandler(3, 0, 0, TimeUnit.MINUTES));
            arg0.pipeline().addLast(new DelimiterBasedFrameDecoder(8192, lineDelimiter()));
            arg0.pipeline().addLast(new TimeServerHandler());
        }
    }

}