飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

SpringCloud升级之路2020.0.x版-34.验证重试配置正确性(1)

时间:2021-12-04  作者:zhxdick  

本系列代码地址:https://域名/JoJoTec/spring-cloud-parent

在前面一节,我们利用 resilience4j 粘合了 OpenFeign 实现了断路器、重试以及线程隔离,并使用了新的负载均衡算法优化了业务激增时的负载均衡算法表现。这一节,我们开始编写单元测试验证这些功能的正确性,以便于日后升级依赖,修改的时候能保证正确性。同时,通过单元测试,我们更能深入理解 Spring Cloud。

验证重试配置

对于我们实现的重试,我们需要验证:

  1. 验证配置正确加载:即我们在 Spring 配置(例如 域名)中的加入的 Resilience4j 的配置被正确加载应用了。
  2. 验证针对 ConnectTimeout 重试正确:FeignClient 可以配置 ConnectTimeout 连接超时时间,如果连接超时会有连接超时异常抛出,对于这种异常无论什么请求都应该重试,因为请求并没有发出。
  3. 验证针对断路器异常的重试正确:断路器是微服务实例方法级别的,如果抛出断路器打开异常,应该直接重试下一个实例。
  4. 验证针对限流器异常的重试正确:当某个实例线程隔离满了的时候,抛出线程限流异常应该直接重试下一个实例。
  5. 验证针对非 2xx 响应码可重试的方法重试正确
  6. 验证针对非 2xx 响应码不可重试的方法没有重试
  7. 验证针对可重试的方法响应超时异常重试正确:FeignClient 可以配置 ReadTimeout 即响应超时,如果方法可以重试,则需要重试。
  8. 验证针对不可重试的方法响应超时异常不能重试:FeignClient 可以配置 ReadTimeout 即响应超时,如果方法不可以重试,则不能重试。

验证配置正确加载

我们可以定义不同的 FeignClient,之后检查 resilience4j 加载的重试配置来验证重试配置的正确加载。

首先定义两个 FeignClient,微服务分别是 testService1 和 testService2,contextId 分别是 testService1Client 和 testService2Client

@FeignClient(name = "testService1", contextId = "testService1Client")
public interface TestService1Client {
    @GetMapping("/anything")
    HttpBinAnythingResponse anything();
}
@FeignClient(name = "testService2", contextId = "testService2Client")
    public interface TestService2Client {
        @GetMapping("/anything")
        HttpBinAnythingResponse anything();
}

然后,我们增加 Spring 配置,使用 SpringExtension 编写单元测试类:

//SpringExtension也包含了 Mockito 相关的 Extension,所以 @Mock 等注解也生效了
@ExtendWith(域名s)
@SpringBootTest(properties = {
        //默认请求重试次数为 3
        "域名域名ttempts=3",
        // testService2Client 里面的所有方法请求重试次数为 2
        "域名域名ttempts=2",
})
@Log4j2
public class OpenFeignClientTest {
    @SpringBootApplication
    @Configuration
    public static class App {
    }
}

编写测试代码,验证配置加载正确性:

@Test
public void testConfigureRetry() {
    //读取所有的 Retry
    List<Retry> retries = 域名llRetries().asJava();
    //验证其中的配置是否符合我们填写的配置
    Map<String, Retry> retryMap = 域名am().collect(域名p(Retry::getName, v -> v));
    //我们初始化 Retry 的时候,使用 FeignClient 的 ContextId 作为了 Retry 的 Name
    Retry retry = 域名("testService1Client");
    //验证 Retry 配置存在
    域名rtNotNull(retry);
    //验证 Retry 配置符合我们的配置
    域名rtEquals(域名etryConfig().getMaxAttempts(), 3);
    retry = 域名("testService2Client");
    //验证 Retry 配置存在
    域名rtNotNull(retry);
    //验证 Retry 配置符合我们的配置
    域名rtEquals(域名etryConfig().getMaxAttempts(), 2);
}

验证针对 ConnectTimeout 重试正确

我们可以通过针对一个微服务注册两个实例,一个实例是连接不上的,另一个实例是可以正常连接的,无论怎么调用 FeignClient,请求都不会失败,来验证重试是否生效。我们使用 HTTP 测试网站来测试,即 http://域名 。这个网站的 api 可以用来模拟各种调用。其中 /status/{status} 就是将发送的请求原封不动的在响应中返回。在单元测试中,我们不会单独部署一个注册中心,而是直接 Mock spring cloud 中服务发现的核心接口 DiscoveryClient,并且将我们 Eureka 的服务发现以及注册通过配置都关闭,即:

