如何解决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'
}
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
休息控制器:
...
@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 状态,我应该做什么或改变什么才能正确处理不允许调用的异常?
谢谢
解决方法
由于 Spring AOP 的工作方式,如果您从同一类中调用带注释的方法,则会跳过代理。您必须将 invokeService()
提取到另一个 bean/类中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。