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

Gateway集成Netty服務的配置加載詳解

 更新時間:2023年02月28日 15:47:09   作者:知了一笑  
這篇文章主要為大家介紹了Gateway集成Netty服務的配置加載詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

一、Netty簡介

Gateway和Netty都有盲區(qū)的感覺;

Netty是一個異步的,事件驅動的網(wǎng)絡應用框架,用以快速開發(fā)高可靠、高性能的網(wǎng)絡應用程序。

傳輸服務:提供網(wǎng)絡傳輸能力的管理;

協(xié)議支持:支持常見的數(shù)據(jù)傳輸協(xié)議;

核心模塊:包括可擴展事件模型、通用的通信API、零拷貝字節(jié)緩沖;

二、Netty入門案例

1、服務端啟動

配置Netty服務器端程序,引導相關核心組件的加載;

public class NettyServer {
    public static void main(String[] args) {
        // EventLoop組,處理事件和IO
        EventLoopGroup parentGroup = new NioEventLoopGroup();
        EventLoopGroup childGroup = new NioEventLoopGroup();
        try {
            // 服務端啟動引導類
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(parentGroup, childGroup)
                    .channel(NioServerSocketChannel.class).childHandler(new ChannelInit());
            // 異步IO的結果
            ChannelFuture channelFuture = serverBootstrap.bind(8082).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }
}

2、通道初始化

ChannelInitializer特殊的通道處理器,提供一種簡單的方法,對注冊到EventLoop的通道進行初始化;比如此處設置的編碼解碼器,自定義處理器;

public class ChannelInit extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) {
        // 獲取管道
        ChannelPipeline pipeline = socketChannel.pipeline();
        // Http編碼、解碼器
        pipeline.addLast("DefHttpServerCodec",new HttpServerCodec());
        // 添加自定義的handler
        pipeline.addLast("DefHttpHandler", new DefHandler());
    }
}

3、自定義處理器

處理對服務器端發(fā)起的訪問,通常包括請求解析,具體的邏輯執(zhí)行,請求響應等過程;

public class DefHandler extends SimpleChannelInboundHandler<HttpObject> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject message) throws Exception {
        if(message instanceof HttpRequest) {
            // 請求解析
            HttpRequest httpRequest = (HttpRequest) message;
            String uri = httpRequest.uri();
            String method = httpRequest.method().name();
            log.info("【HttpRequest-URI:"+uri+"】");
            log.info("【HttpRequest-method:"+method+"】");
            Iterator<Map.Entry<String,String>> iterator = httpRequest.headers().iteratorAsString();
            while (iterator.hasNext()){
                Map.Entry<String,String> entry = iterator.next();
                log.info("【Header-Key:"+entry.getKey()+";Header-Value:"+entry.getValue()+"】");
            }
            // 響應構建
            ByteBuf content = Unpooled.copiedBuffer("Netty服務", CharsetUtil.UTF_8);
            FullHttpResponse response = new DefaultFullHttpResponse
                                        (HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=utf-8");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
            ctx.writeAndFlush(response);
        }
    }
}

4、測試請求

上面入門案例中,簡單的配置了一個Netty服務器端,啟動之后在瀏覽器中模擬訪問即可;

http://127.0.0.1:8082/?id=1&name=Spring

三、Gateway集成

1、依賴層級

項目中Gateway網(wǎng)關依賴的版本為2.2.5.RELEASE,發(fā)現(xiàn)Netty依賴的版本為4.1.45.Final,是當下比較主流的版本;

<!-- 1、項目工程依賴 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>
<!-- 2、starter-gateway依賴 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
  <version>2.3.2.RELEASE</version>
</dependency>
<!-- 3、starter-webflux依賴 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-reactor-netty</artifactId>
  <version>2.3.2.RELEASE</version>
</dependency>

2、自動化配置

在Gateway網(wǎng)關的自動化配置配置類中,提供了Netty配置的管理;

