SpringBoot整合Netty

来源:互联网 发布:网络最热门表情包 编辑:程序博客网 时间:2024/06/15 11:48

SpringBoot整合Netty

SpringBoot中使用Netty与spring中使用Netty没有差别,在Spring中使用Netty可以考虑Netty的启动时机,可以在Bean加载的时候启动,可以写一个自执行的函数启动,这里采用监听Spring容器的启动事件来启动Netty。
业务需求:

  1. Netty端口可以配置
  2. Netty监听事件进行启动
  3. 端口配置采用SpringBoot风格

下面是Netty的启动代码,只是一个样例

public void start() {        bootstrap.group(bossLoopGroup, workerLoopGroup).channel(NioServerSocketChannel.class)        .handler(new LoggingHandler(LogLevel.INFO))        .childHandler(new ChannelInitializer<SocketChannel>() {            @Override            protected void initChannel(SocketChannel socketChannel) throws Exception {                ChannelPipeline pipeline = socketChannel.pipeline();                if (getSslContext() != null) {                    pipeline.addLast(getSslContext().newHandler(socketChannel.alloc()));                }            }        });        try {            ChannelFuture sync = bootstrap.bind(getPort()).sync();            this.channel = sync.channel();            logger.info("Baymax RPC server start on port(s) : {}", getPort());        } catch (InterruptedException e) {            e.printStackTrace();        }    }

这里端口是可以配置的

Spring容器中内置事件有

  1. ContextStoppedEvent
  2. ContextRefreshEvent
  3. ContextStartedEvent
  4. ContextClosedEvent

事件的关系如下

Spring容器中事件关系

这里采取监听 ContextRefreshEvent 事件
事件监听代码如下

public class BaymaxRpcDeployedNotifier implements SmartApplicationListener {    private final static int BACHELOR = 110;    private ApplicationContext applicationContext;    private RpcServer rpcServer;    public BaymaxRpcDeployedNotifier(ApplicationContext applicationContext, RpcServer rpcServer) {        this.applicationContext = applicationContext;        this.rpcServer = rpcServer;    }    @Override    public boolean supportsEventType(Class<? extends ApplicationEvent> aClass) {        return aClass == ContextRefreshedEvent.class;    }    @Override    public boolean supportsSourceType(Class<?> aClass) {        return ApplicationContext.class.isAssignableFrom(aClass);    }    @Override    public void onApplicationEvent(ApplicationEvent applicationEvent) {        rpcServer.start();    }    @Override    public int getOrder() {        return BACHELOR;    }}

SmartApplicationListener 可以做事件监听流,以后在介绍

现在Netty可以根据Spring的启动进行启动,还差一个工作,将netty的端口配置添加到SpringBoot的配置中,SpringBoot中添加自定义配置代码如下

@ConfigurationProperties(prefix = "baymax.rpc.server")public class BaymaxNettyServerProperties {    private int port = 10220;    private List<ChannelHandler> channelHandlerList = new ArrayList<>();    public int getPort() {        return port;    }    public void setPort(int port) {        this.port = port;    }    public List<ChannelHandler> getChannelHandlerList() {        return channelHandlerList;    }    public void setChannelHandlerList(List<ChannelHandler> channelHandlerList) {        this.channelHandlerList = channelHandlerList;    }}

这里需要添加一个文件名字叫做spring.factories,内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.com.immortals.baymax.boot.BaymaxNettyServerProperties

最近缺点资源分,手头宽裕的支援点

到这里基本就结束了代码在SpringBootNetty直接下载就好,在父pom中把parent改成springboot,下面的依赖添加版本就好,否则不好使的。