【Spring Cloud】分布式必学springcloud(八)——配置Feign重试机制

一、前言

      在上一篇博客中,小编向大家介绍了Feign的负载均衡和断路器的使用。在这篇博客中,小编向大家介绍一下Ribbon在Feign的配置以及Feign的重试机制。

二、Ribbon配置

      通过小编上一篇博客介绍,Feign的底层是通过Ribbon实现的。所以我们可以通过配置Ribbon对Feign进行自定义配置。

2.1 全局配置

      全局配置很简单,直接使用ribbon.属性=值 ,的方式来设置ribbon的各项参数。

      如,修改默认客户端调用超时时间:

ribbon:
    ConnectTimeout: 500
    ReadTimeout: 5000

2.2 指定服务配置

      多数情况下,我们需要根据具体情况具体分析,所以针对不同的服务需要具体的配置。根据指定的服务进行特殊的配置。

      指定服务配置跟前几篇博客中配置ribbon指定负载均衡策略一样: 服务名.ribbon.属性=值 .

      如:client1为服务名,属性在下面介绍

client1:
  ribbon:
    MaxAutoRetries: 1
    MaxAutoRetriesNextServer: 2
    ConnectTimeout: 5000
    ReadTimeout: 2000
    OkToRetryOnAllOperations: true

三、相关配置介绍

配置 说明
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 断路器的超时时间需要大于ribbon的超时时间,不然不会触发重试。
hello-service.ribbon.ConnectTimeout 请求连接的超时时间
hello-service.ribbon.ReadTimeout 请求处理的超时时间
hello-service.ribbon.OkToRetryOnAllOperations 是否对所有操作请求都进行重试
hello-service.ribbon.MaxAutoRetriesNextServer 重试负载均衡其他的实例最大重试次数,不包括首次server
hello-service.ribbon.MaxAutoRetries 同一台实例最大重试次数,不包括首次调用

四、Feign测试

4.1 添加重试配置文件

      设置请求处理时间 readtimeout 为 2000,另外设置hystrix的超时时间要大于readtimeout时间。

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 7001
spring:
  application:
    name: feign-service

feign:
  hystrix:
    enabled: true


client1:
  ribbon:
    #配置首台服务器重试1次
    MaxAutoRetries: 1
    #配置其他服务器重试两次
    MaxAutoRetriesNextServer: 2
    #链接超时时间
    ConnectTimeout: 500
    #请求处理时间
    ReadTimeout: 2000
    #每个操作都开启重试机制
    OkToRetryOnAllOperations: true

#配置断路器超时时间,默认是1000(1秒)
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 2001

4.2 设置提供者随机超时

      提供者1:设置随机线程休眠0~3000毫秒。

package com.wl.client.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * Created by Ares on 2018/4/11.
 */
@RestController
public class User {

    @GetMapping("/user/findById")
    public String findById(@RequestParam("id")String id) throws InterruptedException {
        int i = new Random().nextInt(3000);
        System.out.println("client线程休眠时间:"+i);
        Thread.sleep(i);
        return "这个是springcloud的客户端1----"+id;
    }


}

      提供者2:设置随机线程休眠0~3000毫秒。

package com.wl.client.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * Created by Ares on 2018/4/11.
 */
@RestController
public class User {

    @GetMapping("/user/findById")
    public String findById(@RequestParam("id")String id) throws InterruptedException {
        int i = new Random().nextInt(3000);
        System.out.println("clientc线程休眠时间:"+i);
        Thread.sleep(i);
        return "这个是springcloud的客户端3----"+id;
    }


}

4.3 运行

4.3.1 测试1 随机0~3000延迟

      依次运行eureka,两个提供者,feign。

扫描二维码关注公众号,回复: 1054821 查看本文章

这里写图片描述

      查看打印的日志:

这里写图片描述

这里写图片描述

4.3.2 测试2 3000延迟

      3000延迟,线程绝对会直接重试。可以通过这个例子体会一下MaxAutoRetries和MaxAutoRetriesNextServer的区别。

这里写图片描述

      运行结果说明:开始请求访问的时候,clientapplication(1)经过负载均衡分配,开始执行,但是执行3000超时,所以重试了一次,重试依旧是超时。所以安排给其他的提供者clientapplication处理,他处理第一次,超时,处理第二次也超时,然后就给其他的提供者处理,clientapplication(1)处理两次后依旧超时。这时候,所有的提供者都重试完毕,所以就返回失败了。

      可以通过上面的体会MaxAutoRetries和MaxAutoRetriesNextServer的区别:

  • MaxAutoRetries:首个处理的提供者,重试的次数

  • MaxAutoRetriesNextServer:首个提供者无法处理给其他提供者处理,重试的次数,首个提供者也会作为其他提供者,所有的提供者都重试失败,则返回失败。

五、小结

      通过这次重试的学习,我们可以针对特殊的服务设置超时时间,然后对不同的服务进行处理,非常的方便。另外Feign的默认的重试次数是5次。在用dubbo的时候,重试3次。

猜你喜欢

转载自blog.csdn.net/kisscatforever/article/details/80048395