Spring Cloud 使用 Resilience4j 实现服务熔断

打印 上一主题 下一主题

主题 789|帖子 789|积分 2367

CircuitBreaker 断路器

服务熔断是为了保护我们的服务,比如当某个服务出现问题的时候,控制打向它的流量,让它有时间去恢复,或者限制一段时间只能有固定数量的请求打向这个服务。这些都是保护措施。我在实际工作中也确实遇到过,数据库出现问题了,进而导致Web服务出现问题了,导致不依赖数据库的服务也出现问题了,出现一连串问题。 这次学习《玩转 Spring 全家桶》,丁雪丰老师给了使用resilience4j的例子。 丁老师的例子是2019年的,这个框架已经修改了些方法,所以我自己也花了些时间来理解了它的用法。现将过程记录下来。
首先POM文件引入
  1. <dependency>
  2.     <groupId>io.github.resilience4j</groupId>
  3.     <artifactId>resilience4j-spring-boot2</artifactId>
  4.     <version>2.0.2</version>
  5. </dependency>
复制代码
接着改造之前的Controller方法
  1. @RestController
  2. @RequestMapping("/customer")
  3. @Slf4j
  4. public class BookController {
  5.     @Autowired
  6.     private BookService bookService;
  7.     private CircuitBreaker circuitBreaker;
  8.     public BookController(CircuitBreakerRegistry registry) {
  9.         circuitBreaker = registry.circuitBreaker("menu");
  10.     }
  11.     @GetMapping("/menu")
  12.     public List<Book> readMenu() {
  13.         Supplier<List<Book>> supplier = () -> bookService.getAll();
  14.         circuitBreaker.getEventPublisher()
  15.                 .onEvent(event -> log.info(event.toString()));
  16.         try{
  17.             return circuitBreaker.executeSupplier(supplier);
  18.         }
  19.         catch (Exception ex)
  20.         {
  21.             log.error(ex.getMessage());
  22.             return Collections.emptyList();
  23.         }
  24.     }
  25. }
复制代码
不同的地方就是引入了CircuitBreaker, 然后使用它将我们的方法“bookService.getAll()”包起来了。
然后在配置文件中添加如下的配置
  1. resilience4j.circuitbreaker.backends.menu.failure-rate-threshold=50
  2. resilience4j.circuitbreaker.backends.menu.wait-duration-in-open-state=60000
  3. resilience4j.circuitbreaker.backends.menu.sliding-window-size=5
  4. resilience4j.circuitbreaker.backends.menu.permitted-number-of-calls-in-half-open-state=2
  5. resilience4j.circuitbreaker.backends.menu.minimum-number-of-calls=2
复制代码
稍微解释一下这里的配置
failure-rate-threshold=50是说失败率超过50%就熔断,
wait-duration-in-open-state= 60000,是说熔断后等待60S才允许再次调用。
sliding-window-size =5 可以理解为5个请求统计一次,
permitted-number-of-calls-in-half-open-state = 2是说进入半开的状态的时候,还允许请求多少个。
minimum-number-of-calls=2是说最少有多少个请求才开始统计。 这里的参数都是我为了实验设置的,实际情况根据需要进行调整。参数比较多,具体可以参加官方文档
https://resilience4j.readme.io/docs/circuitbreaker
我们来看下实际的效果通过浏览器访问,
首先我们现打开BookService,让它有一次成功的请求,日志会输出
CircuitBreaker 'menu' recorded a successful call.
然后我们将BookService关闭,让它请求失败,日志会输出如下
CircuitBreaker 'menu' recorded an error: 'feign.RetryableException: Connection refused: no further information executing GET http://bookshop-service/book/getAll'. Elapsed time: 2050 ms
CircuitBreaker 'menu' exceeded failure rate threshold. Current failure rate: 50.0
CircuitBreaker 'menu' changed state from CLOSED to OPEN
可以看到断路器已经打开了,
接着我们继续访问会出现,
CircuitBreaker 'menu' recorded a call which was not permitted.
这个时候请求不会打到BookService上面了。就算这个时候我们的BookService恢复正常。
等待60s后进入半Open的状态
CircuitBreaker 'menu' changed state from OPEN to HALF_OPEN
这个时候恢复BookService正常,我们请求也会正常响应了
CircuitBreaker 'menu' recorded a successful call
多请求几次,断路器就从HALF_OPEN变成了CLOSED
CircuitBreaker 'menu' changed state from HALF_OPEN to CLOSED
这里给一个官方的状态图来说明

断路器有三个状态: CLOSED,  OPEN, HALF_OPEN。

  • CLOSED是最开始的状态,也就是关闭状态,流量可以正常通过,
  • 当失败比率超过threshold后,断路器打开, 变成OPEN 打开后流量不可以通过;
  • 等待一定的时间后,断路器进入半开状态 HALF_OPEN, 这个时候如果失败率低于阈值,断路器进入CLOSED状态,如果超过阈值,断路器继续保证OPEN,再等待,如此往复。
