Springboot集成Netty

添加依赖

<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.72.Final</version>
</dependency>

创建服务

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
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.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import java.net.InetSocketAddress;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Netty 服务端
 *
 * @author fangjiaxiaobai
 * @date 2022-01-07 23:37
 */
@Slf4j
@Component
public class RouterServer2 {

    /**
     * boss 线程组用于处理连接工作
     */
    private final EventLoopGroup boss = new NioEventLoopGroup();

    /**
     * work 线程组用于数据处理
     */
    private final EventLoopGroup work = new NioEventLoopGroup();

    @Resource
    private ChannelHandler childHandler;

    /**
     * SpringBoot 启动的时候 调用
     *
     * @throws InterruptedException 中断异常
     */
    @PostConstruct
    public void init() throws InterruptedException {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(boss, work)
            // 指定Channel 
            .channel(NioServerSocketChannel.class)
            //使用指定的端口设置套接字地址
            .localAddress(new InetSocketAddress(7788))
            //服务端可连接队列数,对应TCP/IP协议listen函数中backlog参数
            .option(ChannelOption.SO_BACKLOG, 1024)
            //设置TCP长连接,一般如果两个小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文
            .childOption(ChannelOption.SO_KEEPALIVE, true)
            //将小的数据包包装成更大的帧进行传送,提高网络的负载,即TCP延迟传输
            .childOption(ChannelOption.TCP_NODELAY, true)
            .childHandler(new ChannelInitializer<Channel>() { ④
                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ch.pipeline()
                        //添加编码器 ⓪
                        .addLast(new RouterMessageEncode())
                        //添加Netty 自带的 换行解码器(用来解决 沾包,拆包) ①
                        .addLast(new LineBasedFrameDecoder(1024))
                        //添加自定义的 解码器 ②
                        .addLast(new RouterMessageDecode())
                        //添加 接收消息的 处理器 ③
                        .addLast(new ServiceMessageReceiveHandler());
                }
            });
        ChannelFuture future = bootstrap.bind().sync();
        if (future.isSuccess()) {
            log.info("start router Server success on port 7788");
        }
    }

    /**
     * SpringBoot 销毁的时候 调用
     *
     * @throws InterruptedException 中断异常
     */
    @PreDestroy
    public void destory() throws InterruptedException {
        boss.shutdownGracefully().sync();
        work.shutdownGracefully().sync();
        log.info("关闭Netty");
    }
}

上一篇:消息中间


下一篇:web页面放到手机页面,缩放问题