• SpringCloud系列——Feign+Ribbon配置实战篇


    一般情况下,FeignRibbon我们都是结合使用的。Feign网络请求客户端,Ribbon用于负载均衡,feign底层实现了ribbon
     

    默认配置

    默认配置入口
    1. //RibbonClientConfiguration.java
    2. @Bean
    3. @ConditionalOnMissingBean
    4. public IClientConfig ribbonClientConfig() {
    5. DefaultClientConfigImpl config = new DefaultClientConfigImpl();
    6. config.set(CommonClientConfigKey.ConnectTimeout, 1000);
    7. config.set(CommonClientConfigKey.ReadTimeout, 1000);
    8. return config;
    9. }

    1.文件配置

    全局配置:

    1. ribbon.ConnectTimeout=1000
    2. ribbon.ReadTimeout=3000

    单个配置:

    1. feignClientName.ribbon.ConnectTimeout=1000
    2. feignClientName.ribbon.ReadTimeout=3000

    2.代码注解配置

    新增配置类:

    1. @Configuration
    2. public class CustomConfiguration {
    3. @Bean
    4. public Request.Options feignRequestOptions() {
    5. return new Request.Options(connectTimeoutMillis, readTimeoutMillis);
    6. }
    7. }

    在被FeignClient注解的类中添加配置:

    1. @FeignClient(name = "user-api", configuration = CustomConfiguration.class)
    2. public interface TestFeign {
    3. @GetMapping(value = "/getName")
    4. @ApiOperation(value = "测试数据", httpMethod = "GET")
    5. String getName();
    6. }

    其中,代码注解配置比文件配置方式优先级更高。

    3.重试次数设置

    Feign默认是不会进行重试的!

    1. //FeignClientsConfiguration.java
    2. @Bean
    3. @ConditionalOnMissingBean
    4. public Retryer feignRetryer() {
    5. return Retryer.NEVER_RETRY;
    6. }

     自定义重试策略

    1. @Bean
    2. public Retryer feignRetryer() {
    3. return new Retryer.Default(period, maxPeriod, maxAttempts);
    4. }

    同时Feign的HTTP客户端支持三种框架:

            HttpURLConnection、HttpClient、OkHttpFeign使用的默认客户端是HttpURLConnection,且没有SSL认证:

    4.切换网络请求客户端

    1. //feign.Client.java
    2. public interface Client{
    3. public static class Default implements Client {
    4. HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
    5. final HttpURLConnection connection =
    6. (HttpURLConnection) new URL(request.url()).openConnection();
    7. ......
    8. }
    9. }
    10. }

    切换成ApacheHttpClient

    新增依赖:这里的版本号,要跟io.github.openfeign:feign-core的版本,保持一致。

    1. io.github.openfeign
    2. feign-httpclient
    3. 10.1.0
    新增配置:
    feign.compression.response.enabled=true
    

    这里设置为true之后,spring容器就会去加载自定义的httpClient

    1. //HttpClientFeignLoadBalancedConfiguration.java
    2. //若为true,就会来加载这里
    3. @Bean
    4. @ConditionalOnProperty(value = "feign.compression.response.enabled", havingValue = "true")
    5. public CloseableHttpClient customHttpClient(HttpClientConnectionManager httpClientConnectionManager,
    6. FeignHttpClientProperties httpClientProperties) {
    7. HttpClientBuilder builder = HttpClientBuilder.create().disableCookieManagement().useSystemProperties();
    8. this.httpClient = createClient(builder, httpClientConnectionManager, httpClientProperties);
    9. return this.httpClient;
    10. }
    11. //若为false,加载默认的HttpURLConnection
    12. @Bean
    13. @ConditionalOnProperty(value = "feign.compression.response.enabled", havingValue = "false", matchIfMissing = true)
    14. public CloseableHttpClient httpClient(ApacheHttpClientFactory httpClientFactory, HttpClientConnectionManager httpClientConnectionManager,
    15. FeignHttpClientProperties httpClientProperties) {
    16. this.httpClient = createClient(httpClientFactory.createBuilder(), httpClientConnectionManager, httpClientProperties);
    17. return this.httpClient;
    18. }

    切换成OkHttp

    新增依赖:这里的版本号,要跟io.github.openfeign:feign-core的版本,保持一致。

    1. io.github.openfeign
    2. feign-okhttp
    3. 10.1.0

    新增配置:

    1. feign.httpclient.enabled=false
    2. feign.okhttp.enabled=true

  • 相关阅读:
    move_base代码解析(一)MoveBase::executeCb
    前端发展趋势:WebAssembly、PWA 和响应式设计
    蓝桥等考Python组别十级007
    用户画像的应用场景
    公众号留言功能怎么恢复?评论功能如何开启?
    【mindspore】【模式】PYNATIVE_MODE模式和GRAPH模式的区别
    【后端】Python中的Web开发框架Flask入门hello world;几个案例让你迅速入门Python Flask
    Selenium原理深度解析
    MySQL数据表的基本操作和基于 MySQL数据表的基本操作的综合实例项目
    LOAD_BALANCE=false 主在不会切换备
  • 原文地址:https://blog.csdn.net/weixin_66568937/article/details/139273332