亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Feign使用HttpClient和OkHttp方式分別是怎樣的

發布時間:2021-10-08 09:58:44 來源:億速云 閱讀:583 作者:柒染 欄目:開發技術

這期內容當中小編將會給大家帶來有關Feign使用HttpClient和OkHttp方式分別是怎樣的,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

使用HttpClient和OkHttp

在Feign中,Client是一個非常重要的組件,Feign最終發送Request請求以及接收Response響應都是由Client組件來完成的。Client在Feign源碼中是一個接口,在默認情況下,Client的實現類是Client.Default。Client.Default是由HttpURLConnection來實現網絡請求的。另外,Client還支持HttpClient和OkHttp來進行網絡請求。

首先查看FeignRibbonClient的自動配置類FeignRibbonClientAutoConfiguration,該類在程序啟動的時候注入一些Bean,其中注入了一個BeanName為feignClient的Client類型的Bean。在省缺配置BeanName為FeignClient的Bean的情況下,會自動注入Client.Default這個對象,跟蹤Client.Default源碼,Client.Default使用的網絡請求框架是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);
        }        
        ......//代碼省略
}

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

使用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注解可知,這個配置可以不寫,因為在默認情況下就為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作為網絡請求框架,則只需要在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,默認2000
    max-connections: 200 #線程池最大連接數
  okhttp:
    enabled: true

經過上面設置已經可以使用okhttp了,因為在FeignAutoConfiguration中已實現自動裝配

Feign使用HttpClient和OkHttp方式分別是怎樣的

如果需要對okhttp做更精細的參數設置,那需要自定義okhttp的實現,可以模仿上圖中的實現

上述就是小編為大家分享的Feign使用HttpClient和OkHttp方式分別是怎樣的了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

涿鹿县| 宜阳县| 靖安县| 嵩明县| 鄂伦春自治旗| 淅川县| 浏阳市| 苍南县| 石河子市| 阳新县| 皮山县| 济阳县| 府谷县| 珲春市| 金平| 龙南县| 娄底市| 志丹县| 静乐县| 通城县| 若尔盖县| 时尚| 沙雅县| 沙湾县| 遵义市| 华坪县| 祁东县| 张家港市| 金寨县| 繁昌县| 安新县| 柏乡县| 克拉玛依市| 大厂| 永顺县| 法库县| 城市| 沾化县| 定兴县| 固始县| 秦安县|