SpringBoot整合Netty

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,下面的依赖添加版本就好,否则不好使的。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值