基于Feign使用okhttp的填坑之旅

 更新时间:2021年02月25日 09:53:17   作者:石楠烟斗的雾  
这篇文章主要介绍了基于Feign使用okhttp的填坑之旅,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

1、由于项目需要远程调用http请求

因此就想到了Feign,因为真的非常的方便,只需要定义一个接口就行。

但是feign默认使用的JDK的URLHttpConnection,没有连接池效率不好,从Feign的自动配置类FeignAutoConfiguration中可以看到Feign除了默认的http客户端还支持okhttp和ApacheHttpClient,我这里选择了okhttp,它是有连接池的。

2、看看网络上大部分博客中是怎么使用okhttp的

1)、引入feign和okhttp的maven坐标

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
 
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
 
 <dependency>
   <groupId>io.github.openfeign</groupId>
   <artifactId>feign-okhttp</artifactId>
 </dependency>

2)、在配置文件中禁用默认的URLHttpConnection,启动okhttp

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

3)、其实这个时候就可以使用okhttp了

但网络上大部分博客还写了一个自定义配置类,在其中实例化了一个okhttp3.OkHttpClient,就是这么一个配置类导致了大坑啊,有了它之后okhttp根本不会生效,不信咱们就是来试一下

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class OkHttpConfig { 
  @Bean
  public okhttp3.OkHttpClient okHttpClient(){
    return new okhttp3.OkHttpClient.Builder()
        //设置连接超时
        .connectTimeout(10 , TimeUnit.SECONDS)
        //设置读超时
        .readTimeout(10 , TimeUnit.SECONDS)
        //设置写超时
        .writeTimeout(10 , TimeUnit.SECONDS)
        //是否自动重连
        .retryOnConnectionFailure(true)
        .connectionPool(new ConnectionPool(10 , 5L, TimeUnit.MINUTES))
        .build();
  }
}

上面这个配置类其实就是配置了一下okhttp的基本参数和连接池的基本参数

此时我们可以在配置文件中开始日志打印,看一下那些自动配置没有生效

debug=true

启动我们的项目可以在控制台搜索到如下日志输出

FeignAutoConfiguration.OkHttpFeignConfiguration:
   Did not match:
     - @ConditionalOnBean (types: okhttp3.OkHttpClient; SearchStrategy: all) found beans of type 'okhttp3.OkHttpClient' okHttpClient (OnBeanCondition)
   Matched:
     - @ConditionalOnClass found required class 'feign.okhttp.OkHttpClient'; @ConditionalOnMissingClass did not find unwanted class 'com.netflix.loadbalancer.ILoadBalancer' (OnClassCondition)
     - @ConditionalOnProperty (feign.okhttp.enabled) matched (OnPropertyCondition)

从日志中可以清楚的看到FeignAutoConfiguration.OkHttpFeignConfiguration没有匹配成功(Did not match),原因也很简单是因为容器中已经存在了okhttp3.OkHttpClient对象,我们去看看这个配置类的源码,其中类上标注了@ConditionalOnMissingBean(okhttp3.OkHttpClient.class),意思上当容器中不存在okhttp3.OkHttpClient对象时才生效,然后我们却在自定义的配置类中画蛇添足的实例化了一个该对象到容器中。

@Configuration(proxyBeanMethods = false)
 @ConditionalOnClass(OkHttpClient.class)
 @ConditionalOnMissingClass("com.netflix.loadbalancer.ILoadBalancer")
 @ConditionalOnMissingBean(okhttp3.OkHttpClient.class)
 @ConditionalOnProperty("feign.okhttp.enabled")
 protected static class OkHttpFeignConfiguration { 
 private okhttp3.OkHttpClient okHttpClient; 
 @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(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);
 } 
 }

4)、该如何处理才能使okhttp生效

其中我们的自定义配置类中并没有做什么特别复杂的事情,仅仅是给okhttp3.OkHttpClient和它的连接池对象设置了几个参数罢了,看看上面OkHttpFeignConfiguration类中实例化的几个类对象,其中就包含了okhttp3.OkHttpClient和ConnectionPool,从代码中不难看出它们的参数值都是从FeignHttpClientProperties获取的,因此我们只需要在配置文件中配上feign.httpclient开头的相关配置就可以了生效了。

如果我们的目的不仅仅是简单的修改几个参数值,比如需要在okhttp中添加拦截器Interceptor,这也非常简单,只需要写一个Interceptor的实现类,然后将OkHttpFeignConfiguration的内容完全复制一份到我们自定义的配置类中,并设置okhttp3.OkHttpClient的拦截器即可。

import okhttp3.Interceptor;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 
import java.io.IOException; 
public class MyOkhttpInterceptor implements Interceptor { 
  Logger logger = LoggerFactory.getLogger(MyOkhttpInterceptor.class); 
  @Override
  public Response intercept(Chain chain) throws IOException {
    logger.info("okhttp method:{}",chain.request().method());
    logger.info("okhttp request:{}",chain.request().body());
    return chain.proceed(chain.request());
  }
}

将自定义配置类中原有的内容去掉,复制一份OkHttpFeignConfiguration的代码做简单的修改,设置拦截器的代码如下

  @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(connectTimeout, TimeUnit.MILLISECONDS)
        .followRedirects(followRedirects).connectionPool(connectionPool)
          //这里设置我们自定义的拦截器
        .addInterceptor(new MyOkhttpInterceptor())
        .build();
    return this.okHttpClient;
  }

3、最后上两张图,Feign的动态代理使用和处理流程

补充:spring cloud feign sentinel okhttp3 gzip 压缩问题

引入pom okhttp 配置,okhttp使用连接池技术,相对feign httpUrlConnection 每次请求,创建一个连接,效率更高

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

