SpringBoot整合Netty的流程步驟
簡(jiǎn)介
Netty是一個(gè)基于Java的開源網(wǎng)絡(luò)應(yīng)用框架,它提供了高性能、異步事件驅(qū)動(dòng)的網(wǎng)絡(luò)編程能力。Netty旨在幫助開發(fā)者構(gòu)建高性能、高可靠性的網(wǎng)絡(luò)應(yīng)用程序。
Netty提供了簡(jiǎn)潔的API和豐富的功能,可以輕松處理各種網(wǎng)絡(luò)通信協(xié)議,如TCP、UDP、WebSocket等。它的設(shè)計(jì)理念是基于事件驅(qū)動(dòng)和回調(diào)機(jī)制,而不是傳統(tǒng)的線程模型,這使得它可以實(shí)現(xiàn)高并發(fā)、低延遲的網(wǎng)絡(luò)通信。
通過使用Netty,開發(fā)者可以方便地處理復(fù)雜的網(wǎng)絡(luò)通信邏輯,例如請(qǐng)求-響應(yīng)模式、長(zhǎng)連接、心跳檢測(cè)等。Netty提供了靈活的編解碼器和處理器,可以對(duì)網(wǎng)絡(luò)數(shù)據(jù)進(jìn)行高效的編解碼和處理。同時(shí),Netty還提供了可靠的錯(cuò)誤處理機(jī)制和事件機(jī)制,方便開發(fā)者進(jìn)行異常處理和擴(kuò)展。
實(shí)例
版本依賴
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);
}
/**
* 讀取完畢時(shí)
*
* @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();
}測(cè)試
先啟動(dòng)EchoServer,再啟動(dòng)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: 26BHttpServer (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();
}測(cè)試Http
訪問鏈接:http://127.0.0.1:8089
hello netty http coding
以上就是SpringBoot整合Netty的流程步驟的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合Netty的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot如何集成Netty
- SpringBoot集成netty實(shí)現(xiàn)websocket通信功能
- SpringBoot整合Netty+Websocket實(shí)現(xiàn)消息推送的示例代碼
- SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法
- springboot之springboot與netty整合方案
- springboot整合netty框架實(shí)現(xiàn)站內(nèi)信
- Springboot整合Netty自定義協(xié)議實(shí)現(xiàn)示例詳解
- springboot整合netty框架的方式小結(jié)
- Springboot+netty實(shí)現(xiàn)Web聊天室
- SpringBoot整合Netty服務(wù)端的實(shí)現(xiàn)示例
相關(guān)文章
springBoot使用JdbcTemplate代碼實(shí)例
這篇文章主要介紹了springBoot使用JdbcTemplate代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
SpringBoot整合TomCat實(shí)現(xiàn)本地圖片服務(wù)器代碼解析
這篇文章主要介紹了SpringBoot整合TomCat實(shí)現(xiàn)本地圖片服務(wù)器代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
java HashMap擴(kuò)容詳解及實(shí)例代碼
這篇文章主要介紹了java HashMap擴(kuò)容詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鳎ǚ椒ㄦ湥┑暮?jiǎn)單例子
這篇文章主要介紹了JAVA中實(shí)現(xiàn)鏈?zhǔn)讲僮鞯睦?模仿jQuery的方法鏈實(shí)現(xiàn),需要的朋友可以參考下2014-04-04
詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序
這篇文章主要介紹了詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
SpringBoot之@ConditionalOnProperty注解使用方法
在平時(shí)業(yè)務(wù)中,我們需要在配置文件中配置某個(gè)屬性來決定是否需要將某些類進(jìn)行注入,讓Spring進(jìn)行管理,而@ConditionalOnProperty能夠?qū)崿F(xiàn)該功能,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-05-05

