From ddceb5f3121924c7b658d41c1c5c2681965c5853 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 14 Jan 2020 15:06:26 +0800 Subject: [PATCH] add spring cloud sentinel circuitbreaker demo --- .../cloud/examples/ServiceApplication.java | 22 +++++++++++++++++-- .../cloud/examples/TestController.java | 17 ++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java index 17121bd9..cda038b7 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/ServiceApplication.java @@ -16,11 +16,18 @@ package com.alibaba.cloud.examples; +import java.util.Collections; + +import com.alibaba.cloud.circuitbreaker.sentinel.SentinelCircuitBreakerFactory; +import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder; import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.slots.block.RuleConstant; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.circuitbreaker.Customizer; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @@ -31,8 +38,7 @@ import org.springframework.web.client.RestTemplate; public class ServiceApplication { @Bean - @SentinelRestTemplate(blockHandler = "handleException", - blockHandlerClass = ExceptionUtil.class) + @SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class) public RestTemplate restTemplate() { return new RestTemplate(); } @@ -47,6 +53,18 @@ public class ServiceApplication { return new JsonFlowRuleListConverter(); } + @Bean + public Customizer defaultConfig() { + return factory -> { + factory.configureDefault( + id -> new SentinelConfigBuilder().resourceName(id) + .rules(Collections.singletonList(new DegradeRule(id) + .setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100) + .setTimeWindow(10))) + .build()); + }; + } + public static void main(String[] args) { SpringApplication.run(ServiceApplication.class, args); } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java index 1e7a909d..ffacf273 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/TestController.java @@ -19,6 +19,7 @@ package com.alibaba.cloud.examples; import com.alibaba.csp.sentinel.annotation.SentinelResource; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @@ -32,6 +33,9 @@ public class TestController { @Autowired private RestTemplate restTemplate; + @Autowired + private CircuitBreakerFactory circuitBreakerFactory; + @GetMapping("/hello") @SentinelResource("resource") public String hello() { @@ -54,4 +58,17 @@ public class TestController { return restTemplate.getForObject("http://www.taobao.com/test", String.class); } + @GetMapping("/slow") + public String slow() { + return circuitBreakerFactory.create("show").run(() -> { + try { + Thread.sleep(1000L); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + return "success"; + }, throwable -> "fallback"); + } + }