最新使用微服務feign進行服務調用時,出現了如下錯誤:
test-test-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
具體的表現是,當 使用feign調用馬上就報錯,也沒有超過設定的逾時時間,我的配置如下:
ribbon:
OkToRetryOnAllOperations: false #對所有操作請求都進行重試,預設false
ReadTimeout: 60000 #負載均衡逾時時間,預設值5000
ConnectTimeout: 3000 #ribbon請求連接配接的逾時時間,預設值2000
MaxAutoRetries: 0 #對目前執行個體的重試次數,預設0
MaxAutoRetriesNextServer: 1 #對切換執行個體的重試次數,預設1
hystrix:
command:
default: #default全局有效,service id指定應用有效
execution:
timeout:
#如果enabled設定為false,則請求逾時交給ribbon控制,為true,則逾時作為熔斷根據
enabled: true
isolation:
thread:
timeoutInMilliseconds: 60000 #斷路器逾時時間,預設1000ms
例如我設定的ribbon逾時時間為60秒,但調用1秒内就報錯,一開始懷疑是錯誤次數太多導緻熔斷,還設定了熔斷視窗和熔斷次數,後來發現,hystrix的timeoutInMilliseconds應該配置得比ribbon的大,這樣ribbon才會觸發逾時重試機制,将timeoutInMilliseconds設定成70000就可以了。此外說一點關于hystrix和ribbon的兩個關于逾時的設定,在feign調用過程中是數值較小的生效。如果各位網友遇到的問題不是我的這個解決方案,由于feign預設輸出的日志十分有限,就需要展示更多日志來排查問題,可以加上以下配置,将feign的調用日志詳細的展示出來,幫助我們排查,這裡我增加了一個配置類:
@Configuration
public class FeignConfig {
/**
* 配置請求重試
*
*/
@Bean
public Retryer feignRetryer() {
return new Retryer.Default(200, SECONDS.toMillis(2), 10);
}
/**
* 設定請求逾時時間
*預設
* public Options() {
* this(10 * 1000, 60 * 1000);
* }
*
*/
@Bean
Request.Options feignOptions() {
return new Request.Options(60 * 1000, 60 * 1000);
}
/**
* 列印請求日志
* @return
*/
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
控制台列印出來的日志就像下面所示:
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] <--- HTTP/1.1 500 (607ms)
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] cache-control: no-cache, no-store, max-age=0, must-revalidate
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] connection: close
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] content-type: application/json;charset=UTF-8
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] date: Mon, 09 Oct 2017 11:31:07 GMT
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] expires: 0
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] pragma: no-cache
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] transfer-encoding: chunked
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-application-context: test-test-service:dev:8771
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-content-type-options: nosniff
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-frame-options: DENY
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-xss-protection: 1; mode=block
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload]
2017-10-09 19:31:02.049 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload]
{
"code" : 500,
"error" : "Internal Server Error",
"data" : null
}
2017-10-09 19:31:02.049 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] <--- END HTTP (72-byte body)