Foreword
Feign Ribbon component uses a retry mechanism and increases the retry mechanism according to the status code is determined by default, default is not enabled. Feign using Spring Retry components, you need to be introduced to enable dependent.
A, POM introduced Spring Retry
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
Second, the configuration file
eureka-client:
ribbon:
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 1
retryableStatusCodes: 500,404
OkToRetryOnAllOperations: true
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.AvailabilityFilteringRule #负载均衡规则
eureka-client
ServerId own, MaxAutoRetries
the maximum number of retries on the same server (not the first attempt), MaxAutoRetriesNextServer
the maximum number of retries to the next server (not including the first server), retryableStatusCodes
may be determined according to the status code returned by the interface whether to retry the other services OkToRetryOnAllOperations
only request retry timeout for all
Note : Ribbon retry mechanism only GET requests or set to take effect OkToRetryOnAllOperations details, please see source code:
public class RibbonLoadBalancedRetryPolicy implements LoadBalancedRetryPolicy {
...
public Boolean canRetry(LoadBalancedRetryContext context) {
HttpMethod method = context.getRequest().getMethod();
return HttpMethod.GET == method || lbContext.isOkToRetryOnAllOperations();
}
...
}
Feign return a status code to do a retry determination RetryableFeignLoadBalancer
public class RetryableFeignLoadBalancer extends FeignLoadBalancer
implements ServiceInstanceChooser {
...
[@Override](https://my.oschina.net/u/1162528)
public RibbonResponse execute(final RibbonRequest request,
IClientConfig configOverride) throws IOException {
...
if (retryPolicy != null
&& retryPolicy.retryableStatusCode(response.status())) {
byte[] byteArray = response.body() == null ? new byte[] {}
: StreamUtils
.copyToByteArray(response.body().asInputStream());
response.close();
throw new RibbonResponseStatusCodeException(
RetryableFeignLoadBalancer.this.clientName, response,
byteArray, request.getUri());
}
...
}
...
}
Retry mechanism is used to retry when Spring Retry components when an exception is thrown!
GET request refers to the interface feign when the client requests from other client statement in mapping annotation type, RequestMapping not set the default method is GET request
@FeignClient("stores")
public interface StoreClient {
@RequestMapping(method = RequestMethod.GET, value = "/stores")
List<Store> getStores();
@RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
Store update(@PathVariable("storeId") long storeId, Store store);
}
Written in the last
- First: watching the thumbs up, thank you for your recognition of the author;
- ...
- Second: hand forward, to share knowledge, to allow more people to learn;
- ...
- Third: remember point concerns, updated daily! ! !
- ...