Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Hystrix To Resilience4j

From Hystrix To Resilience4j

Lightning Talk in SpringOne Platform 2019.

5dbaf4015e7f249ab21b195ced8e9e46?s=128

Masatoshi Tada

October 07, 2019
Tweet

Transcript

  1. From Hystrix To Resilience4j Masatoshi Tada (@suke_masa) Casareal, Inc. 2019-10-07

    SpringOne Platform LT (C) CASAREAL, Inc. All rights reserved. 1
  2. Circuit Breaker in 30 secs (C) CASAREAL, Inc. All rights

    reserved. 2 ms1 ms2 ms3 ms4
  3. Circuit Breaker in 30 secs (C) CASAREAL, Inc. All rights

    reserved. 3 ms1 ms2 ms3 ms4 Recovery
  4. pCreated by Netflix pCurrently in maintenance mode (C) CASAREAL, Inc.

    All rights reserved. 4
  5. Resilience4j pCircuit Breaker pBulkhead pRate Limiter pRetry pMonitoring with Prometheus

    p... (C) CASAREAL, Inc. All rights reserved. 5
  6. Spring Boot Starter (C) CASAREAL, Inc. All rights reserved. 6

    <dependency> <groupId>io.github.resilience4j</groupId> <artifactId> resilience4j-spring-boot2</artifactId> <version> 1.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-aop</artifactId> </dependency>
  7. application.yml (C) CASAREAL, Inc. All rights reserved. 7 resilience4j.circuitbreaker.instances: helloApi:

    failure-rate-threshold: 50 sliding-window-type: COUNT_BASED ... Circuit Breaker name
  8. Architecture (C) CASAREAL, Inc. All rights reserved. 8 CircuitBreaker Config

    CircuitBreaker Registry Circuit Breaker Circuit Breaker Circuit Breaker
  9. Creating Circuit Breaker (C) CASAREAL, Inc. All rights reserved. 9

    @Service public class HelloService { public HelloService(... , CircuitBreakerRegistry cbr) { ... this.circuitBreaker = cbr.circuitBreaker("helloApi"); } ... Circuit Breaker name
  10. (a) Functional Style (C) CASAREAL, Inc. All rights reserved. 10

    public Hello hello() { CheckedFunction0<Hello> function = circuitBreaker.decorateCheckedSupplier( // API access () -> restTemplate.getForObject( apiUrl, Hello.class) ); // To next slide Decorating API access
  11. (a) Functional Style (C) CASAREAL, Inc. All rights reserved. 11

    return Try.of(function) // Recover failure .recover(t -> recoverHello(t)) .get(); } // Fallback method private Hello recoverHello(Throwable t) { ... } Fallback
  12. (b) Annotation Style (C) CASAREAL, Inc. All rights reserved. 12

    @CircuitBreaker(name = "helloApi", fallbackMethod = "recoverHello") public Hello hello() { ... } // Fallback method private Hello recoverHello(Throwable t) { ... } Fallback
  13. Monitoring with Prometheus pAdd 3 dependencies: pspring-boot-starter-actuator presilience4j-micrometer pmicrometer-registry-prometheus (C)

    CASAREAL, Inc. All rights reserved. 13
  14. Monitoring with Prometheus pGET /actuator/prometheus (C) CASAREAL, Inc. All rights

    reserved. 14 # HELP resilience4j_circuitbreaker_calls_seconds Total number of successful calls # TYPE resilience4j_circuitbreaker_calls_seconds summary resilience4j_circuitbreaker_calls_seconds_count{kin d="failed",name="helloApi",} 0.0 ...
  15. States Transition (C) CASAREAL, Inc. All rights reserved. 15 CLOSED

    OPEN HALF_OPEN Failure rate >= threshold Duration >= waiting time
  16. States Transition (C) CASAREAL, Inc. All rights reserved. 16 CLOSED

    OPEN HALF_OPEN Failure rate >= threshold Duration >= waiting time Failure rate >= threshold Failure rate < threshold
  17. Sliding Window pCount-based pThe last N Calls pTime-based pThe last

    N seconds (C) CASAREAL, Inc. All rights reserved. 17
  18. Other features pBulkhead pSemaphoreBulkhead pFixedThreadPoolBulkhead pRate Limiter pCache pRetry pTime

    Limiter (C) CASAREAL, Inc. All rights reserved. 18
  19. [FYI] Spring Cloud Circuit Breaker pCircuit Breakers abstraction pResilience4j pHystrix

    pSentinel (by Alibaba) pSpring Retry pActuator endpoints are NOT provided (C) CASAREAL, Inc. All rights reserved. 19
  20. Conclusion pResilience4j is awesome! pYou should try it! pMy sample

    codes: https://github.com/MasatoshiTada/my-first- resilience4j (C) CASAREAL, Inc. All rights reserved. 20