前言

Feign组件默认使用Ribbon的重试机制并增加了根据状态码判断重试机制,默认情况下是不启用的。Feign使用的是Spring Retry组件,需要引入依赖才能启用。

一、POM引入Spring Retry

<dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
  </dependency>

二、配置文件

eureka-client:
   ribbon:
      MaxAutoRetries: 1
      MaxAutoRetriesNextServer: 1
      retryableStatusCodes: 500,404
      OkToRetryOnAllOperations: true
      NFLoadBalancerRuleClassName: com.netflix.loadbalancer.AvailabilityFilteringRule #负载均衡规则

eureka-client是自己的serverId,MaxAutoRetries同一台服务器上的最大重试次数(不包括第一次尝试),MaxAutoRetriesNextServer要重试的下一个服务器的最大数量(不包括第一个服务器),retryableStatusCodes可以根据接口返回的状态码判断是否重试其他服务,OkToRetryOnAllOperations只对所有的超时请求重试

注意: Ribbon的重试机制只有对GET请求或者设置了OkToRetryOnAllOperations生效 详情请查看源码:

public class RibbonLoadBalancedRetryPolicy implements LoadBalancedRetryPolicy {
    ...
        public Boolean canRetry(LoadBalancedRetryContext context) {
        HttpMethod method = context.getRequest().getMethod();
        return HttpMethod.GET == method || lbContext.isOkToRetryOnAllOperations();
    }
    ...
}

Feign对返回状态码做了重试判断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());
        }
        ...
    }
    ...
}

重试机制用的是Spring Retry组件当抛出异常时进行重试!

GET请求指的是feign client 请求其他client时声明的那个interface中mapping注解类型,RequestMapping不设置method默认为GET请求

@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);
}

写在最后

最新文章

  1. 1-安装kvm及虚拟机
  2. Hadoop系列教程&lt;一&gt;---Hadoop是什么呢?
  3. 移动前端中viewport(视口) 转
  4. SQL注入测试平台 SQLol -2.SELECT注入测试
  5. Win32 SDK程序创建一些控件(简单调用InitCommonControlsEx,并指定ICC_LISTVIEW_CLASSES控件就可以了)
  6. 来自JavaScript Garden摘取
  7. 阿里云服务器无法远程其他的mysql服务器
  8. 宏HASH_DELETE
  9. java中判断Object对象类型
  10. jquery字体更改后的鼠标-影像学改变//凝视内容
  11. mysql很全的和完整的总结
  12. C#读写Excel的几种方法
  13. SLAM学习--开源测试数据集合
  14. 发送http请求的方法
  15. 9/24matplotlib使用入门
  16. 学习OpenCV——SVM
  17. 如何用word文档在博客里发表文章
  18. arcgis10.2怎么把地理坐标系转化为投影坐标系(平面,米制坐标) arcmap 10.2 从 WGS_1984 转 Beijing_1954
  19. 面试整理(1):原生ajax
  20. iOS: NSURLConnection详解

热门文章

  1. PHP array_keys()
  2. poj 1331 Multiply
  3. POJ 1260-Pearls(DP)
  4. D3js-API介绍【中】
  5. 从零讲Java,给你一条清晰地学习道路!该学什么就学什么!
  6. FOBiz组合模糊查询
  7. luogu2679 子串
  8. 修改select默认样式
  9. document.getElementById方法在火狐和谷歌浏览器兼容
  10. redhat6 改 centos yum 源