okhttp 开始压缩条件

增加拦截器动态删除Accept-Encoding 参数,使okhttp压缩生效

@Slf4j
public class HttpOkInterceptor implements Interceptor {
  @Override
  public Response intercept(Chain chain) throws IOException { 
    Request originRequest = chain.request();
    Response response = null;
    if (StringUtils.isNotEmpty(originRequest.header("Accept-Encoding"))) {
      Request request = originRequest.newBuilder().removeHeader("Accept-Encoding").build();
 
      long doTime = System.nanoTime();
      response = chain.proceed(request);
      long currentTime = System.nanoTime();
      if(response != null) {
        ResponseBody responseBody = response.peekBody(1024 * 1024);
        LogUtil.info(log, String.format("接收响应: [%s] %n返回json:【%s】 %.1fms%n%s",
            response.request().url(),
            responseBody.string(),
            (currentTime - doTime) / 1e6d,
            response.headers()));
      }else {
        String encodedPath = originRequest.url().encodedPath();
        LogUtil.info(log, String.format("接收响应: [%s] %n %.1fms%n",
            encodedPath,
            (currentTime - doTime) / 1e6d));
      }
    } 
    return response;
	} 
}

feign 配置

feign:
 sentinel:
  # 开启Sentinel对Feign的支持
  enabled: true
 httpclient:
  enabled: false
 okhttp:
  enabled: true

feign 配置类

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig { 
  @Bean
  public okhttp3.OkHttpClient okHttpClient(){
    return new okhttp3.OkHttpClient.Builder()
        //设置连接超时
        .connectTimeout(10, TimeUnit.SECONDS)
        //设置读超时
        .readTimeout(10, TimeUnit.SECONDS)
        //设置写超时
        .writeTimeout(10, TimeUnit.SECONDS)
        //是否自动重连
        .retryOnConnectionFailure(true)
        .connectionPool(new ConnectionPool(10, 5L, TimeUnit.MINUTES))
        .build(); 
  } 
}

案例:feign client

@FeignClient(name = "服务名称",fallbackFactory = FeignFallBack.class ,url = "调试地址", configuration =FeignConfiguration.class)
public interface FeignService { 
  @RequestMapping(value = "/test/updateXx", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity<byte[]> updateXx(@RequestBody XxVo xXVo);  
}

不知为啥 sentinel feign默认http,对压缩支持不好,使用okhttp 代替实现

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

相关文章

  • jsp中存取session值简单介绍

    jsp中存取session值简单介绍

    这篇文章主要介绍了jsp中存取session值简单介绍,涉及request和session的域操作等相关内容,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • 基于@RestControllerAdvice与@ControllerAdvice的区别说明

    基于@RestControllerAdvice与@ControllerAdvice的区别说明

    这篇文章主要介绍了@RestControllerAdvice与@ControllerAdvice的区别说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 如何让Jackson JSON生成的数据包含的中文以unicode方式编码

    如何让Jackson JSON生成的数据包含的中文以unicode方式编码

    这篇文章主要介绍了如何让Jackson JSON生成的数据包含的中文以unicode方式编码。需要的朋友可以过来参考下,希望对大家有所帮助
    2013-12-12
  • 经典再现 基于JAVA平台开发坦克大战游戏

    经典再现 基于JAVA平台开发坦克大战游戏

    经典再现,这篇文章主要介绍了基于JAVA平台开发坦克大战游戏的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Java基于解释器模式实现定义一种简单的语言功能示例

    Java基于解释器模式实现定义一种简单的语言功能示例

    这篇文章主要介绍了Java基于解释器模式实现定义一种简单的语言功能,简单描述了解释器模式的概念、功能及Java使用解释器模式定义一种简单语言的相关实现与使用技巧,需要的朋友可以参考下
    2018-05-05
  • SpringBoot集成ElasticSearch(ES)实现全文搜索功能

    SpringBoot集成ElasticSearch(ES)实现全文搜索功能

    Elasticsearch是一个开源的分布式搜索和分析引擎,它被设计用于处理大规模数据集,它提供了一个分布式多用户能力的全文搜索引擎,本文将给大家介绍SpringBoot集成ElasticSearch(ES)实现全文搜索功能,需要的朋友可以参考下
    2024-02-02
  • Java使用桥接模式实现开关和电灯照明功能详解

    Java使用桥接模式实现开关和电灯照明功能详解

    这篇文章主要介绍了Java使用桥接模式实现开关和电灯照明功能,较为详细的讲述了桥接模式的概念、原理并结合实例形式分析了Java使用桥接模式实现开关和电灯照明功能相关操作步骤与注意事项,需要的朋友可以参考下
    2018-05-05
  • 深入理解Java 对象和类

    深入理解Java 对象和类

    下面小编就为大家带来一篇深入理解Java 对象和类。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • IntelliJ IDEA 2020.2.3永久破解激活教程(亲测有效)

    IntelliJ IDEA 2020.2.3永久破解激活教程(亲测有效)

    intellij idea 2022是一款市面上最好的JAVA IDE编程工具,该工具支持git、svn、github等版本控制工具,整合了智能代码助手、代码自动提示等功能,本教程给大家分享IDEA 2022最新永久激活码,感兴趣的朋友参考下吧
    2020-10-10
  • SpringSecurity自动登录流程与实现详解

    SpringSecurity自动登录流程与实现详解

    这篇文章主要介绍了SpringSecurity自动登录流程与实现详解,所谓的自动登录是在访问链接时浏览器自动携带上了Cookie中的Token交给后端校验,如果删掉了Cookie或者过期了同样是需要再次验证的,需要的朋友可以参考下
    2024-01-01

最新评论