Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Resilience4j异常处理_Java_Spring Boot_Microservices_Circuit Breaker_Resilience4j - Fatal编程技术网

Java Resilience4j异常处理

Java Resilience4j异常处理,java,spring-boot,microservices,circuit-breaker,resilience4j,Java,Spring Boot,Microservices,Circuit Breaker,Resilience4j,我正试图通过使用Resilience4j库将容错功能集成到微服务中。 我有: 构建.渐变: ... buildscript { ext { springBootVersion = '2.2.4.RELEASE' lombokVersion = '1.18.10' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boo

我正试图通过使用Resilience4j库将容错功能集成到微服务中。
我有:
构建.渐变

...
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'
}
...
@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";
    }
}
application.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
休息控制器

...
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'
}
...
@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";
    }
}
有两个端点:foo和bar
带有断路器注释的“foo”映射器,最终在N次故障后打开电路
“bar”映射器使用一些业务逻辑调用另一个方法,并调用用断路器注释包装的方法。在这种情况下,我无法达到打开状态,无法根据业务规则正确处理这些场景。我总是失败

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

谢谢

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