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

Feign 使用HttpClient和OkHttp方式

 更新時間:2021年10月01日 10:34:10   作者:歐拉兔  
這篇文章主要介紹了Feign 使用HttpClient和OkHttp方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用HttpClient和OkHttp

在Feign中,Client是一個非常重要的組件,F(xiàn)eign最終發(fā)送Request請求以及接收Response響應(yīng)都是由Client組件來完成的。Client在Feign源碼中是一個接口,在默認(rèn)情況下,Client的實現(xiàn)類是Client.Default。Client.Default是由HttpURLConnection來實現(xiàn)網(wǎng)絡(luò)請求的。另外,Client還支持HttpClient和OkHttp來進(jìn)行網(wǎng)絡(luò)請求。

首先查看FeignRibbonClient的自動配置類FeignRibbonClientAutoConfiguration,該類在程序啟動的時候注入一些Bean,其中注入了一個BeanName為feignClient的Client類型的Bean。在省缺配置BeanName為FeignClient的Bean的情況下,會自動注入Client.Default這個對象,跟蹤C(jī)lient.Default源碼,Client.Default使用的網(wǎng)絡(luò)請求框架是HttpURLConnection,代碼如下:

public static class Default implements Client {
        private final SSLSocketFactory sslContextFactory;
        private final HostnameVerifier hostnameVerifier;
 
        public Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier) {
            this.sslContextFactory = sslContextFactory;
            this.hostnameVerifier = hostnameVerifier;
        }
 
        public Response execute(Request request, Options options) throws IOException {
            HttpURLConnection connection = this.convertAndSend(request, options);
            return this.convertResponse(connection, request);
        }        
        ......//代碼省略
}

這種情況下,由于缺乏連接池的支持,在達(dá)到一定流量的后服務(wù)肯定會出問題 。

使用HttpClient

那么如何在Feign中使用HttpClient的框架呢?我們查看FeignAutoConfiguration.HttpClientFeignConfiguration的源碼:

    @Configuration
    @ConditionalOnClass({ApacheHttpClient.class})
    @ConditionalOnMissingClass({"com.netflix.loadbalancer.ILoadBalancer"})
    @ConditionalOnMissingBean({CloseableHttpClient.class})
    @ConditionalOnProperty(
        value = {"feign.httpclient.enabled"},
        matchIfMissing = true
    )
    protected static class HttpClientFeignConfiguration {
        private final Timer connectionManagerTimer = new Timer("FeignApacheHttpClientConfiguration.connectionManagerTimer", true);
        @Autowired(
            required = false
        )
        private RegistryBuilder registryBuilder;
        private CloseableHttpClient httpClient;
 
        protected HttpClientFeignConfiguration() {
        }
 
        @Bean
        @ConditionalOnMissingBean({HttpClientConnectionManager.class})
        public HttpClientConnectionManager connectionManager(ApacheHttpClientConnectionManagerFactory connectionManagerFactory, FeignHttpClientProperties httpClientProperties) {
            final HttpClientConnectionManager connectionManager = connectionManagerFactory.newConnectionManager(httpClientProperties.isDisableSslValidation(), httpClientProperties.getMaxConnections(), httpClientProperties.getMaxConnectionsPerRoute(), httpClientProperties.getTimeToLive(), httpClientProperties.getTimeToLiveUnit(), this.registryBuilder);
            this.connectionManagerTimer.schedule(new TimerTask() {
                public void run() {
                    connectionManager.closeExpiredConnections();
                }
            }, 30000L, (long)httpClientProperties.getConnectionTimerRepeat());
            return connectionManager;
        }
 
        @Bean
        public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory, HttpClientConnectionManager httpClientConnectionManager, FeignHttpClientProperties httpClientProperties) {
            RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(httpClientProperties.getConnectionTimeout()).setRedirectsEnabled(httpClientProperties.isFollowRedirects()).build();
            this.httpClient = httpClientFactory.createBuilder().setConnectionManager(httpClientConnectionManager).setDefaultRequestConfig(defaultRequestConfig).build();
            return this.httpClient;
        }
 
        @Bean
        @ConditionalOnMissingBean({Client.class})
        public Client feignClient(HttpClient httpClient) {
            return new ApacheHttpClient(httpClient);
        }
 
        @PreDestroy
        public void destroy() throws Exception {
            this.connectionManagerTimer.cancel();
            if (this.httpClient != null) {
                this.httpClient.close();
            }
 
        }
    }

從代碼@ConditionalOnClass({ApacheHttpClient.class})注解可知,只需要在pom文件上加上HttpClient依賴即可。另外需要在配置文件中配置feign.httpclient.enabled為true,從@ConditionalOnProperty注解可知,這個配置可以不寫,因為在默認(rèn)情況下就為true:

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>9.4.0</version>
</dependency>

使用OkHttp