@AutoConfigureBefore({ HttpHandlerAutoConfiguration.class,WebFluxAutoConfiguration.class })
@ConditionalOnClass(DispatcherHandler.class)
public class GatewayAutoConfiguration {
    @Configuration(proxyBeanMethods = false)
    @ConditionalOnClass(HttpClient.class)
    protected static class NettyConfiguration {
        @Bean
        @ConditionalOnProperty(name = "spring.cloud.gateway.httpserver.wiretap")
        public NettyWebServerFactoryCustomizer nettyServerWiretapCustomizer(
                Environment environment, ServerProperties serverProperties) {
            return new NettyWebServerFactoryCustomizer(environment, serverProperties) {
                @Override
                public void customize(NettyReactiveWebServerFactory factory) {
                    factory.addServerCustomizers(httpServer -> httpServer.wiretap(true));
                    super.customize(factory);
                }
            };
        }
    }
}

四、配置加載

1、基礎配置

在工程的配置文件中,簡單做一些基礎性的設置;

server:
  port: 8081                  # 端口號
  netty:                      # Netty組件
    connection-timeout: 3000  # 連接超時

2、屬性配置類

在ServerProperties類中,并沒有提供很多顯式的Netty配置參數(shù),更多信息需要參考工廠類;

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
    private Integer port;
    public static class Netty {
        private Duration connectionTimeout;
    }
}

3、配置加載分析

  • 基于配置的屬性,定制化管理Netty服務的信息;
public class NettyWebServerFactoryCustomizer
        implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory>{
    private final Environment environment;
    private final ServerProperties serverProperties;
    @Override
    public void customize(NettyReactiveWebServerFactory factory) {
        PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
        ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();
        propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull()
                .to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));
    }
}
  • NettyReactiveWeb服務工廠,基于上述入門案例,創(chuàng)建WebServer時,部分參數(shù)信息出自LoopResources接口;
public class NettyReactiveWebServerFactory extends AbstractReactiveWebServerFactory {
    private ReactorResourceFactory resourceFactory;
    @Override
    public WebServer getWebServer(HttpHandler httpHandler) {
        HttpServer httpServer = createHttpServer();
        ReactorHttpHandlerAdapter handlerAdapter = new ReactorHttpHandlerAdapter(httpHandler);
        NettyWebServer webServer = new NettyWebServer(httpServer, handlerAdapter, this.lifecycleTimeout);
        webServer.setRouteProviders(this.routeProviders);
        return webServer;
    }
    private HttpServer createHttpServer() {
		HttpServer server = HttpServer.create();
		if (this.resourceFactory != null) {
        	LoopResources resources = this.resourceFactory.getLoopResources();
        	server = server.tcpConfiguration(
        			(tcpServer) -> tcpServer.runOn(resources).addressSupplier(this::getListenAddress));
        }
        return applyCustomizers(server);
	}
}

五、周期管理方法

1、控制類

Gateway項目中,Netty服務核心控制類,通過NettyReactiveWebServerFactory工廠類創(chuàng)建,對Netty生命周期的管理提供了一層包裝;

public class NettyWebServer implements WebServer {
    private final HttpServer httpServer;
    private final ReactorHttpHandlerAdapter handlerAdapter;
    /**
     * 啟動方法
     */
    @Override
    public void start() throws WebServerException {
        if (this.disposableServer == null) {
            this.disposableServer = startHttpServer();
            // 控制臺日志
            logger.info("Netty started on port(s): " + getPort());
            startDaemonAwaitThread(this.disposableServer);
        }
    }
    private DisposableServer startHttpServer() {
        HttpServer server = this.httpServer;
        if (this.routeProviders.isEmpty()) {
            server = server.handle(this.handlerAdapter);
        }
        return server.bindNow();
    }
    /**
     * 停止方法
     */
    @Override
    public void stop() throws WebServerException {
        if (this.disposableServer != null) {
            // 釋放資源
            if (this.lifecycleTimeout != null) {
                this.disposableServer.disposeNow(this.lifecycleTimeout);
            }
            else {
                this.disposableServer.disposeNow();
            }
            // 對象銷毀
            this.disposableServer = null;
        }
    }
}

