简体   繁体   English

Resilience4j 异常处理

[英]Resilience4j exception handling

I'm trying to integrate fault tolerance in a microservice by using Resilience4j library.我正在尝试通过使用 Resilience4j 库将容错集成到微服务中。
I have:我有:
build.gradle : build.gradle

...
buildscript {
ext {
    springBootVersion = '2.2.4.RELEASE'
    lombokVersion = '1.18.10'
}
repositories {
    mavenCentral()
 }
 dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
 }
}
plugins {
    id 'org.springframework.boot' version '2.2.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.sample'
sourceCompatibility = '11'
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR8")
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-aop'
    implementation 'org.springframework.boot:spring-boot-configuration-processor'


    compile 'io.github.resilience4j:resilience4j-spring-boot2:1.7.0'
    implementation 'io.micrometer:micrometer-registry-prometheus'
}

application.yml file:应用程序.yml 文件:

resilience4j.circuitbreaker:
  configs:
    default:
      registerHealthIndicator: true
      slidingWindowSize: 5
      minimumNumberOfCalls: 5
      permittedNumberOfCallsInHalfOpenState: 3
      automaticTransitionFromOpenToHalfOpenEnabled: true
      waitDurationInOpenState: 5s
      failureRateThreshold: 50
      eventConsumerBufferSize: 10
      recordExceptions:
        - org.springframework.web.client.HttpServerErrorException
        - java.util.concurrent.TimeoutException
        - java.io.IOException
      ignoreExceptions:
        - com.example.githubtest.BusinessException
    shared:
      slidingWindowSize: 100
      permittedNumberOfCallsInHalfOpenState: 30
      waitDurationInOpenState: 1s
      failureRateThreshold: 50
      eventConsumerBufferSize: 10
      ignoreExceptions:
        - com.example.githubtest.BusinessException
  instances:
    serviceA:
      baseConfig: default

Rest Controller : Rest Controller

...
@RestController
public class MyController {
    private final RestTemplate rest;

    public MyController() { this.rest = new RestTemplate(); }

    @GetMapping(path = "foo")
    @CircuitBreaker(name = "serviceA", fallbackMethod = "customFallback")
    public String foo() {
        throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "This is a remote exception");
    }
    @GetMapping(path = "bar")
    public String bar() {
        // Does not get to OPEN state
        return invokeService();
    }

    @CircuitBreaker(name = "serviceA", fallbackMethod = "customFallback")
    public String invokeService() {
        throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "This is a remote exception");
    }

    public String customFallback(Exception e) {
        if (e instanceof CallNotPermittedException) {
            System.out.println("Call no permitted!");
        }
        System.out.println(e.getMessage());
        return "Fallback default return";
    }
}

There are 2 endpoints: foo & bar有 2 个端点:foo & bar
"foo" mapper wrapped with circuit breaker annotation which eventually opens the circuit after N failures “foo”映射器包裹着断路器注释,最终在 N 次故障后打开电路
"bar" mapper invokes another method with some business logic and invokes a method wrapped with circuit breaker annotation. “bar”映射器使用一些业务逻辑调用另一个方法,并调用一个用断路器注释包装的方法。 In this case, I'm not able to reach OPEN state to handle these scenarios properly according to business rules.在这种情况下,我无法达到 OPEN state 以根据业务规则正确处理这些场景。 I always get failures.我总是失败。

What should I do or change in order to start reaching OPEN state in the second case to be able to handle call not permitted exceptions properly?在第二种情况下,我应该做什么或改变才能开始达到 OPEN state 以便能够正确处理不允许的呼叫异常?
Thank you谢谢

Because of the way Spring AOP works, the proxies are skipped if you invoke an annotated method from within the same class.由于 Spring AOP 的工作方式,如果您从同一个 class 中调用带注释的方法,则会跳过代理。 You have to extract invokeService() into another bean/class.您必须将invokeService()提取到另一个 bean/类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM