最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Feign 使用HttpClient和OkHttp方式

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

使用HttpClient和OkHttp

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

首先查看FeignRibbonClient的自動配置類FeignRibbonClientAutoConfiguration,該類在程序啟動的時候注入一些Bean,其中注入了一個BeanName為feignClient的Client類型的Bean。在省缺配置BeanName為FeignClient的Bean的情況下,會自動注入Client.Default這個對象,跟蹤Client.Default源碼,Client.Default使用的網(wǎng)絡請求框架是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作為網(wǎng)絡請求框架,則只需要在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 #線程池最大連接數(shù)
  okhttp:
    enabled: true

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

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

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

相關文章

  • 詳細圖解Java中字符串的初始化

    詳細圖解Java中字符串的初始化

    字符串廣泛應用在Java編程中,在Java中字符串屬于對象,Java提供了String類來創(chuàng)建和操作字符串,下面這篇文章主要給大家介紹了Java中字符串初始化的相關資料,需要的朋友可以參考下
    2021-08-08
  • Java中SpringSecurity密碼錯誤5次鎖定用戶的實現(xiàn)方法

    Java中SpringSecurity密碼錯誤5次鎖定用戶的實現(xiàn)方法

    這篇文章主要介紹了Java中SpringSecurity密碼錯誤5次鎖定用戶的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • 如何解決shardingsphere報錯Missing?the?data?source?name:‘null‘

    如何解決shardingsphere報錯Missing?the?data?source?name:‘null‘

    使用ShardingSphere進行分庫操作時,如果遇到“Missing?the?datasource?name:?‘null’”的錯誤,通常是因為所操作的表沒有配置相關的路由信息,例如,如果在properties中僅配置了health_record和health_task的路由規(guī)則
    2024-11-11
  • springBoot動態(tài)加載jar及如何將類注冊到IOC

    springBoot動態(tài)加載jar及如何將類注冊到IOC

    在SpringBoot項目中動態(tài)加載jar文件并將其類注冊到IOC容器是一種高級應用方式,,這種方法為SpringBoot項目提供了更靈活的擴展能力,使得項目可以在不修改原有代碼的基礎上增加新的功能模塊,感興趣的朋友一起看看吧
    2024-11-11
  • springboot 定時任務@Scheduled實現(xiàn)解析

    springboot 定時任務@Scheduled實現(xiàn)解析

    這篇文章主要介紹了springboot 定時任務@Scheduled實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • Java去掉字符串最后一個逗號的方法

    Java去掉字符串最后一個逗號的方法

    Java中去掉字符串的最后一個逗號有多種實現(xiàn)方法,不同的方法適用于不同的場景,本文通過實例代碼介紹Java去掉字符串最后一個逗號的相關知識,感興趣的朋友一起看看吧
    2023-12-12
  • JAVA實現(xiàn)基于Tcp協(xié)議的簡單Socket通信實例

    JAVA實現(xiàn)基于Tcp協(xié)議的簡單Socket通信實例

    本篇文章主要介紹了JAVA實現(xiàn)基于Tcp協(xié)議的簡單Socket通信實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • java壓縮多個文件并且返回流示例

    java壓縮多個文件并且返回流示例

    這篇文章主要介紹了java壓縮多個文件并且返回流示例,返回壓縮流主是為了在程序里再做其它操作,需要的朋友可以參考下
    2014-03-03
  • SpringBoot設置接口超時時間的方法

    SpringBoot設置接口超時時間的方法

    這篇文章主要介紹了SpringBoot設置接口超時時間的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Spring實戰(zhàn)之搜索Bean類操作示例

    Spring實戰(zhàn)之搜索Bean類操作示例

    這篇文章主要介紹了Spring實戰(zhàn)之搜索Bean類操作,結合實例形式分析了Spring搜索Bean類的相關配置、接口實現(xiàn)與操作技巧,需要的朋友可以參考下
    2019-12-12

最新評論

江油市| 咸宁市| 土默特左旗| 囊谦县| 田林县| 汉阴县| 清流县| 科技| 衡阳市| 台南市| 封丘县| 于田县| 南充市| 杨浦区| 南城县| 闽侯县| 绥宁县| 沂南县| 闻喜县| 平昌县| 普宁市| 成武县| 巴中市| 常宁市| 桂阳县| 绥滨县| 新密市| 古丈县| 邹平县| 沁源县| 鸡西市| 亚东县| 隆子县| 安宁市| 平潭县| 新化县| 台北市| 池州市| 天津市| 阿克陶县| 阿拉善左旗|