Spring認證|Spring Cloud官方介紹

語言: CN / TW / HK

原標題:Spring認證|Spring Cloud官方介紹

Spring Cloud 提供了多種不同的功能,允許您在自己的應用程式中使用一致性的 API,開發人員選擇最適合您的應用程式需求的功能。

支援的實現

Netfix Hystrix

彈性4J

哨兵

春季重試

核心概念

要在您的程式碼中建立生產,您可以使用CircuitBreakerFactoryAPI。當您在類路徑中包含Spring Cloud Circuit Breaker starter 時,將自動為您建立一個實現此API 的bean。給個使用此API 的非常簡單的例子

@服務

公共靜態類 DemoControllerService {

私人休息模板休息;

私人斷路器工廠 cbFactory;

公共 DemoControllerService(RestTemplate 休息,CircuitBreakerFactory cbFactory){

this.rest = 休息;

this.cbFactory = cbFactory;

}

公共字串慢(){

return cbFactory.create("slow").run(() -> rest.getForObject("/slow", String.class), throwable -> "fallback");

}

}

該CircuitBreakerFactory.createAPI將建立一個命名類的一個例項CircuitBreaker。該執行方法採用一個供應商和一個功能。這就是您要包裝在供應時中的程式碼。Function是法院跳閘這將執行的回退。將傳遞Throwable導致回退被觸發的函式。如果您不想提供後備,您可以選擇退出後備。

反應式程式碼中的

如果 Project Reactor 在類路徑上,那麼您也可以使用 ReactiveCircuitBreakerFactory 用於響應式程式碼。

@服務

公共靜態類 DemoControllerService {

私有 ReactiveCircuitBreakerFactory cbFactory;

私有 WebClient webClient;

公共演示控制器服務(WebClient webClient,ReactiveCircuitBreakerFactory cbFactory){

this.webClient = webClient;

this.cbFactory = cbFactory;

}

公共單聲道慢(){

return webClient.get().uri("/slow").retrieve().bodyToMono(String.class).transform(

it -> cbFactory.create("slow").run(it, throwable -> return Mono.just("fallback")));

}

}

該ReactiveCircuitBreakerFactory.createAPI 將建立一個命名類的一個例項ReactiveCircuitBreaker。該方法應該採用MonoorFlux 使其包裝在國外中。您可以選擇配置一個回退功能,如果回跳閘通過Throwable導致失敗退,則該回退將被呼叫。

Spring啟動配置

Spring Cloud BOM 提供以下啟動器

Hystrix - org.springframework.cloud:spring-cloud-starter-netflix-hystrix

彈性4J - org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j

反應性彈性4J - org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j

春季重試—— org.springframework.cloud:spring-cloud-starter-circuitbreaker-spring-retry

哨兵—— org.springframework.cloud:spring-cloud-starter-circuitbreaker-sentinal

未完待續……