zoukankan      html  css  js  c++  java
  • springcloud第二篇:Resilience4j

    Resilience4j是用来替代Hystrix的一个开源组件。

    引入依赖:

    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-spring-boot2</artifactId>
        <version>1.3.1</version>
        <exclusions>
            <exclusion>
                <groupId>io.github.resilience4j</groupId>
                <artifactId>resilience4j-circuitbreaker</artifactId>
            </exclusion>
            <exclusion>
                <groupId>io.github.resilience4j</groupId>
                <artifactId>resilience4j-timelimiter</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-circuitbreaker</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-timelimiter</artifactId>
        <version>1.3.1</version>
    </dependency>

    用Resilience4j实现重试:

    1、用@Retry标注调用下游服务的接口方法,用name属性指定这个重试的名称。我们可以针对不同的调用下游服务制定不同的重试策略

    @FeignClient(name = "spring-cloud-eureka-client-producer")
    @Component
    public interface MyInterface {
    @Retry(name = "helloRetry")
    @PostMapping(value = "/hello")
    String hello(@RequestBody Person person);
    }

    2、在配置文件中添加

    resilience4j.retry.retry-aspect-order=1
    resilience4j.retry.backends.helloRetry.max-retry-attempts=3
    resilience4j.retry.backends.helloRetry.wait-duration=2000
    resilience4j.retry.backends.helloRetry.event-consumer-buffer-size=1
    resilience4j.retry.backends.helloRetry.enable-exponential-backoff=false
    resilience4j.retry.backends.helloRetry.exponential-backoff-multiplier=2
    resilience4j.retry.backends.helloRetry.enable-randomized-wait=false
    resilience4j.retry.backends.helloRetry.randomized-wait-factor=2
    resilience4j.retry.backends.helloRetry.retry-exception-predicate=com.kou.springbooteurekaconsumerdemo.HelloRetryExceptionPredicate

    retry-aspect-order指定重试优先级,max-retry-attempts指定最多执行多少次,wait-duration指定上次执行失败后多少毫秒后才重试,

    3、新建步骤2中用到的HelloRetryExceptionPredicate类

    public class HelloRetryExceptionPredicate implements Predicate<Throwable> {
        @Override
        public boolean test(Throwable throwable) {
            if (throwable instanceof FeignException) {
                FeignException e = (FeignException) throwable;
                Throwable cause = e.getCause();
                return cause instanceof IOException;
            }
            return false;
        }
    }

    这个类指定了只有调下游服务抛IOException时才重试。当然,我们也可以指定404、50X时也重试。

    如果不想为每一个调用都指定重试策略,而是想所有的调用都指定同一个重试策略,那么只需指定所有调用的@Retry注解的name属性值都相同就可以了。

    如何实现fallback呢?

    用Resilience4j实现断路器:

    1、用@CircuitBreaker标注调用下游服务的接口方法,用name属性指定这个重试的名称。我们可以针对不同的调用下游服务制定不同的重试策略

  • 相关阅读:
    PAT 1065. A+B and C (64bit) (20)
    PAT 1042. Shuffling Machine (20)
    PAT 1001. A+B Format (20)
    HDU 2082 找单词 母函数
    NYOJ 138 找球号(二) bitset 二进制的妙用
    POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星
    POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
    zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂
    NYOJ 323 Drainage Ditches 网络流 FF 练手
    POJ 1273 Drainage Ditches 网络流 FF
  • 原文地址:https://www.cnblogs.com/koushr/p/5873463.html
Copyright © 2011-2022 走看看