Spring Cloud OpenFeign 切换为 Okhttp3 提升 QPS

妖精的绣舞 提交于 2019-12-06 02:21:19

Spring Cloud  OpenFeign 切换为 Okhttp3 提升 QPS 直接进入干货区:

 

# 引入 okhttp3

 

  •  
  •  
    compile("io.github.openfeign:feign-okhttp")    compile("com.squareup.okhttp3:okhttp")

 

# 配置 application.yml

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
feign:  # feign启用hystrix,才能熔断、降级  # hystrix:  # enabled: true  # 启用 okhttp 关闭默认 httpclient  httpclient:    enabled: false  okhttp:    enabled: true  # 请求与响应的压缩以提高通信效率  compression:    request:      enabled: true      min-request-size: 2048      mime-types: text/xml,application/xml,application/json    response:      enabled: true

 

# okhttp3 参数配置

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
/** * 配置 okhttp 与连接池 * ConnectionPool 默认创建5个线程,保持5分钟长连接 * * @author http://mybatis.plus * @since 2019-08-19 */@Configuration@ConditionalOnClass(Feign.class)@AutoConfigureBefore(FeignAutoConfiguration.class)public class OkHttpConfig {
    // 默认老外留给你彩蛋中文乱码,加上它就 OK    @Bean    public Encoder encoder() {        return new FormEncoder();    }
    @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();    }}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!