环境:Springboot2.6.14 + Spring Cloud2021.0.5
概述
Spring Cloud Circuit breaker提供了一个跨不同断路器实现的抽象。它为你的应用程序提供了一致的API,让你(开发人员)选择最适合你应用程序需求的断路器实现。
Spring Cloud支持以下断路器实现:
- Resilience4J
- Sentinel
- Spring Retry
核心概念
要在代码中创建断路器,可以使用CircuitBreakerFactory API。当你在classpath中包含SpringCloud CircuitBreaker启动器时,会自动为你创建一个实现了这个API的bean。下面的例子展示了如何使用这个API:
@Service
public static class DemoControllerService {
private RestTemplate rest;
private CircuitBreakerFactory cbFactory;
public DemoControllerService(RestTemplate rest, CircuitBreakerFactory cbFactory) {
this.rest = rest;
this.cbFactory = cbFactory;
}
public String slow() {
return cbFactory.create("slow").run(() -> rest.getForObject("/slow", String.class), throwable -> "fallback");
}
}
以上是断路器的基本使用方法。CircuitBreakerFactory.create API创建一个名为CircuitBreaker的类实例。run方法接受一个Supplier和一个Function作为参数。Supplier是要封装在断路器中的代码。该函数是在断路器跳闸时运行的备用机制也就是会调用run方法的第二个参数Function。
实现原理
- 自动配置
在上面我们所看到的API,如:CircuitBreaker,CircuitBreakerFactory这些接口(抽象类)的定义都在Spring Cloud Commons包中定义,而具体的实现我们还需要自己根据情况引入。如Resilience4J或者Sentinel,本篇内容使用Resilience4J。
首先,引入Ressilience4J自动配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
引入该依赖后自动配置CircuitBreakerFactory的实现
// 可以通过spring.cloud.circuitbreaker.resilience4j.enabled控制是否启用,默认开启
@EnableConfigurationProperties(Resilience4JConfigurationProperties.class)
@ConditionalOnProperty(name = { "spring.cloud.circuitbreaker.resilience4j.enabled",
"spring.cloud.circuitbreaker.resilience4j.blocking.enabled" }, matchIfMissing = true)
public class Resilience4JAutoConfiguration {
@Autowired(required = false)
private List<Customizer<Resilience4JCircuitBreakerFactory>> customizers = new ArrayList<>();
@Bean
@ConditionalOnMissingBean(CircuitBreakerFactory.class)
public Resilience4JCircuitBreakerFactory resilience4jCircuitBreakerFactory(
CircuitBreakerRegistry circuitBreakerRegistry, TimeLimiterRegistry timeLimiterRegistry,
@Autowired(required = false) Resilience4jBulkheadProvider bulkheadProvider,
Resilience4JConfigurationProperties resilience4JConfigurationProperties) {
Resilience4JCircuitBreakerFactory factory = new Resilience4JCircuitBreakerFactory(circuitBreakerRegistry,
timeLimiterRegistry, bulkheadProvider, resilience4JConfigurationProperties);
customizers.forEach(customizer -> customizer.customize(factory));
return factory;
}
}
有了上面的工厂类Resilience4JCircuitBreakerFactory后我们就可以通过该工厂进行实际的开发了。
在上面的Bean创建方法参数中有几个非常重要的核心类:
- CircuitBreakerRegistry
该类用来配置注册创建CircuitBreaker实例。
- TimeLimiterRegistry
该类用来注册配置每一个实例(CircuitBreaker)的时间限制。
- Resilience4jBulkheadProvider
该类主要是提供了隔离机制,其内通过BulkheadRegistry及
ThreadPoolBulkheadRegistry注册配置每一个实例的隔离机制及线程池隔离配置。
- 核心类Resilience4JCircuitBreakerFactory
在需要进行熔断降级操作都需用通过该工厂进行创建CircuitBreaker实例
public class Resilience4JCircuitBreakerFactory {
private CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
private TimeLimiterRegistry timeLimiterRegistry = TimeLimiterRegistry.ofDefaults();
private ExecutorService executorService = Executors.newCachedThreadPool();
private Function<String, Resilience4JConfigBuilder.Resilience4JCircuitBreakerConfiguration> defaultConfiguration;
public Resilience4JCircuitBreakerFactory(CircuitBreakerRegistry circuitBreakerRegistry,
TimeLimiterRegistry timeLimiterRegistry, Resilience4jBulkheadProvider bulkheadProvider,
Resilience4JConfigurationProperties resilience4JConfigurationProperties) {
this.circuitBreakerRegistry = circuitBreakerRegistry;
this.timeLimiterRegistry = timeLimiterRegistry;
this.bulkheadProvider = bulkheadProvider;
// 获取默认的配置(当没有为具体的id实例创建配置时,使用该默认配置)
this.defaultConfiguration = id -> new Resilience4JConfigBuilder(id)
.circuitBreakerConfig(this.circuitBreakerRegistry.getDefaultConfig())
.timeLimiterConfig(this.timeLimiterRegistry.getDefaultConfig()).build();
this.resilience4JConfigurationProperties = resilience4JConfigurationProperties;
}
}
创建CircuitBreaker实例
public Resilience4JCircuitBreaker create(String id) {
return create(id, id, this.executorService);
}
private Resilience4JCircuitBreaker create(String id, String groupName,
ExecutorService circuitBreakerExecutorService) {
// 根据实例id获取对应的配置,如果不存在则使用默认的配置
// 初始是没有对应id的配置
Resilience4JConfigBuilder.Resilience4JCircuitBreakerConfiguration config = getConfigurations()
.computeIfAbsent(id, defaultConfiguration);
// spring.cloud.circuitbreaker.resilience4j.disableThreadPool 默认false
if (resilience4JConfigurationProperties.isDisableThreadPool()) {
return new Resilience4JCircuitBreaker(id, groupName, config.getCircuitBreakerConfig(),
config.getTimeLimiterConfig(), circuitBreakerRegistry, timeLimiterRegistry,
Optional.ofNullable(circuitBreakerCustomizers.get(id)), bulkheadProvider);
} else {
// 创建CircuitBreaker实例,这里的config.getCircuitBreakerConfig和config.getTimeLimiterConfig
// 两个方法返回的默认的配置(首次)
return new Resilience4JCircuitBreaker(id, groupName, config.getCircuitBreakerConfig(),
config.getTimeLimiterConfig(), circuitBreakerRegistry, timeLimiterRegistry,
circuitBreakerExecutorService, Optional.ofNullable(circuitBreakerCustomizers.get(id)),bulkheadProvider);
}
}
Resilience4JCircuitBreaker实例
public class Resilience4JCircuitBreaker implements CircuitBreaker {
public Resilience4JCircuitBreaker(String id, String groupName,
io.github.resilience4j.circuitbreaker.CircuitBreakerConfig circuitBreakerConfig,
TimeLimiterConfig timeLimiterConfig, CircuitBreakerRegistry circuitBreakerRegistry,
TimeLimiterRegistry timeLimiterRegistry, ExecutorService executorService,
Optional<Customizer<io.github.resilience4j.circuitbreaker.CircuitBreaker>> circuitBreakerCustomizer,
Resilience4jBulkheadProvider bulkheadProvider) {
this.id = id;
this.groupName = groupName;
this.circuitBreakerConfig = circuitBreakerConfig;
this.registry = circuitBreakerRegistry;
this.timeLimiterRegistry = timeLimiterRegistry;
this.timeLimiterConfig = timeLimiterConfig;
this.executorService = executorService;
// ...
}
public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
// ...
// 下面的CircuitBreaker和TimeLimiter都会先从Registry中获取,不存在则使用默认的
// 从CircuitBreakerRegistry获取对应Id实例配置,如果不存在则返回的就是当前circuitBreakerConfig
io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry.circuitBreaker(this.id,
this.circuitBreakerConfig, tags);
// 如果对应Id实例配置不存在则应用默认的timeLimiterConfig配置
TimeLimiter timeLimiter = timeLimiterRegistry.timeLimiter(id, timeLimiterConfig, tags);
// ...
}
}
CircuitBreakerRegistry注册器
该注册器将配置文件中配置的实例配置进行注册并提供默认的配置
@Import({CircuitBreakerConfigurationOnMissingBean.class, FallbackConfigurationOnMissingBean.class})
public class CircuitBreakerAutoConfiguration {}
CircuitBreakerConfigurationOnMissingBean该类注册CircuitBreakerRegistry
@Configuration
public class CircuitBreakerConfigurationOnMissingBean extends AbstractCircuitBreakerConfigurationOnMissingBean {
public CircuitBreakerConfigurationOnMissingBean(CircuitBreakerConfigurationProperties circuitBreakerProperties) {
super(circuitBreakerProperties);
}
}
属性配置
// 相关配置都在该属性配置中
@ConfigurationProperties(prefix = "resilience4j.circuitbreaker")
public class CircuitBreakerProperties extends CircuitBreakerConfigurationProperties {}
AbstractCircuitBreakerConfigurationOnMissingBean
@Configuration
@Import({FallbackConfigurationOnMissingBean.class, SpelResolverConfigurationOnMissingBean.class})
public abstract class AbstractCircuitBreakerConfigurationOnMissingBean {
protected final CircuitBreakerConfiguration circuitBreakerConfiguration;
protected final CircuitBreakerConfigurationProperties circuitBreakerProperties;
public AbstractCircuitBreakerConfigurationOnMissingBean(CircuitBreakerConfigurationProperties circuitBreakerProperties) {
this.circuitBreakerProperties = circuitBreakerProperties;
this.circuitBreakerConfiguration = new CircuitBreakerConfiguration(circuitBreakerProperties);
}
@Bean
@ConditionalOnMissingBean
public CircuitBreakerRegistry circuitBreakerRegistry(
EventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry,
RegistryEventConsumer<CircuitBreaker> circuitBreakerRegistryEventConsumer,
@Qualifier("compositeCircuitBreakerCustomizer") CompositeCustomizer<CircuitBreakerConfigCustomizer> compositeCircuitBreakerCustomizer) {
// 创建实例
return circuitBreakerConfiguration
.circuitBreakerRegistry(eventConsumerRegistry, circuitBreakerRegistryEventConsumer,compositeCircuitBreakerCustomizer);
}
}
CircuitBreakerConfiguration
@Configuration
public class CircuitBreakerConfiguration {
private final CircuitBreakerConfigurationProperties circuitBreakerProperties;
public CircuitBreakerConfiguration(CircuitBreakerConfigurationProperties circuitBreakerProperties) {
this.circuitBreakerProperties = circuitBreakerProperties;
}
@Bean
public CircuitBreakerRegistry circuitBreakerRegistry(
EventConsumerRegistry<CircuitBreakerEvent> eventConsumerRegistry,
RegistryEventConsumer<CircuitBreaker> circuitBreakerRegistryEventConsumer,
@Qualifier("compositeCircuitBreakerCustomizer") CompositeCustomizer<CircuitBreakerConfigCustomizer> compositeCircuitBreakerCustomizer) {
// 将resilience4j.circuitbreaker.configs所有配置进行注册
CircuitBreakerRegistry circuitBreakerRegistry = createCircuitBreakerRegistry(
circuitBreakerProperties, circuitBreakerRegistryEventConsumer,
compositeCircuitBreakerCustomizer);
//...
// 将resilience4j.circuitbreaker.instances所有配置进行注册
initCircuitBreakerRegistry(circuitBreakerRegistry, compositeCircuitBreakerCustomizer);
return circuitBreakerRegistry;
}
}
上面的configs和instances所有的配置都会注册到AbstractRegistry.entryMap中。
获取实例Id配置
io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry.circuitBreaker(this.id,this.circuitBreakerConfig, tags);
// InMemoryCircuitBreakerRegistry
public CircuitBreaker circuitBreaker(String name, CircuitBreakerConfig config,io.vavr.collection.Map<String, String> tags) {
return computeIfAbsent(name, () -> CircuitBreaker.of(name, Objects.requireNonNull(config, CONFIG_MUST_NOT_BE_NULL), getAllTags(tags)));
}
// AbstractRegistry
protected E computeIfAbsent(String name, Supplier<E> supplier) {
// 从map获取
return entryMap.computeIfAbsent(Objects.requireNonNull(name, NAME_MUST_NOT_BE_NULL), k -> {
E entry = supplier.get();
eventProcessor.processEvent(new EntryAddedEvent<>(entry));
return entry;
});
}
TimeLimiterRegistry注册器
该配置与CircuitBreakerRegistry的配置类似,这里不赘述
以上就是Resilience4J的实现原理
Resilience4J配置
resilience4j:
circuitbreaker:
#configs:
# default:
# minimumNumberOfCalls: 5 #默认配置
instances:
#在Feign中使用的命名
DemoFeignformat:
minimumNumberOfCalls: 5
lkk:
minimumNumberOfCalls: 2
timelimiter:
configs:
#为任何调用提供默认的配置
default:
timeoutDuration: 1s
instances:
DemoFeignformat:
timeoutDuration: 1s
DemoFeigndate3:
timeoutDuration: 5s
lkk:
timeoutDuration: 1s
在上面Feign的命名规则如下:
如果开启了如下配置:
feign:
circuitbreaker:
enabled: true
alphanumericIds:
enabled: true
public class FeignClientFactoryBean {
<T> T getTarget() {
Feign.Builder builder = feign(context);
}
}
public final class FeignCircuitBreaker {
public static final class Builder extends Feign.Builder {
public Feign build(final FallbackFactory<?> nullableFallbackFactory) {
// FeignCircuitBreakerInvocationHandler执行调用
super.invocationHandlerFactory((target, dispatch) -> new FeignCircuitBreakerInvocationHandler(circuitBreakerFactory,
feignClientName, target, dispatch, nullableFallbackFactory,
circuitBreakerGroupEnabled, circuitBreakerNameResolver));
return super.build();
}
}
}
FeignCircuitBreakerInvocationHandler
class FeignCircuitBreakerInvocationHandler implements InvocationHandler {
private final CircuitBreakerNameResolver circuitBreakerNameResolver;
public Object invoke() {
String circuitName = circuitBreakerNameResolver.resolveCircuitBreakerName(feignClientName, target, method);
CircuitBreaker circuitBreaker = circuitBreakerGroupEnabled ? factory.create(circuitName, feignClientName) : factory.create(circuitName);
}
}
static class AlphanumericCircuitBreakerNameResolver extends DefaultCircuitBreakerNameResolver {
@Override
public String resolveCircuitBreakerName(String feignClientName, Target<?> target, Method method) {
return super.resolveCircuitBreakerName(feignClientName, target, method).replaceAll("[^a-zA-Z0-9]", "");
}
}
static class DefaultCircuitBreakerNameResolver implements CircuitBreakerNameResolver {
@Override
public String resolveCircuitBreakerName(String feignClientName, Target<?> target, Method method) {
return Feign.configKey(target.type(), method);
}
}
public abstract class Feign {
// 开启了Alphanumeric会将特殊字符全部删除
public static String configKey(Class targetType, Method method) {
StringBuilder builder = new StringBuilder();
builder.append(targetType.getSimpleName());
builder.append('#').append(method.getName()).append('(');
for (Type param : method.getGenericParameterTypes()) {
param = Types.resolve(targetType, targetType, param);
builder.append(Types.getRawType(param).getSimpleName()).append(',');
}
if (method.getParameterTypes().length > 0) {
builder.deleteCharAt(builder.length() - 1);
}
return builder.append(')').toString();
}
}
以上就是基于Resilience4J的实现原理及配置实现