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

    View full-size slide

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




    View full-size slide

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


    View full-size slide

  4. pCreated by Netflix
    pCurrently in maintenance mode
    (C) CASAREAL, Inc. All rights reserved. 4

    View full-size slide

  5. Resilience4j
    pCircuit Breaker
    pBulkhead
    pRate Limiter
    pRetry
    pMonitoring with Prometheus
    p...
    (C) CASAREAL, Inc. All rights reserved. 5

    View full-size slide

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

    io.github.resilience4j

    resilience4j-spring-boot2

    1.0.0


    org.springframework.boot

    spring-boot-starter-aop

    View full-size slide

  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

    View full-size slide

  8. Architecture
    (C) CASAREAL, Inc. All rights reserved. 8
    CircuitBreaker
    Config
    CircuitBreaker
    Registry
    Circuit
    Breaker
    Circuit
    Breaker
    Circuit
    Breaker

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  13. Monitoring with Prometheus
    pAdd 3 dependencies:
    pspring-boot-starter-actuator
    presilience4j-micrometer
    pmicrometer-registry-prometheus
    (C) CASAREAL, Inc. All rights reserved. 13

    View full-size slide

  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
    ...

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  17. Sliding Window
    pCount-based
    pThe last N Calls
    pTime-based
    pThe last N seconds
    (C) CASAREAL, Inc. All rights reserved. 17

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide