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开发过程中遇到的问题,也希望大家可以分享实际过程中的问题。