首先聲明一點,這里的重試并不是報錯以后的重試,而是負載均衡客戶端發現遠程請求實例不可到達后,去重試其他實例。
1
2
3
4
5
6
7
8
|
@bean @loadbalanced resttemplate resttemplate() { httpcomponentsclienthttprequestfactory httprequestfactory = new httpcomponentsclienthttprequestfactory(); httprequestfactory.setreadtimeout( 5000 ); httprequestfactory.setconnecttimeout( 5000 ); return new resttemplate(httprequestfactory); } |
feign重試機制
feign默認是通過自己包下的retryer進行重試配置,默認是5次
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package feign; import static java.util.concurrent.timeunit.seconds; /** * cloned for each invocation to {@link client#execute(request, feign.request.options)}. * implementations may keep state to determine if retry operations should continue or not. */ public interface retryer extends cloneable { /** * if retry is permitted, return (possibly after sleeping). otherwise propagate the exception. */ void continueorpropagate(retryableexception e); retryer clone(); public static class default implements retryer { private final int maxattempts; private final long period; private final long maxperiod; int attempt; long sleptformillis; public default () { this ( 100 , seconds.tomillis( 1 ), 5 ); } public default ( long period, long maxperiod, int maxattempts) { this .period = period; this .maxperiod = maxperiod; this .maxattempts = maxattempts; this .attempt = 1 ; } |
feign取消重試
1
2
3
4
|
@bean retryer feignretryer() { return retryer.never_retry; } |
feign請求超時設置
1
2
3
4
5
6
7
|
@bean request.options requestoptions(configurableenvironment env){ int ribbonreadtimeout = env.getproperty( "ribbon.readtimeout" , int . class , 6000 ); int ribbonconnectiontimeout = env.getproperty( "ribbon.connecttimeout" , int . class , 3000 ); return new request.options(ribbonconnectiontimeout, ribbonreadtimeout); } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/zhangjianbin/p/7228606.html