//SpringExtension也包含了 Mockito 相关的 Extension,所以 @Mock 等注解也生效了
@ExtendWith(域名s)
@SpringBootTest(properties = {
        //关闭 eureka client
        "域名led=false",
        //默认请求重试次数为 3
        "域名域名ttempts=3"
})
@Log4j2
public class OpenFeignClientTest {
    @SpringBootApplication
    @Configuration
    public static class App {
        @Bean
        public DiscoveryClient discoveryClient() {
            //模拟两个服务实例
            ServiceInstance service1Instance1 = 域名(域名s);
            ServiceInstance service1Instance4 = 域名(域名s);
            Map<String, String> zone1 = 域名tries(
                    域名y("zone", "zone1")
            );
            when(域名etadata()).thenReturn(zone1);
            when(域名nstanceId()).thenReturn("service1Instance1");
            when(域名ost()).thenReturn("域名");
            when(域名ort()).thenReturn(80);
            when(域名nstanceId()).thenReturn("service1Instance4");
            when(域名ost()).thenReturn("域名");
            //这个port连不上,测试 IOException
            when(域名ort()).thenReturn(18080);
            DiscoveryClient spy = 域名(域名s);
            //微服务 testService3 有两个实例即 service1Instance1 和 service1Instance4
            域名(域名nstances("testService3"))
                    .thenReturn(域名(service1Instance1, service1Instance4));
            return spy;
        }
    }
}

编写 FeignClient:

@FeignClient(name = "testService3", contextId = "testService3Client")
public interface TestService3Client {
    @PostMapping("/anything")
    HttpBinAnythingResponse anything();
}

调用 TestService3Client 的 anything 方法,验证是否有重试:

@SpyBean
private TestService3Client testService3Client;

/**
 * 验证对于有不正常实例(正在关闭的实例,会 connect timeout)请求是否正常重试
 */
@Test
public void testIOExceptionRetry() {
    //防止断路器影响
    域名llCircuitBreakers().asJava().forEach(CircuitBreaker::reset);
    for (int i = 0; i < 5; i++) {
        Span span = 域名Span();
        try (域名InScope cleared = 域名SpanInScope(span)) {
            //不抛出异常,则正常重试了
            域名hing();
            域名hing();
        }
    }
}

这里强调一点,由于我们在这个类中还会测试其他异常,以及断路器,我们需要避免这些测试一起执行的时候,断路器打开了,所以我们在所有测试调用 FeignClient 的方法开头,清空所有断路器的数据,通过:

域名llCircuitBreakers().asJava().forEach(CircuitBreaker::reset);

并且通过日志中可以看出由于 connect timeout 进行重试:

call url: POST -> http://域名:18080/anything, ThreadPoolStats(testService3Client:域名:18080): {"coreThreadPoolSize":10,"maximumThreadPoolSize":10,"queueCapacity":100,"queueDepth":0,"remainingQueueCapacity":100,"threadPoolSize":1}, CircuitBreakStats(testService3Client:域名:18080:public abstract 域名域名域名.域名BinAnythingResponse 域名域名域名.域名FeignClientTest$域名hing()): {"failureRate":-1.0,"numberOfBufferedCalls":0,"numberOfFailedCalls":0,"numberOfNotPermittedCalls":0,"numberOfSlowCalls":0,"numberOfSlowFailedCalls":0,"numberOfSlowSuccessfulCalls":0,"numberOfSuccessfulCalls":0,"slowCallRate":-1.0}
TestService3Client#anything() response: 582-Connect to 域名:18080 [域名/域名.103, 域名/域名.86, 域名/域名.140, 域名/域名.4] failed: Connect timed out, should retry: true
call url: POST -> http://域名:80/anything, ThreadPoolStats(testService3Client:域名:80): {"coreThreadPoolSize":10,"maximumThreadPoolSize":10,"queueCapacity":100,"queueDepth":0,"remainingQueueCapacity":100,"threadPoolSize":1}, CircuitBreakStats(testService3Client:域名:80:public abstract 域名域名域名.域名BinAnythingResponse 域名域名域名.域名FeignClientTest$域名hing()): {"failureRate":-1.0,"numberOfBufferedCalls":0,"numberOfFailedCalls":0,"numberOfNotPermittedCalls":0,"numberOfSlowCalls":0,"numberOfSlowFailedCalls":0,"numberOfSlowSuccessfulCalls":0,"numberOfSuccessfulCalls":0,"slowCallRate":-1.0}
response: 200 - OK