查看FeignAutoConfiguration.HttpClientFeignConfiguration的源碼:

    @Configuration
    @ConditionalOnClass({OkHttpClient.class})
    @ConditionalOnMissingClass({"com.netflix.loadbalancer.ILoadBalancer"})
    @ConditionalOnMissingBean({okhttp3.OkHttpClient.class})
    @ConditionalOnProperty({"feign.okhttp.enabled"})
    protected static class OkHttpFeignConfiguration {
        private okhttp3.OkHttpClient okHttpClient;
 
        protected OkHttpFeignConfiguration() {
        }
 
        @Bean
        @ConditionalOnMissingBean({ConnectionPool.class})
        public ConnectionPool httpClientConnectionPool(FeignHttpClientProperties httpClientProperties, OkHttpClientConnectionPoolFactory connectionPoolFactory) {
            Integer maxTotalConnections = httpClientProperties.getMaxConnections();
            Long timeToLive = httpClientProperties.getTimeToLive();
            TimeUnit ttlUnit = httpClientProperties.getTimeToLiveUnit();
            return connectionPoolFactory.create(maxTotalConnections, timeToLive, ttlUnit);
        }
 
        @Bean
        public okhttp3.OkHttpClient client(OkHttpClientFactory httpClientFactory, ConnectionPool connectionPool, FeignHttpClientProperties httpClientProperties) {
            Boolean followRedirects = httpClientProperties.isFollowRedirects();
            Integer connectTimeout = httpClientProperties.getConnectionTimeout();
            Boolean disableSslValidation = httpClientProperties.isDisableSslValidation();
            this.okHttpClient = httpClientFactory.createBuilder(disableSslValidation).connectTimeout((long)connectTimeout, TimeUnit.MILLISECONDS).followRedirects(followRedirects).connectionPool(connectionPool).build();
            return this.okHttpClient;
        }
 
        @PreDestroy
        public void destroy() {
            if (this.okHttpClient != null) {
                this.okHttpClient.dispatcher().executorService().shutdown();
                this.okHttpClient.connectionPool().evictAll();
            }
 
        }
 
        @Bean
        @ConditionalOnMissingBean({Client.class})
        public Client feignClient(okhttp3.OkHttpClient client) {
            return new OkHttpClient(client);
        }
    }

同理,如果想要在Feign中使用OkHttp作為網(wǎng)絡(luò)請求框架,則只需要在pom文件中加上feign-okhttp的依賴,代碼如下:

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
    <version>10.2.0</version>
</dependency>

OpenFeign替換為OkHttp

pom中引入feign-okhttp

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>

在application.yml中配置okhttp

feign:
  httpclient:
    connection-timeout: 2000  #單位ms,默認(rèn)2000
    max-connections: 200 #線程池最大連接數(shù)
  okhttp:
    enabled: true

經(jīng)過上面設(shè)置已經(jīng)可以使用okhttp了,因為在FeignAutoConfiguration中已實現(xiàn)自動裝配

如果需要對okhttp做更精細(xì)的參數(shù)設(shè)置,那需要自定義okhttp的實現(xiàn),可以模仿上圖中的實現(xiàn)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 8中Collectors.toMap空指針異常源碼解析

    Java 8中Collectors.toMap空指針異常源碼解析

    這篇文章主要為大家介紹了Java 8中Collectors.toMap空指針異常源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Java Char的簡單工具類CharUtil分享

    Java Char的簡單工具類CharUtil分享

    下面小編就為大家分享一篇Java Char的簡單工具類CharUtil,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • java如何自動補(bǔ)齊數(shù)值至指定位數(shù)

    java如何自動補(bǔ)齊數(shù)值至指定位數(shù)

    這篇文章主要介紹了java如何自動補(bǔ)齊數(shù)值至指定位數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • SpringBoot使用Jwt處理跨域認(rèn)證問題的教程詳解

    SpringBoot使用Jwt處理跨域認(rèn)證問題的教程詳解

    這篇文章主要介紹了SpringBoot使用Jwt處理跨域認(rèn)證問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • 使用list stream: 任意對象List拼接字符串

    使用list stream: 任意對象List拼接字符串

    這篇文章主要介紹了使用list stream:任意對象List拼接字符串操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring使用@Autowired注解實現(xiàn)自動裝配方式

    Spring使用@Autowired注解實現(xiàn)自動裝配方式

    這篇文章主要介紹了Spring使用@Autowired注解實現(xiàn)自動裝配方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java參數(shù)傳遞實現(xiàn)代碼及過程圖解

    Java參數(shù)傳遞實現(xiàn)代碼及過程圖解

    這篇文章主要介紹了Java參數(shù)傳遞實現(xiàn)代碼及過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • CountDownLatch和Atomic原子操作類源碼解析

    CountDownLatch和Atomic原子操作類源碼解析

    這篇文章主要為大家介紹了CountDownLatch和Atomic原子操作類的源碼解析以及理解應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • Java編程基本概念

    Java編程基本概念

    本文主要介紹了Java編程的基本概念,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • JVM垃圾收集器詳解

    JVM垃圾收集器詳解

    本文主要介紹了JVM垃圾收集器的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02

最新評論