@Bulkhead
原理是利用Spring Aop進行增強,@Bulkhead聲明在Class上,該Class所有public method會做隔離處理,聲明在特定method上,只有該特定method纔會做隔離處理。
BulkheadAspect
Bulkhead利用BulkheadAspect作爲切面容器進行隔離處理,實現org.springframework.core.Ordered,實現Pointcut按優先級切入。
The Resilience4j Aspects order is following:
Retry > CircuitBreaker > RateLimiter > Bulkhead
構造方法
//Bulkhead基於SpringBoot的自動配置
private final BulkheadConfigurationProperties bulkheadConfigurationProperties;
/*Bulkhead註冊容器,只能管理被Spring管理的bean創建的Bulkhead實例*/
private final BulkheadRegistry bulkheadRegistry;
//Bulkhead切面擴展默認支持(RxJava2BulkheadAspectExt、ReactorBulkheadAspectExt)
private final List<BulkheadAspectExt> bulkheadAspectExts;
public BulkheadAspect(BulkheadConfigurationProperties backendMonitorPropertiesRegistry, BulkheadRegistry bulkheadRegistry, @Autowired(required = false) List<BulkheadAspectExt> bulkheadAspectExts) {
this.bulkheadConfigurationProperties = backendMonitorPropertiesRegistry;
this.bulkheadRegistry = bulkheadRegistry;
this.bulkheadAspectExts = bulkheadAspectExts;
}
定義切入點
只能有Bulkhead class or Bulkhead annotation
@Pointcut(value = "@within(Bulkhead) || @annotation(Bulkhead)", argNames = "Bulkhead")
public void matchAnnotatedClassOrMethod(Bulkhead Bulkhead) {
}
熔斷處理@Around
BulkheadAspect-> bulkheadAroundAdvice() 方法是resilience4j利用Spring Aop做隔離處理邏輯處。
getBackendMonitoredAnnotation(ProceedingJoinPoint proceedingJoinPoint) 和 getOrCreateBulkhead(String methodName, String backend) 與CircuitBreakerAspect實現類似,不再敘述。
handleJoinPoint
Bulkhead核心接口實現
注意點:
(1)若不採用官方推薦默認Spring Boot自動配置方式配置Bulkhead,注意Bulkhead實例必須被Spring 管理bean創建,否則會創建默認配置的Bulkhead實例。
(2)注意@Bulkhead聲明在Class和Method上的作用域。
(3)基於AOP實現的Bulkhead相關bean在BulkheadConfiguration中配置。
(4)BulkheadAspect會根據切入點method.returnType選擇合適的處理策略。
下圖爲Bulkhead切面容器BulkheadAspect處理熔斷的大體流程圖
BulkheadAspect::handleJoinPoint> Bulkhead::executeCheckedSupplier 默認處理Java類型的邏輯會在後續章節講解。