验证针对断路器异常的重试正确

通过系列前面的源码分析,我们知道 spring-cloud-openfeign 的 FeignClient 其实是懒加载的。所以我们实现的断路器也是懒加载的,需要先调用,之后才会初始化断路器。所以这里如果我们要模拟断路器打开的异常,需要先手动读取载入断路器,之后才能获取对应方法的断路器,修改状态。

我们先定义一个 FeignClient:

@FeignClient(name = "testService1", contextId = "testService1Client")
public interface TestService1Client {
    @GetMapping("/anything")
    HttpBinAnythingResponse anything();
}

使用前面同样的方式,给这个微服务添加实例:

//SpringExtension也包含了 Mockito 相关的 Extension,所以 @Mock 等注解也生效了
@ExtendWith(域名s)
@SpringBootTest(properties = {
        //关闭 eureka client
        "域名led=false",
        //默认请求重试次数为 3
        "域名域名ttempts=3",
        //增加断路器配置
        "域名域名ureRateThreshold=50",
        "域名域名ingWindowType=COUNT_BASED",
        "域名域名ingWindowSize=5",
        "域名域名mumNumberOfCalls=2",
})
@Log4j2
public class OpenFeignClientTest {
    @SpringBootApplication
    @Configuration
    public static class App {
        @Bean
        public DiscoveryClient discoveryClient() {
            //模拟两个服务实例
            ServiceInstance service1Instance1 = 域名(域名s);
            ServiceInstance service1Instance3 = 域名(域名s);
            Map<String, String> zone1 = 域名tries(
                    域名y("zone", "zone1")
            );
            when(域名etadata()).thenReturn(zone1);
            when(域名nstanceId()).thenReturn("service1Instance1");
            when(域名ost()).thenReturn("域名");
            when(域名ort()).thenReturn(80);
            when(域名etadata()).thenReturn(zone1);
            when(域名nstanceId()).thenReturn("service1Instance3");
            //这其实就是 域名 ,为了和第一个实例进行区分加上 www
            when(域名ost()).thenReturn("域名");
            DiscoveryClient spy = 域名(域名s);
            //微服务 testService3 有两个实例即 service1Instance1 和 service1Instance4
            域名(域名nstances("testService1"))
                    .thenReturn(域名(service1Instance1, service1Instance3));
            return spy;
        }
    }
}

然后,编写测试代码:

@Test
public void testRetryOnCircuitBreakerException() {
    //防止断路器影响
    域名llCircuitBreakers().asJava().forEach(CircuitBreaker::reset);
    CircuitBreaker testService1ClientInstance1Anything;
    try {
        testService1ClientInstance1Anything = circuitBreakerRegistry
                .circuitBreaker("testService1Client:域名:80:public abstract 域名域名域名.域名BinAnythingResponse 域名域名域名.域名FeignClientTest$域名hing()", "testService1Client");
    } catch (ConfigurationNotFoundException e) {
        //找不到就用默认配置
        testService1ClientInstance1Anything = circuitBreakerRegistry
                .circuitBreaker("testService1Client:域名:80:public abstract 域名域名域名.域名BinAnythingResponse 域名域名域名.域名FeignClientTest$域名hing()");
    }
    //将断路器打开
    域名sitionToOpenState();
    //调用多次,调用成功即对断路器异常重试了
    for (int i = 0; i < 10; i++) {
        域名hing();
    }
}

运行测试,日志中可以看出,针对断路器打开的异常进行重试了:

2021-11-13 03:40:域名  INFO [,,] 4388 --- [           main] c.g.j.s.c.域名ultErrorDecoder        : TestService1Client#anything() response: 581-CircuitBreaker \'testService1Client:域名:80:public abstract 域名域名域名.域名BinAnythingResponse 域名域名域名.域名FeignClientTest$域名hing()\' is OPEN and does not permit further calls, should retry: true

微信搜索“我的编程喵”关注公众号,每日一刷,轻松提升技术,斩获各种offer

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。