在工作中,使用netty来开发遇到了两个问题记录一下
启动问题
如果直接注册到容器中启动,如下代码所示,那么Netty会阻塞springboot,导致服务并未完全启动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package top.tangyh.lamp.msg.his.netty;
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import java.net.InetSocketAddress;
@Component @Slf4j public class NettyServerStart implements ApplicationRunner {
private static String HOST = "127.0.0.1"; private static Integer PORT = 8848; @Resource private NettyServer nettyServer;
@Override public void run(ApplicationArguments args) throws Exception { InetSocketAddress address = new InetSocketAddress(HOST, PORT); log.info("neety服务器启动地址: " + HOST + ":" + PORT); nettyServer.start(address); } }
|
解决办法就是异步处理,在启动方法添加注解@Async,并且在Spring Boot启动类上开启异步注解,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| @Override @Async public void run(ApplicationArguments args) throws Exception { InetSocketAddress address = new InetSocketAddress(HOST, PORT); log.info("neety服务器启动地址: " + HOST + ":" + PORT); nettyServer.start(address); } ------------------------------------- @SpringBootApplication @EnableDiscoveryClient @EnableTransactionManagement @ComponentScan({ UTIL_PACKAGE, BUSINESS_PACKAGE }) @EnableFeignClients(value = { UTIL_PACKAGE, BUSINESS_PACKAGE }) @Slf4j @EnableFormValidator @EnableAsync public class MsgServerApplication {
public static void main(String[] args) throws UnknownHostException { ConfigurableApplicationContext application = SpringApplication.run(MsgServerApplication.class, args); Environment env = application.getEnvironment(); log.info("\n----------------------------------------------------------\n\t" + "应用 '{}' 运行成功! 访问连接:\n\t" + "Swagger文档: \t\thttp://{}:{}/doc.html\n\t" + "数据库监控: \t\thttp://{}:{}/druid\n" + "----------------------------------------------------------", env.getProperty("spring.application.name"), InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port", "8080"), InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port", "8080")
); } }
|
依赖加载问题
在netty的handler类中无法注入依赖,这是因为初始化netty时,handler是new出来的没有托管给spring,所以无法依赖注入。
解决办法是:
1️⃣加@Component
1 2 3
| @Slf4j @Component public class NettyServerHandler extends ChannelInboundHandlerAdapter
|
2️⃣创建静态的handler静态对象
1
| private static NettyServerHandler nettyServerHandler;
|
3️⃣使用@PostConstruct
初始化
1 2 3 4
| @PostConstruct public void init() { nettyServerHandler = this; }
|
在使用时需要在依赖注入前加上handle
1
| nettyServerHandler.icuObsvRdClient.saveXt(recordDTO);
|