feign+hystrix相关超时时间配置问题
feign+hystrix整合配置
1. 準備一個測試服務端
package com.nmm.study.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /*** <p>Title: FeignServiceController</p>* <p>description: 服務提供者測試</p>** @author niemingming 2021/5/14*/ @RestController public class FeignServiceController {/*** 根據參入參數匹配不同異常類型* 1:正常* 2:超時* 3:異常* @param type* @param name* @return*/@GetMapping("/v1/feign/test")public String feignConn(int type, String name) throws InterruptedException {if (type == 1) {return "hello " + name;}if (type == 2) {Thread.sleep(10000);return "time10: " + name;}throw new RuntimeException("failure");}}2.導入feign和hystrix的依賴。
這里我們采用httpclient作為rpc,之所有采用httpclient,是因為其與okhttp性能相差不大。而且是Apache家的,feign默認支持的就是他。支持的更好一點。
<!--引入feign相關內容--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>3.0.1</version> </dependency> <!--引入配置項--> <dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId><version>11.0</version> </dependency> <!--引入hystrix--> <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId><version>2.2.7.RELEASE</version> </dependency>3.配置feign起效
到這一步,如果不考慮熔斷,feign的基本調用就可以了。我們直接在服務中調用就好了。
package com.nmm.study.controller;import com.nmm.study.api.Client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /*** <p>Title: FeingClientController</p>* <p>description: 測試類</p>** @author niemingming 2021/5/14*/ @RestController public class FeingClientController {@Autowiredprivate Client client;@GetMapping("/v1/feignclient")public String testFeign(int type, String name) {return client.feignConn(type, name);} }3.feign連接池管理配置。
feign:httpclient:enabled: true max-connection: 200 max-connections-per-route: 50 connection-timeout: 2000參數說明: 具體生效邏輯可以參看FeignAutoConfiguration配置類。
- enabled:這個參數默認是true,當我們引入了httpclient依賴自動就生效了。
- max-connection:就是httpclient的配置,配置最大連接數
- max-connections-per-route:每個路由的最大連接數
- connection-timeout: httpclient的全局連接超時配置。
4.關于feign超時配置
我們配置了連接池,那么接下來就是超時配置了,我們都不希望因為別人拖垮自己。
feign:client: #FeignClientPropertiesconfig:default:read-timeout: 20000connect-timeout: 20000feigndemo: #feignnameread-timeout: 20000connect-timeout: 20000參數說明:通過參看FeignAutoCOnfiguration類,我們知道具體配置項在FeignClientProperties上。
這里直接說明
- config:配置項,以下,開頭default標識默認配置,feigndemo:我們配置的feignclient的name,是針對該client的具體配置。
- default:默認配置項
- read-timeout:讀取超時
- connect-timeout: 連接超時
- feigndemo:某一個client配置,這里是@FeignClient中的name指定值。
5.通常我們還需要處理異常,以及希望有熔斷。啟用熔斷
feign:circuitbreaker:enabled: true #升級后的熔斷開啟。說明:在我使用的這一版本中,springcloud升級了,2020之后,熔斷開啟時feign.circuitbreaker.enabled參數不在是feign.hystrix.enabled
6.熔斷處理,我們可以指定fallback,如果需要詳細的異常信息請事先fallbackFactory
package com.nmm.study.api.fallback;import com.nmm.study.api.Client; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component;/*** <p>Title: ClientFactoryClient</p>* <p>description: </p>** @author niemingming 2021/5/14*/@Slf4j @Component public class ClientFactoryClient implements FallbackFactory<Client> {@Autowiredprivate ClientFallback clientFallback;@Overridepublic Client create(Throwable cause) {log.error("獲取到的異常,不是原始異常!", cause);//需要將熔斷對象返回。return clientFallback;} }ClientFallback就是實現了Client的fallback類。
7.熔斷的超時怎么辦?
hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 200008.如果要單獨配置某個client的超時怎么辦?
這個還是通過FeignAutoConfiguration類,在CircuitBreakerFactory中找到。直接說,我使用的版本中。commandKey規則是feignclientname_methodName;以我的例子說明:
hystrix:command:feigndemo_feignConn: #參考FeignAutoConfiguration里面關于熔斷的配置項CircuitBreakerFactory;規則為feignname_methodnameexecution:isolation:strategy: THREAD #隔離方式,默認的線程隔離thread:timeoutInMilliseconds: 200009.兩個超時怎么生效,實際測試是都生效了。
10.補充一個WebFilter配置問題.
package com.nmm.study;import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException;/*** <p>Title: TestFilter</p>* <p>description: 連接器不生效問題</p>** @author niemingming 2021/5/14*/@WebFilter(filterName = "testFilter", urlPatterns = "/v1/*") public class TestFilter implements Filter {@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {} }- 一般來說,當我們實現了Filter,并將bean交給spring管理時,也就是只寫一個@Component注解,過濾器就會生效,只不過這時是對所有請求生效。
- urlPartterns生效問題。首先,去掉@Component,改為@WebFilter注解。
- 再啟動類增加@ServletComponentScan注解。
- 如果還不生效,請仔細檢查urlPartterns是否正確,一個*,不是兩個**,兩個是無效的。我就是這個問題。
11.版本說明,分析問題時請注意版本問題。
- springboot:2.4.4
- springcloudopenfeign: 3.0.1
- springcloudhystrix: 2.2.7.RELEASE
總結
以上是生活随笔為你收集整理的feign+hystrix相关超时时间配置问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 讲一下 SVG... 吧
- 下一篇: 从写简历,到面试、谈薪酬的那些技巧和防坑