pom引入
<!-- 熔断器 : https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-spring-boot3 -->
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot3</artifactId>
<version>2.2.0</version>
</dependency>
配置文件
resilience4j:
# 熔断器配置
circuitbreaker:
instances:
doctorService: # 熔断器实例名称
slidingWindowSize: 10 # 滑动窗口大小,表示用于计算失败率的请求数量
minimumNumberOfCalls: 5 # 最小调用次数,必须达到此次数才能计算失败率
failureRateThreshold: 50 # 失败率阈值,超过此比例将触发熔断(百分比)
waitDurationInOpenState: 20000ms # 熔断器打开状态持续时间,30秒后进入半开状态
permittedNumberOfCallsInHalfOpenState: 3 # 半开状态下允许的请求数量
使用
@CircuitBreaker(name = "doctorService", fallbackMethod = "fallback")
public String queryDoctorByRestTemplate() {
return queryDoctorRestClient.queryDoctor("queryDoctor");
}
public String fallback(Throwable ex) {
log.error("查询医生失败, 触发熔断...");
return "查询医生失败, 触发熔断...";
}