SpringBoot整合Netty的流程步驟
簡介
Netty是一個基于Java的開源網(wǎng)絡(luò)應(yīng)用框架,它提供了高性能、異步事件驅(qū)動的網(wǎng)絡(luò)編程能力。Netty旨在幫助開發(fā)者構(gòu)建高性能、高可靠性的網(wǎng)絡(luò)應(yīng)用程序。
Netty提供了簡潔的API和豐富的功能,可以輕松處理各種網(wǎng)絡(luò)通信協(xié)議,如TCP、UDP、WebSocket等。它的設(shè)計理念是基于事件驅(qū)動和回調(diào)機制,而不是傳統(tǒng)的線程模型,這使得它可以實現(xiàn)高并發(fā)、低延遲的網(wǎng)絡(luò)通信。
通過使用Netty,開發(fā)者可以方便地處理復(fù)雜的網(wǎng)絡(luò)通信邏輯,例如請求-響應(yīng)模式、長連接、心跳檢測等。Netty提供了靈活的編解碼器和處理器,可以對網(wǎng)絡(luò)數(shù)據(jù)進行高效的編解碼和處理。同時,Netty還提供了可靠的錯誤處理機制和事件機制,方便開發(fā)者進行異常處理和擴展。
實例
版本依賴
JDK17
SpringBoot 3.1.0
Netty 4.1.90.Final
引入依賴
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>springboot-netty</artifactId> <name>${project.artifactId}</name> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>17</java.version> <maven.version>17</maven.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 引入Netty依賴 --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.90.Final</version> </dependency> </dependencies>
EchoServer
// 創(chuàng)建Server端 EventLoopGroup workGroup = new NioEventLoopGroup(); final EchoServerHandler serverHandler = new EchoServerHandler(); try { ServerBootstrap b = new ServerBootstrap(); b.group(workGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(serverHandler); } }); // 綁定端口 ChannelFuture f = b.bind(8088).sync(); // 等待連接關(guān)閉 f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { // 關(guān)閉所有線程 workGroup.shutdownGracefully(); }
EchoServerHandler
@Sharable public class EchoServerHandler extends ChannelInboundHandlerAdapter { /** * 讀取 * * @param ctx * @param msg * @throws Exception */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ctx.write(msg); } /** * 讀取完畢時 * * @param ctx * @throws Exception */ @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } /** * 抓住異常 * * @param ctx * @param cause * @throws Exception */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } }
EchoClient
EventLoopGroup group = new NioEventLoopGroup(); EchoClientHandler clientHandler = new EchoClientHandler(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(clientHandler); } }); // 連接server端 ChannelFuture cf = b.connect("127.0.0.1", 8088).sync(); // 等待連接關(guān)閉 cf.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally { group.shutdownGracefully(); }
測試
先啟動EchoServer,再啟動EchoClient
18:36:39.872 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler -- [id: 0xe21b38be, L:/127.0.0.1:54127 - R:/127.0.0.1:8088] WRITE: 26B +-------------------------------------------------+ | 0 1 2 3 4 5 6 7 8 9 a b c d e f | +--------+-------------------------------------------------+----------------+ |00000000| 48 65 6c 6c 6f 20 4e 65 74 74 79 20 53 6f 63 6b |Hello Netty Sock| |00000010| 65 74 20 43 6f 64 65 69 6e 67 |et Codeing | +--------+-------------------------------------------------+----------------+ 18:36:39.872 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler -- [id: 0xe21b38be, L:/127.0.0.1:54127 - R:/127.0.0.1:8088] READ COMPLETE 18:36:42.882 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler -- [id: 0xe21b38be, L:/127.0.0.1:54127 - R:/127.0.0.1:8088] FLUSH 18:36:42.885 [nioEventLoopGroup-2-1] INFO io.netty.handler.logging.LoggingHandler -- [id: 0xe21b38be, L:/127.0.0.1:54127 - R:/127.0.0.1:8088] READ: 26B
HttpServer (HTTP服務(wù))
// 主從模式 EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { // HTTP 模式 ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpServerExpectContinueHandler()); p.addLast(new HttpServerHandler()); } }); ChannelFuture ch = b.bind(8089).sync(); System.out.println("Open Http Server : http://127.0.0.1:8089"); ch.channel().closeFuture().sync(); } catch (Exception ex) { ex.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); }
測試Http
訪問鏈接:http://127.0.0.1:8089
hello netty http coding
以上就是SpringBoot整合Netty的流程步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Netty的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot如何集成Netty
- SpringBoot集成netty實現(xiàn)websocket通信功能
- SpringBoot整合Netty+Websocket實現(xiàn)消息推送的示例代碼
- SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法
- springboot之springboot與netty整合方案
- springboot整合netty框架實現(xiàn)站內(nèi)信
- Springboot整合Netty自定義協(xié)議實現(xiàn)示例詳解
- springboot整合netty框架的方式小結(jié)
- Springboot+netty實現(xiàn)Web聊天室
- SpringBoot整合Netty服務(wù)端的實現(xiàn)示例
相關(guān)文章
SpringBoot整合TomCat實現(xiàn)本地圖片服務(wù)器代碼解析
這篇文章主要介紹了SpringBoot整合TomCat實現(xiàn)本地圖片服務(wù)器代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08

詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序

SpringBoot之@ConditionalOnProperty注解使用方法