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.

Masatoshi Tada

October 07, 2019
Tweet

More Decks by Masatoshi Tada

Other Decks in Technology

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. 3 ms1 ms2 ms3 ms4 Recovery
  3. 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>
  4. 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
  5. Architecture (C) CASAREAL, Inc. All rights reserved. 8 CircuitBreaker Config

    CircuitBreaker Registry Circuit Breaker Circuit Breaker Circuit Breaker
  6. 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
  7. (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
  8. (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
  9. (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
  10. 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 ...
  11. States Transition (C) CASAREAL, Inc. All rights reserved. 15 CLOSED

    OPEN HALF_OPEN Failure rate >= threshold Duration >= waiting time
  12. 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
  13. Sliding Window pCount-based pThe last N Calls pTime-based pThe last

    N seconds (C) CASAREAL, Inc. All rights reserved. 17
  14. [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
  15. 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