SpringBoot集成Netty

SpringBoot集成netty

基础类实现 CommandLineRunner

也可以将netty用多线程启动

package cn.wxt.test;

import cn.wxt.test.test2.Test2Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class})
@SpringBootApplication
public class TestApplication implements CommandLineRunner {
    @Autowired
    private Test2Server server;

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        server.startup();
    }
}

netty Server端

package cn.wxt.test.test2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class Test2Server {
    private final EventLoopGroup boss = new NioEventLoopGroup();
    private final EventLoopGroup worker = new NioEventLoopGroup();
    //
    private static final Logger logger = LoggerFactory.getLogger(Test2Server.class);

    public void startup(){
        try {
            ServerBootstrap server = new ServerBootstrap();//启动类
            server.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    //BACKLOG用于构造服务端套接字ServerSocket对象,标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            sc.pipeline().addLast(new FixedLengthFrameDecoder(10));
                            sc.pipeline().addLast(new StringDecoder());
                            sc.pipeline().addLast(new Test2ServerHandler());
                        }
                    });
            ChannelFuture cf = server.bind(9092).sync();
            logger.info("server start");
            cf.channel().closeFuture().sync();
        }
        catch(Exception e) {
            e.printStackTrace();
        }finally {
            logger.info("server shutdown.");
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}

http传输过程
SpringBoot集成Netty_第1张图片

相关问题

关于 LEAK: ByteBuf.release() was not called before it’s garbage-collected

netty默认的分配bytebuff的方式是PooledByteBufAllocator,所以要手动回收,要不然会造成内存泄漏。

关于Netty的ByteBuff内存泄漏问题
https://www.jianshu.com/p/b9241e7a9eda

Netty内存分配ByteBuf
https://blog.csdn.net/qq_35729915/article/details/90761381

Netty之内存分配器ByteBufAllocator
https://blog.csdn.net/u011212394/article/details/103984870

你可能感兴趣的:(netty,SpringBoot,java)