我试图通过使用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: defaultRest控制器
...
@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“映射器用一些业务逻辑调用另一个方法,并调用一个包含断路器注释的方法。在这种情况下,我无法达到开放状态,无法根据业务规则正确地处理这些场景。我总是失败。
在第二种情况下,为了能够正确地处理不允许的调用异常,我应该做什么或更改以开始达到开放状态?
谢谢
发布于 2021-02-04 07:40:57
由于Spring的工作方式,如果您从同一个类中调用带注释的方法,则会跳过代理。您必须将invokeService()提取到另一个bean/类中。
https://stackoverflow.com/questions/66015349
复制相似问题