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開發過程中遇到的問題,也希望大家可以分享實際過程中的問題。