欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Netty的流程步驟

 更新時間:2023年09月20日 11:23:48   作者:fking86  
Netty是一個基于Java的開源網(wǎng)絡(luò)應(yīng)用框架,它提供了高性能、異步事件驅(qū)動的網(wǎng)絡(luò)編程能力,Netty旨在幫助開發(fā)者構(gòu)建高性能、高可靠性的網(wǎng)絡(luò)應(yīng)用程序,本文給大家詳細(xì)介紹了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)文章!

相關(guān)文章

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

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

    這篇文章主要介紹了詳解使用spring boot admin監(jiān)控spring cloud應(yīng)用程序,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Java 括號匹配問題案例詳解

    Java 括號匹配問題案例詳解

    這篇文章主要介紹了Java 括號匹配問題案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 深入解析Spring?Boot?的SPI機制詳情

    深入解析Spring?Boot?的SPI機制詳情

    這篇文章主要介紹了深入解析Spring?Boot的SPI機制詳情,SPI是JDK內(nèi)置的一種服務(wù)提供發(fā)現(xiàn)機制,可以用來啟用框架擴展和替換組件,主要用于框架中開發(fā),更多相關(guān)介紹,感興趣的小伙伴可以參考一下下面文章內(nèi)容
    2022-08-08
  • SpringBoot之@ConditionalOnProperty注解使用方法

    SpringBoot之@ConditionalOnProperty注解使用方法

    在平時業(yè)務(wù)中,我們需要在配置文件中配置某個屬性來決定是否需要將某些類進行注入,讓Spring進行管理,而@ConditionalOnProperty能夠?qū)崿F(xiàn)該功能,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-05-05
  • Mybatis-plus更新Null字段的四種方法

    Mybatis-plus更新Null字段的四種方法

    在項目開發(fā)過程中,經(jīng)常會使用Mybatis-plus的updateById()方法,快速將接收道德參數(shù)或者查詢結(jié)果中原本不為null的字段更新為null,這個時候使用updateById()并不能實現(xiàn)這個操作,不會報錯,但是對應(yīng)的字段并沒有更新為null,所以本文介紹了Mybatis-plus更新Null字段的方法
    2025-03-03
  • 最新評論