Springboot整合Netty启动需要注意

1. Netty的同步方式阻塞Springboot主线程

    常规的demo级别的netty服务端的代码写法是这样的:

 try {
            serverBootstrap.group(bossGroup, workGroup)
                    .channel(NioServerSocketChannel.class)
                    //设置过滤器
                    .childHandler(new NettyHandler())
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture future = serverBootstrap.bind(SERVER_PORT).sync();
            future.channel().closeFuture().sync();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放掉所有资源包括创建的线程
            workGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }

当我们在该函数中直接调用下面的代码时:

channelFuture.channel().closeFuture().sync();

     Netty会进入无限循环之中,将会不再加载和扫描之后的类了。有可能你还需要mybatis、redis等,可能就扫描不到了。所以需要使用线程池来execute,这样才能让netty不阻塞Springboot。 

 

2. SpringBoot集成Netty时在Handler类中注入为null

     

public class RequestHandler extends SimpleChannelInboundHandler<Request> {
    @Autowired
    private UserService userService;
    ....
}

   userService注入后为nul,查找了一些资料,Springboot非controller使用@Autowired注解注入为null的问题(加@Component注解,再写个初始化函数),依旧无效。

  上面的代码更新为:

public class RequestHandler extends SimpleChannelInboundHandler<Request> {
    private static UserService userService;
    static {
        userService = SpringContextUtil.getBean(UserService.class);
    }
    ...
}

   这里提供一个getBean的工具类,代码如下:

@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringContextUtil.applicationContext == null) {
            SpringContextUtil.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

后边会继续更新netty开发过程中遇到的问题,也希望大家可以分享实际过程中的问题。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值