断路器现在还支持设置慢请求,使用起来还是比较方便。对于参数的设置如果不是很理解,可以通过单元测试的方法来加深对它的理解。这里参考https://github.com/eugenp/tutorials/blob/master/libraries-6/src/test/java/com/baeldung/resilence4j/Resilience4jUnitTest.java 上面的例子,给出来个单元测试
  1.     interface RemoteService {
  2.         int process(int i);
  3.     }
  4.     private RemoteService service;
  5.     @Test
  6.     public void whenCircuitBreakerIsUsed_thenItWorksAsExpected() {
  7.         service = mock(RemoteService.class);
  8.         CircuitBreakerConfig config = CircuitBreakerConfig.custom()
  9.                 // Percentage of failures to start short-circuit
  10.                 .failureRateThreshold(20)
  11.                 .minimumNumberOfCalls(5)
  12.                 .build();
  13.         CircuitBreakerRegistry registry = CircuitBreakerRegistry.of(config);
  14.         CircuitBreaker circuitBreaker = registry.circuitBreaker("my");
  15.         Function<Integer, Integer> decorated = CircuitBreaker.decorateFunction(circuitBreaker, service::process);
  16.         when(service.process(anyInt())).thenThrow(new RuntimeException());
  17.         circuitBreaker.getEventPublisher()
  18.                 .onEvent(event ->
  19.                 {
  20.                     log.info(event.toString());
  21.                 });
  22.         for (int i = 0; i < 10; i++) {
  23.             try {
  24.                 decorated.apply(i);
  25.             } catch (Exception ignore) {
  26.             }
  27.         }
  28.         verify(service, times(5)).process(any(Integer.class));
  29.     }
复制代码
这里设置最少请求5次,失败率超过20%就熔断,然后我们请求了10次,实际上只调用了Service5次。
对于其它参数,你可以调整后,根据需要来验证是否符合预期。它的日志输出如下
  1. CircuitBreaker 'my' recorded an error: 'java.lang.RuntimeException'. Elapsed time: 2 ms
  2. CircuitBreaker 'my' recorded an error: 'java.lang.RuntimeException'. Elapsed time: 0 ms
  3. CircuitBreaker 'my' recorded an error: 'java.lang.RuntimeException'. Elapsed time: 0 ms
  4. CircuitBreaker 'my' recorded an error: 'java.lang.RuntimeException'. Elapsed time: 0 ms
  5. CircuitBreaker 'my' recorded an error: 'java.lang.RuntimeException'. Elapsed time: 0 ms
  6. CircuitBreaker 'my' exceeded failure rate threshold. Current failure rate: 100.0
  7. CircuitBreaker 'my' changed state from CLOSED to OPEN
  8. CircuitBreaker 'my' recorded a call which was not permitted.
  9. CircuitBreaker 'my' recorded a call which was not permitted.
  10. CircuitBreaker 'my' recorded a call which was not permitted.
  11. CircuitBreaker 'my' recorded a call which was not permitted.
  12. CircuitBreaker 'my' recorded a call which was not permitted.  
复制代码
可以看到5次过后,就开始打开断路器,后面的call就不被允许了。
隔舱Bulkhead

Resilience4j 里面的Bulkhead可以简单的理解为允许多少个并发访问。我们这里还是通过单元测试的方法来演示它的功能
  1.     @Test
  2.     public void whenBulkheadIsUsed_thenItWorksAsExpected() throws InterruptedException {
  3.         service = mock(RemoteService.class);
  4.         BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).build();
  5.         BulkheadRegistry registry = BulkheadRegistry.of(config);
  6.         Bulkhead bulkhead = registry.bulkhead("my");
  7.         Function<Integer, Integer> decorated = Bulkhead.decorateFunction(bulkhead, service::process);
  8.        try {
  9.             callAndBlock(decorated);
  10.         }
  11.        catch(BulkheadFullException ex)
  12.         {
  13.             log.error("isfull");
  14.         }
  15.         finally
  16.         {
  17.            verify(service, times(2)).process(any(Integer.class));
  18.         }
  19.     }
  20.     private void callAndBlock(Function<Integer, Integer> decoratedService) throws InterruptedException {
  21.         when(service.process(anyInt())).thenAnswer(invocation -> {
  22.             log.info("service called");
  23.             return null;
  24.         });
  25.         ArrayList<Integer> numberList = new ArrayList<Integer>();
  26.         for(int i = 0;i<10;i++)
  27.         {
  28.             numberList.add(i);
  29.         }
  30.         numberList.parallelStream().forEach((i)->{
  31.             try {
  32.                 decoratedService.apply(i);
  33.             }
  34.             catch (Exception ex)
  35.             {
  36.                 log.error("meet error " + ex.getMessage());
  37.             } catch (Throwable e) {
  38.                 throw new RuntimeException(e);
  39.             }
  40.         });
  41.     }
复制代码
首先我们解读一下callAndBlock, 它会并发的去执行一个function. 如果我们不用隔舱,它的输出会是这样。
  1. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-4] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  2. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-9] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  3. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-5] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  4. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-3] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  5. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-1] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  6. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-6] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  7. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-7] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  8. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-8] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  9. 2022-12-28T15:22:52.010+08:00  INFO 37276 --- [onPool-worker-2] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  10. 2022-12-28T15:22:52.011+08:00  INFO 37276 --- [           main] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
