mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add spring cloud sentinel circuitbreaker demo
This commit is contained in:
parent
b70129c17f
commit
ddceb5f312
@ -16,11 +16,18 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.examples;
|
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.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
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.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@ -31,8 +38,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
public class ServiceApplication {
|
public class ServiceApplication {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@SentinelRestTemplate(blockHandler = "handleException",
|
@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
|
||||||
blockHandlerClass = ExceptionUtil.class)
|
|
||||||
public RestTemplate restTemplate() {
|
public RestTemplate restTemplate() {
|
||||||
return new RestTemplate();
|
return new RestTemplate();
|
||||||
}
|
}
|
||||||
@ -47,6 +53,18 @@ public class ServiceApplication {
|
|||||||
return new JsonFlowRuleListConverter();
|
return new JsonFlowRuleListConverter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Customizer<SentinelCircuitBreakerFactory> 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) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ServiceApplication.class, args);
|
SpringApplication.run(ServiceApplication.class, args);
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ package com.alibaba.cloud.examples;
|
|||||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -32,6 +33,9 @@ public class TestController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CircuitBreakerFactory circuitBreakerFactory;
|
||||||
|
|
||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
@SentinelResource("resource")
|
@SentinelResource("resource")
|
||||||
public String hello() {
|
public String hello() {
|
||||||
@ -54,4 +58,17 @@ public class TestController {
|
|||||||
return restTemplate.getForObject("http://www.taobao.com/test", String.class);
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user