springboot整合netty實(shí)現(xiàn)心跳檢測和自動重連
1. 引入依賴
在 pom.xml
中添加 Netty 和 Spring Boot 相關(guān)依賴。
<dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Netty Dependency --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.63.Final</version> </dependency> <!-- 其他相關(guān)依賴 --> </dependencies>
2. 配置 Netty 服務(wù)端
創(chuàng)建一個 Netty 服務(wù)器啟動類,配置心跳檢測機(jī)制。
import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.timeout.IdleStateHandler; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component public class NettyServer implements CommandLineRunner { private final int port = 8080; @Override public void run(String... args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new IdleStateHandler(5, 7, 10, TimeUnit.SECONDS)); ch.pipeline().addLast(new HeartbeatHandler()); } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } }
3. 實(shí)現(xiàn)心跳檢測處理器
創(chuàng)建一個 HeartbeatHandler
類處理心跳檢測。
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleState; public class HeartbeatHandler extends ChannelInboundHandlerAdapter { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.READER_IDLE) { System.out.println("讀空閑"); // 關(guān)閉連接 ctx.close(); } else if (event.state() == IdleState.WRITER_IDLE) { System.out.println("寫空閑"); } else if (event.state() == IdleState.ALL_IDLE) { System.out.println("讀寫空閑"); // 發(fā)送心跳包 ctx.writeAndFlush("ping\n"); } } else { super.userEventTriggered(ctx, evt); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }
4. 配置 Netty 客戶端
創(chuàng)建一個 Netty 客戶端啟動類,實(shí)現(xiàn)自動重連和心跳檢測。
import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.timeout.IdleStateHandler; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component public class NettyClient { private final String host = "localhost"; private final int port = 8080; private final int MAX_RETRY = 5; private int retry = 0; public void start() { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS)); ch.pipeline().addLast(new ClientHeartbeatHandler()); } }); connect(b); } catch (Exception e) { e.printStackTrace(); } } private void connect(Bootstrap b) { b.connect(host, port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { System.out.println("連接服務(wù)器成功"); } else { System.out.println("連接服務(wù)器失敗,嘗試重連"); retry++; if (retry < MAX_RETRY) { future.channel().eventLoop().schedule(() -> connect(b), 2 << retry, TimeUnit.SECONDS); } else { System.out.println("重連失敗次數(shù)達(dá)到最大,放棄連接"); } } } }); } }
5. 實(shí)現(xiàn)客戶端心跳處理器
創(chuàng)建一個 ClientHeartbeatHandler
類處理心跳包。
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleState; public class ClientHeartbeatHandler extends ChannelInboundHandlerAdapter { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent event = (IdleStateEvent) evt; if (event.state() == IdleState.WRITER_IDLE) { System.out.println("發(fā)送心跳包"); ctx.writeAndFlush("ping\n"); } } else { super.userEventTriggered(ctx, evt); } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("連接斷開,嘗試重連"); // 在這里實(shí)現(xiàn)重連邏輯 // 比如: ctx.channel().eventLoop().schedule(() -> connect(), 5, TimeUnit.SECONDS); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); } }
6. 啟動 Spring Boot 應(yīng)用
在 Spring Boot 的主類中啟動 Netty 服務(wù)器和客戶端。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PostConstruct; @SpringBootApplication public class NettySpringBootApplication { @Autowired private NettyServer nettyServer; @Autowired private NettyClient nettyClient; public static void main(String[] args) { SpringApplication.run(NettySpringBootApplication.class, args); } @PostConstruct public void startNetty() { new Thread(() -> { try { nettyServer.run(); } catch (Exception e) { e.printStackTrace(); } }).start(); new Thread(() -> nettyClient.start()).start(); } }
關(guān)鍵點(diǎn)總結(jié)
- 依賴引入:確保引入了 Spring Boot 和 Netty 的必要依賴。
- Netty 服務(wù)器配置:使用
ServerBootstrap
配置服務(wù)器端,包括心跳檢測處理。 - Netty 客戶端配置:使用
Bootstrap
配置客戶端,實(shí)現(xiàn)自動重連和心跳檢測。 - 心跳處理器:在服務(wù)器端和客戶端分別實(shí)現(xiàn)心跳檢測處理器,處理心跳包和連接超時。
- Spring Boot 集成:在 Spring Boot 應(yīng)用啟動時啟動 Netty 服務(wù)器和客戶端。
到此這篇關(guān)于springboot整合netty實(shí)現(xiàn)心跳檢測和自動重連的文章就介紹到這了,更多相關(guān)springboot 心跳檢測和自動重連內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 解決gateway報(bào)netty堆外內(nèi)存溢出io.netty.util.internal.OutOfDirectMemoryError
- springboot接入netty實(shí)現(xiàn)在線統(tǒng)計(jì)人數(shù)
- Netty如何自定義編碼解碼器
- 基于Netty實(shí)現(xiàn)WebSocket的常用處理器及區(qū)別解析
- 利用Netty+SpringBoot實(shí)現(xiàn)定時后端向前端推送數(shù)據(jù)
- SpringCloud整合Netty集群實(shí)現(xiàn)WebSocket的示例代碼
- io.netty項(xiàng)目UDP實(shí)現(xiàn)方式
相關(guān)文章
Java調(diào)用Zookeeper的實(shí)現(xiàn)步驟
本文主要介紹了Java調(diào)用Zookeeper的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Java中二叉樹的先序、中序、后序遍歷以及代碼實(shí)現(xiàn)
這篇文章主要介紹了Java中二叉樹的先序、中序、后序遍歷以及代碼實(shí)現(xiàn),一棵二叉樹是結(jié)點(diǎn)的一個有限集合,該集合或者為空,或者是由一個根節(jié)點(diǎn)加上兩棵別稱為左子樹和右子樹的二叉樹組成,需要的朋友可以參考下2023-11-11java服務(wù)器的簡單實(shí)現(xiàn)過程記錄
在線瀏覽網(wǎng)頁離不開服務(wù)器,用戶發(fā)出請求request,服務(wù)器做出響應(yīng)response,提供給用戶需要的頁面,這篇文章主要給大家介紹了關(guān)于java服務(wù)器簡單實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-11-11Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表
這篇文章主要介紹了Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09java、springboot?接口導(dǎo)出txt方式
這篇文章主要介紹了java、springboot?接口導(dǎo)出txt方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Eclipse創(chuàng)建java程序可執(zhí)行jar包教程
這篇文章主要為大家分享了Eclipse創(chuàng)建java程序可執(zhí)行jar包教程,具有一定的實(shí)用性和參考價值,感興趣的小伙伴們可以參考一下2016-05-05Java優(yōu)雅的處理金錢問題(BigDecimal)
本文主要介紹了Java優(yōu)雅的處理金錢問題(BigDecimal),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06向量數(shù)據(jù)庫之如何使用Elasticsearch實(shí)現(xiàn)向量數(shù)據(jù)存儲與搜索
這篇文章主要介紹了向量數(shù)據(jù)庫之如何使用Elasticsearch實(shí)現(xiàn)向量數(shù)據(jù)存儲與搜索,在向量函數(shù)的計(jì)算過程中,會對所有匹配的文檔進(jìn)行線性掃描,因此,查詢預(yù)計(jì)時間會隨著匹配文檔的數(shù)量線性增長,本文給大家講解的非常詳細(xì),需要的朋友參考下吧2023-06-06基于Spring Security實(shí)現(xiàn)對密碼進(jìn)行加密和校驗(yàn)
我們在入門案例中,其實(shí)已經(jīng)是一個非常簡單的認(rèn)證,但是用戶名是寫死的,密碼也需要從控制臺查看,很顯然實(shí)際中并不能這么做,下面的學(xué)習(xí)中,我們來實(shí)現(xiàn)基于內(nèi)存模型的認(rèn)證以及用戶的自定義認(rèn)證,密碼加密等內(nèi)容,需要的朋友可以參考下2024-07-07