复制代码
可以看到启动了10个线程去访问方法。加了隔舱后,隔舱限定了一次只能两个,输出如下
  1. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-4] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  2. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-6] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  3. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-7] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  4. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-4] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  5. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-8] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  6. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-5] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  7. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-2] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  8. 2022-12-28T15:33:48.648+08:00 ERROR 32256 --- [onPool-worker-3] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error Bulkhead 'my' is full and does not permit further calls
  9. 2022-12-28T15:33:48.650+08:00  INFO 32256 --- [onPool-worker-1] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  10. 2022-12-28T15:33:48.650+08:00  INFO 32256 --- [           main] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
复制代码
可以看到只有两次成功的访问,其它的访问都被block了。
限速器RateLimiter

RateLimiter的功能是限定一段时间内允许多少次访问,还是使用和Bulkhead一样的例子一样
  1. @Test
  2.     public void whenRateLimiterInUse_thenItWorksAsExpected() throws InterruptedException {
  3.         service = mock(RemoteService.class);
  4.         RateLimiterConfig config = RateLimiterConfig.custom()
  5.                 .limitRefreshPeriod(Duration.ofMillis(1000))
  6.                 .limitForPeriod(4)
  7.                 .timeoutDuration(Duration.ofMillis(25))
  8.                 .build();
  9.         RateLimiterRegistry rateLimiterRegistry = RateLimiterRegistry.of(config);
  10.         RateLimiter rateLimiter = rateLimiterRegistry
  11.                 .rateLimiter("name1");
  12.         CheckedFunction<Integer, Integer> decorated = RateLimiter
  13.                 .decorateCheckedFunction(rateLimiter, service::process);
  14.         try {
  15.             callAndBlock(decorated);
  16.         }
  17.         catch(Exception ex)
  18.         {
  19.             log.error("isfull");
  20.         }
  21.         finally
  22.         {
  23.             verify(service, times(4)).process(any(Integer.class));
  24.         }
  25.     }
  26.     private void callAndBlock(CheckedFunction<Integer, Integer> decoratedService) throws InterruptedException {
  27.         when(service.process(anyInt())).thenAnswer(invocation -> {
  28.             log.info("service called");
  29.             return null;
  30.         });
  31.         ArrayList<Integer> numberList = new ArrayList<Integer>();
  32.         for(int i = 0;i<10;i++)
  33.         {
  34.             numberList.add(i);
  35.         }
  36.         numberList.parallelStream().forEach((i)->{
  37.             try {
  38.                 decoratedService.apply(i);
  39.             }
  40.             catch (Exception ex)
  41.             {
  42.                 log.error("meet error " + ex.getMessage());
  43.             } catch (Throwable e) {
  44.                 throw new RuntimeException(e);
  45.             }
  46.         });
  47.     }
复制代码
我们这里故意设置1S中允许访问4次,实际的运行情况也是只允许了4次。日志输出如下
  1. 2022-12-28T15:39:52.027+08:00  INFO 35236 --- [onPool-worker-2] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  2. 2022-12-28T15:39:52.027+08:00  INFO 35236 --- [onPool-worker-5] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  3. 2022-12-28T15:39:52.027+08:00  INFO 35236 --- [           main] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  4. 2022-12-28T15:39:52.027+08:00  INFO 35236 --- [onPool-worker-7] c.k.r.bookcustomer.Resilience4jUnitTest  : service called
  5. 2022-12-28T15:39:52.053+08:00 ERROR 35236 --- [onPool-worker-6] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error RateLimiter 'name1' does not permit further calls
  6. 2022-12-28T15:39:52.060+08:00 ERROR 35236 --- [onPool-worker-3] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error RateLimiter 'name1' does not permit further calls
  7. 2022-12-28T15:39:52.060+08:00 ERROR 35236 --- [onPool-worker-9] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error RateLimiter 'name1' does not permit further calls
  8. 2022-12-28T15:39:52.060+08:00 ERROR 35236 --- [onPool-worker-1] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error RateLimiter 'name1' does not permit further calls
  9. 2022-12-28T15:39:52.060+08:00 ERROR 35236 --- [onPool-worker-8] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error RateLimiter 'name1' does not permit further calls
  10. 2022-12-28T15:39:52.075+08:00 ERROR 35236 --- [onPool-worker-4] c.k.r.bookcustomer.Resilience4jUnitTest  : meet error RateLimiter 'name1' does not permit further calls
复制代码
限速器这个功能只能限制在整体性能上面,如果要限制某个用户,只能某段时间访问多少次,它就做不到了。
Relilience4j 里面还提供了Retry,TimeLimiter,Cache. 感觉不是很有必要的功能, Retry在spring里面有相应的功能了,没有必要专门为了使用它而多加个包。 TimeLimiter,Cache 我感觉不是很受重视的功能,连例子文档都懒得提供,可见意义不大。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

去皮卡多

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表