Alibaba Sentinel流控详解
java1234
共 9741字,需浏览 20分钟
·
2021-06-13 18:17
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
概述
1.QPS测试
@GetMapping(value = "/hello")
public String hello() {
return "Hello Sentinel1";
}
Hello Sentinel1
Blocked by Sentinel (flow limiting)
2.线程数测试
@GetMapping(value = "/hello")
public String hello() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello Sentinel1";
}
3.关联测试
@GetMapping(value = "/hello")
public String hello() {
return "Hello Sentinel1";
}
@GetMapping(value = "/hello2")
public String hello2() {
return "Hello Sentinel2";
}
4.链路测试
@Autowired
private TestService testService;
@GetMapping(value = "/hello")
public String hello() {
testService.listOrder();
return "Hello Sentinel1";
}
@GetMapping(value = "/hello2")
public String hello2() {
testService.listOrder();
return "Hello Sentinel2";
}
@Service
public class TestService {
//资源信息
@SentinelResource("listOrder")
public void listOrder(){
System.out.println("listOrder");
}
}
5.快速失败
@ControllerAdvice
@RestController
public class CommonException {
@ExceptionHandler(BlockException.class)
public String customException(Exception e) {
return "系统出小差,请稍后再试。";
}
@ExceptionHandler(UndeclaredThrowableException.class)
public String undeclaredThrowableException(Exception e) {
return "系统出小差,请稍后再试。";
}
@ExceptionHandler(Exception.class)
public String exception(Exception e) {
return "系统出小差,请稍后再试。";
}
}
@Configuration
public class Config {
static {
WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
@Override
public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
// 简单演示,这里可以自行处理
PrintWriter out = response.getWriter();
out.print("try again later");
out.flush();
out.close();
}
});
}
}
@SentinelResource(value = "helloResource" ,blockHandler = "getOrderDowngradeRtTypeFallback",fallback = "helloFallback")
@GetMapping(value = "/hello")
public String hello(@RequestParam(value = "id") Long id) {
if(id==1l){
throw new RuntimeException("1231");
}
return "SUCCESS";
}
public String helloFallback(Long id,Throwable e) {
System.out.println(1111);
return id+"helloFallback";
}
public String getOrderDowngradeRtTypeFallback(Long id,BlockException ex) {
return "服务降级啦,当前服务器请求次数过多,请稍后重试!";
}
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
6.Warm Up
7.排队等待
本文链接:
https://blog.csdn.net/qq_30285985/article/details/107692608
评论