2、管理類

Netty組件中抽象管理類,以安全的方式構建Http服務;

public abstract class HttpServer {
    public static HttpServer create() {
        return HttpServerBind.INSTANCE;
    }
    public final DisposableServer bindNow() {
        return bindNow(Duration.ofSeconds(45));
    }
    public final HttpServer handle(BiFunction<? super HttpServerRequest, ? super
            HttpServerResponse, ? extends Publisher<Void>> handler) {
        return new HttpServerHandle(this, handler);
    }
}

參考源碼

編程文檔:

https://gitee.com/cicadasmile/butte-java-note

應用倉庫:

https://gitee.com/cicadasmile/butte-flyer-parent

以上就是Gateway集成Netty服務的配置加載詳解的詳細內容,更多關于Gateway集成Netty服務的資料請關注腳本之家其它相關文章!

相關文章

  • hotspot解析jdk1.8?Unsafe類park和unpark方法使用

    hotspot解析jdk1.8?Unsafe類park和unpark方法使用

    這篇文章主要為大家介紹了hotspot解析jdk1.8?Unsafe類park和unpark方法使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 教你輕松制作java視頻播放器

    教你輕松制作java視頻播放器

    這篇文章主要為大家詳細介紹了如何編寫屬于自己的java視頻播放器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 為什么Java volatile++不是原子性的詳解

    為什么Java volatile++不是原子性的詳解

    這篇文章主要給大家介紹了關于為什么Java volatile++不是原子性的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • java中get()方法和set()方法的作用淺析

    java中get()方法和set()方法的作用淺析

    這篇文章主要給大家介紹了關于java中get()方法和set()方法的作用,set是是對數(shù)據(jù)進行設置,而get是對數(shù)據(jù)進行獲取,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-07-07
  • java 內部類的實例詳解

    java 內部類的實例詳解

    這篇文章主要介紹了java 內部類的實例詳解的相關資料,希望通過本文大家能夠理解掌握java內部類的使用,需要的朋友可以參考下
    2017-09-09
  • Spring Cloud Nacos 和 Eureka區(qū)別解析

    Spring Cloud Nacos 和 Eureka區(qū)別解析

    Spring Cloud Nacos 和 Spring Cloud Eureka 都是 Spring Cloud 微服務框架中的服務注冊和發(fā)現(xiàn)組件,用于幫助開發(fā)者輕松地構建和管理微服務應用,這篇文章主要介紹了Spring Cloud Nacos 和 Eureka區(qū)別,需要的朋友可以參考下
    2023-08-08
  • Spring的Xml和JavaConfig 擴展哪個好用

    Spring的Xml和JavaConfig 擴展哪個好用

    今天給大家介紹基于注解的Spring擴展,Spring的Xml和JavaConfig 擴展的配置方法,關于Spring的Xml和JavaConfig 擴展你會選哪個呢,帶著這個問題一起通過本文學習下吧
    2021-05-05
  • Java練習之潛艇小游戲的實現(xiàn)

    Java練習之潛艇小游戲的實現(xiàn)

    這篇文章主要和大家分享一個Java小練習——利用Java編寫一個潛艇小游戲,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-03-03
  • SpringBoot登錄用戶權限攔截器

    SpringBoot登錄用戶權限攔截器

    這篇文章主要介紹了SpringBoot登錄用戶權限攔截器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • springboot項目關閉swagger如何防止漏洞掃描

    springboot項目關閉swagger如何防止漏洞掃描

    這篇文章主要介紹了springboot項目關閉swagger如何防止漏洞掃描,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-05-05

最新評論