首页 > 解决方案 > Resilience4j 异常处理

问题描述

我正在尝试通过使用 Resilience4j 库将容错集成到微服务中。
我有:
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'
}

应用程序.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

休息控制器

...
@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";
    }
}

有 2 个端点: foo 和 bar
“foo” 映射器用断路器注释包装,最终在 N 次故障后打开电路
“bar” 映射器调用另一个具有一些业务逻辑的方法,并调用一个用断路器注释包装的方法。在这种情况下,我无法达到 OPEN 状态以根据业务规则正确处理这些场景。我总是失败。

为了在第二种情况下开始达到 OPEN 状态,以便能够正确处理不允许调用的异常,我应该做什么或改变什么?
谢谢

标签: javaspring-bootmicroservicescircuit-breakerresilience4j

解决方案


由于 Spring AOP 的工作方式,如果您从同一类中调用带注释的方法,则会跳过代理。您必须提取invokeService()到另一个 bean/类。


推荐阅读