天天看點

OpenFeign實戰

預設Client替換

Feign 整體架構非常小巧,在處理請求轉換和消息解析的過程中,基本上沒什麼時間消耗。真正影響性能的,是處理Http請求的環節。由于預設情況下,Feign采用的是JDK的HttpURLConnection,是以整體性能并不高。需要進行性能優化,通常采用ApacheHttpClient或者OKHttp,加入連接配接池技術。
ApacheHttpClient

配置類

HttpClientFeignLoadBalancedConfiguration

  • 依賴
<!-- Http Client 支援 -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
</dependency>

<!-- Apache Http Client 對 Feign 支援 -->
<dependency>
    <groupId>com.netflix.feign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>${feign-httpclient.version}</version>
</dependency>
           
  • 配置檔案
feign:
  httpclient:
    # 開啟 Http Client
    enabled: true
    # 最大連接配接數,預設:200
    max-connections: 200
    # 最大路由,預設:50
    max-connections-per-route: 50
    # 連接配接逾時,預設:2000/毫秒
    connection-timeout: 2000
    # 生存時間,預設:900L
    time-to-live: 900
    # 響應逾時的時間機關,預設:TimeUnit.SECONDS
    #timeToLiveUnit: SECONDS
           
OKHttp

OKHttp 是現在比較常用的一個 HTTP 用戶端通路工具,具有以下特點:

  • 支援 SPDY,可以合并多個到同一個主機的請求。
  • 使用連接配接池技術減少請求的延遲(如果SPDY是可用的話)。
  • 使用 GZIP 壓縮減少傳輸的資料量。
  • 緩存響應避免重複的網絡請求。

配置類 OkHttpFeignConfiguration

  • 依賴
<!-- OKHttp 對 Feign 支援 -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>
           
  • 配置檔案
### Feign 配置
feign:
  httpclient:
    # 是否開啟 Http Client
    enabled: false
  okhttp:
    enabled: true
           

日志級别

  • NONE: No logging (DEFAULT).
  • BASIC: Log only the request method and URL and the response status code and execution time.
  • HEADERS: Log the basic information along with request and response headers.
  • FULL: Log the headers, body, and metadata for both requests and responses.
logging:
  level
	com.xxx.FeignClient: DEBUG 

feign:
  client:
    config:
      default:
        loggerLevel: basic
           

逾時熔斷

配置全局預設的 default

配置指定服務的 feignClientName

熔斷時間必須大于ConnectTimeout + ReadTimeout

feign:
  client:
    config:
      default:
        connectTimeout: 1500
        readTimeout: 3000
      feignClientName:
        connectTimeout: 2000
        readTimeout: 2000
  hystrix:
    #預設關閉、開啟hystrix
    enabled: true

# 全局設定逾時:
hystrix:
  command.default.execution.isolation.thread.timeoutInMilliseconds: 6000
  threadpool:
    default:
      coreSize: 20
      maximumSize: 20
      maxQueueSize: 1500
      queueSizeRejectionThreshold: 1200
           

編解碼器

預設

Decoder: ResponseEntityDecoder (which wraps a SpringDecoder)

Encoder: SpringEncoder

  • 依賴

    GsonEncoder

    GsonDecoder

<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-gson -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-gson</artifactId>
    <version>${feign.gson.version}</version>
</dependency>
           

JacksonEncoder

JacksonDecoder

<!-- feign -->
  <dependency>
      <groupId>com.netflix.feign</groupId>
      <artifactId>feign-jackson</artifactId>
      <version>8.18.0</version>
  </dependency>
           
  • 配置檔案
feign:
  client:
    config:
      default:
        decoder: feign.gson.GsonDecoder
        encoder: feign.gson.GsonEncoder
           

Reference

逾時時間
  • https://www.cnblogs.com/cuiqq/p/13218114.html
  • https://blog.csdn.net/agonie201218/article/details/118802928
DeCoder EnCoder
  • https://www.jianshu.com/p/191d45210d16
概述
  • https://www.jianshu.com/p/b6a47b06d3dc#advanced-usage
  • https://www.jianshu.com/p/76debd6c688e
  • https://blog.csdn.net/luanlouis/article/details/82821294
官方文檔
  • https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.0.RELEASE/reference/html/
  • https://github.com/OpenFeign/feign