熔断机制基本原理
- 类比电路熔断器:熔断机制来源于电路中的熔断器概念。当电路中电流过大时,熔断器会熔断,从而切断电路,防止电器设备因电流过载而损坏。在微服务架构中,当某个服务调用出现大量失败(如超时、异常等),达到一定阈值时,就像电路中电流过载一样,触发熔断机制。
- 状态转换:
- 关闭(Closed)状态:正常情况下,服务调用处于关闭状态,熔断器不生效,请求正常通过,同时熔断器会统计请求的成功、失败、超时等指标。
- 打开(Open)状态:当失败请求达到设定的阈值时,熔断器进入打开状态。此时,后续请求不再实际调用下游服务,而是直接返回一个预设的 fallback 响应,快速失败,避免大量无效请求占用资源。
- 半打开(Half - Open)状态:打开状态持续一段时间后,熔断器进入半打开状态。在半打开状态下,会允许少量请求通过,去试探下游服务是否恢复正常。如果这些试探请求成功,说明下游服务可能已恢复,熔断器切换回关闭状态;如果试探请求仍然失败,熔断器再次回到打开状态。
Spring Cloud 中实现熔断机制的方式
- Hystrix:
- 关键配置:
- 在
pom.xml
中引入 Hystrix 依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring - cloud - starter - netflix - hystrix</artifactId>
</dependency>
- 在启动类上添加 `@EnableHystrix` 注解开启 Hystrix 功能:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.netflix.hystrix.contrib.javanica.annotation.EnableHystrix;
@SpringBootApplication
@EnableHystrix
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
- 配置 Hystrix 相关参数,如熔断阈值、超时时间等,在 `application.properties` 中:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 5000
hystrix.command.default.circuitBreaker.requestVolumeThreshold = 20
hystrix.command.default.circuitBreaker.errorThresholdPercentage = 50
- 代码实现思路:使用
@HystrixCommand
注解标注需要熔断保护的方法,并指定 fallback 方法。例如:
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
@Service
public class YourService {
@HystrixCommand(fallbackMethod = "fallbackMethod")
public String yourMethod() {
// 实际调用下游服务的代码
return "Success response from downstream service";
}
public String fallbackMethod() {
// fallback 逻辑,当原方法调用失败时执行
return "Fallback response";
}
}
- Resilience4j:
- 关键配置:
- 在
pom.xml
中引入 Resilience4j 依赖:
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j - circuitbreaker</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j - retry</artifactId>
</dependency>
- 在 `application.properties` 中配置熔断相关参数:
resilience4j.circuitbreaker.instances.default.failureRateThreshold = 50
resilience4j.circuitbreaker.instances.default.waitDurationInOpenState = 10s
resilience4j.circuitbreaker.instances.default.permittedNumberOfCallsInHalfOpenState = 5
- 代码实现思路:通过
CircuitBreaker
工厂创建 CircuitBreaker
实例,并使用 CircuitBreaker
装饰器来包装需要熔断保护的方法。例如:
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import org.springframework.stereotype.Service;
@Service
public class YourService {
private final CircuitBreaker circuitBreaker;
public YourService(CircuitBreakerRegistry circuitBreakerRegistry) {
this.circuitBreaker = circuitBreakerRegistry.circuitBreaker("default");
}
public String yourMethod() {
return circuitBreaker.executeSupplier(() -> {
// 实际调用下游服务的代码
return "Success response from downstream service";
});
}
public String fallbackMethod() {
// fallback 逻辑,当原方法调用失败时执行
return "Fallback response";
}
}