近期做一些物聯(lián)網(wǎng)方面項(xiàng)目,使用到了tcp協(xié)議,之前公司做過socket短連接,網(wǎng)上找了一個(gè)簡單的demo,很早便學(xué)習(xí)到nio方面知識,學(xué)習(xí)了《netty從入門到精通》這本書,同時(shí)也根據(jù)網(wǎng)上視頻做了幾個(gè)demo,但學(xué)習(xí)不太深入,剛好物聯(lián)網(wǎng)項(xiàng)目,就直接使用netty,前期直接使用這個(gè)框架,但作為近十年開發(fā)的java程序員,沒有spring感覺總少了很多不便,后期改框架時(shí)想到了使用springboot整合netty代碼。
先說之前項(xiàng)目中做socket代碼怎么用spring管理的:利用springboot啟動后啟動socket服務(wù),是沒有被spring管理的
public class CouponApplication {
public static void main(String[] args) {
log.info("當(dāng)前版本為:couponmisserver-0.0.5-SNAPSHOT");
log.info("版本日期:2020-1-7");
ApplicationContext context = SpringApplication.run(CouponApplication.class, args);
//啟動stock服務(wù)
Integer port = Integer.valueOf(Config.getConfig().get("socketPort"));
SocketServer socketServer = new SocketServer();
socketServer.init(port, context);
}
}
網(wǎng)上一大神推薦的一個(gè)使用`CommandLineRunner `來加一個(gè)線程啟動netty
/**
* author: hejz
* data: 2022-5-9
*/
@SpringBootApplication
@Slf4j
@EnableScheduling
public class Application implements CommandLineRunner {
@Autowired
private NettyServer nettyServer;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
nettyServer.serverRun();
}
}
我目前也是這樣做的,這樣netty就被整合到springboot中去了,但是近期chatGPT非?;?,晚上無聊時(shí)我也拿它來玩時(shí)問到它這個(gè)問題,看這個(gè)大神怎么回答的:
Spring Boot 中可以通過創(chuàng)建自定義的
NettyServer
或NettyClient
類,并在其中實(shí)現(xiàn) Netty 的服務(wù)端或客戶端代碼,然后在 Spring Boot 的應(yīng)用程序中將其作為一個(gè) Bean 注入到 Spring 容器中。以下是一個(gè)簡單的示例代碼,演示了如何整合 Netty 到 Spring Boot 應(yīng)用程序中。首先,創(chuàng)建一個(gè)自定義的
NettyServer
類,用于實(shí)現(xiàn) Netty 的服務(wù)端代碼:
@Component
public class NettyServer {
@Value("${netty.server.port}")
private int port;
@PostConstruct
public void start() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new NettyServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
之前網(wǎng)上那個(gè)大神給我的建議,我在操作過程中junit測試時(shí)總要先把啟動netty那個(gè)給注釋掉,無疑chatGPT大神給我找到了另一種解決方案,而且比我用的要好的多,我直接使用其給的方案重新優(yōu)化了代碼。
一、啟動類不要動,還是原來的springboot啟動類,在 NettyServer
上使用@Component注解讓spring掃瞄管理,使用@PostConstruct注解直接運(yùn)行netty服務(wù)
@Component
@Slf4j
public class NettyServer {
@Autowired
private NettyServerInitializer nettyServerInitializer;
@Value("${nettyPort}")
private Integer nettyPort;
@PostConstruct
public void serverRun() {
//循環(huán)組接收連接,不進(jìn)行處理,轉(zhuǎn)交給下面的線程組
EventLoopGroup bossGroup = new NioEventLoopGroup();
//循環(huán)組處理連接,獲取參數(shù),進(jìn)行工作處理
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
//服務(wù)端進(jìn)行啟動類
ServerBootstrap serverBootstrap = new ServerBootstrap();
//使用NIO模式,初始化器等等
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(nettyServerInitializer);
//綁定端口
ChannelFuture channelFuture = serverBootstrap.bind(nettyPort).sync();
log.info("tcp服務(wù)器已經(jīng)啟動…………");
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
二、添加NettyServerInitializer類加上@Component注解讓spring掃瞄管理,使用@Autowired進(jìn)行依賴注入管理
@Component
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {
Logger log = LogManager.getLogger(NettyServerInitializer.class);
@Autowired
private NettyHandler nettyHandler;
//連接注冊,創(chuàng)建成功,會被調(diào)用
@Override
protected void initChannel(SocketChannel ch) throws Exception {
log.info("==================netty報(bào)告==================");
log.info("信息:有一客戶端鏈接到本服務(wù)端");
log.info("IP:{}", ch.remoteAddress().getAddress());
log.info("Port:{}", ch.remoteAddress().getPort());
log.info("通道id:{}", ch.id().toString());
log.info("==================netty報(bào)告完畢==================");
ChannelPipeline pipeline = ch.pipeline();
//定義讀寫空閑時(shí)間——(單位秒)
pipeline.addLast(new IdleStateHandler(180, 60,180));
//注冊攔截器
pipeline.addLast(nettyHandler);
}
}
三、其它NettyServerHandler類加上@Component注解讓spring掃瞄管理,使用@Autowired進(jìn)行依賴注入管理,但注意需要添加@ChannelHandler.Sharable注解,防止程序報(bào)錯(cuò)文章來源:http://www.zghlxwxcb.cn/news/detail-448387.html
/**
* @author:hejz 75412985@qq.com
* @create: 2023-01-24 22:51
* @Description: 注冊攔截器——每個(gè)設(shè)備都需要注冊
*/
@Component
@ChannelHandler.Sharable
@Slf4j
public class NettyHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Autowired
private DtuRegister dtuRegister;
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
//讀到的客戶端信息的邏輯處理(略)
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.getCause();
NettyServiceCommon.deleteKey(ctx.channel());
ctx.channel().close();
}
}
至此,springboot整合netty完美解決,這才是便于理解和操作的方式,感謝網(wǎng)上使用監(jiān)聽器啟動netty服務(wù)的那位大神,也感謝chatGPT大神給我提供另一種好的思路和方法文章來源地址http://www.zghlxwxcb.cn/news/detail-448387.html
到了這里,關(guān)于springboot整合netty的正確姿勢的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!