前言
本篇博客主要是向大家介绍:@SentinelResource ,它用于定义资源,并提供可选的异常处理和 fallback 配置项。 通过demo演示,为大家介绍相关的属性。
按资源名称限流
- 启动Nacos成功
- 启动Sentinel成功
- pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>com.zcw.springcloud2020508</artifactId>
<groupId>com.zcw</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-sentinel-service8401</artifactId>
<dependencies>
<dependency>
<groupId>com.zcw</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--SpringCloud alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud alibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud alibaba sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--SpringBoot 整合web组件+actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- 创建Controller类
package com.zcw.springcloud.alibaba.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName : RateLimitController
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 09:36
*/
@RestController
public class RateLimitController {
@GetMapping("/byResource")
@SentinelResource(value = "byResource",blockHandler = "handleException")
public CommonResult byResource(){
return new CommonResult(200,"按自愿名称限流测试OK",
new Payment(2020L,"serial001"));
}
public CommonResult handleException(BlockException blockException){
return new CommonResult(444,blockException.getClass().getCanonicalName()+"\t服务不可用");
}
}
- 配置流控规则:
- 连续点击时,出现如下图所示:
表示1秒钟内查询次数大于1,就跑到我们自定义的处流,限流 - 关闭8401服务测试一下下:
竟然是临时的。。。。
按照Url地址限流
通过访问的URL来限流,会返回Sentinel自带默认的限流处理信息。
@GetMapping("/rateLimit/byUrl")
@SentinelResource(value = "byUrl")
public CommonResult byUrl(){
return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));
}
- 修改我们Sentinel控制台
疯狂点击会报如下错误:
- 竟然没有返回:如下结果,百思不得其解
上面兜底方案面临的问题
1.系统默认的,没有体现我们自己的业务要求
2.依照现有条件,我们自定义的处理方法又和业务代码耦合在一块,不直观。
3.每个业务方法都添加一个兜底的,那代码膨胀加剧。
4.全局统一的处理方法没有体现。
自定义限流处理逻辑
- 创建CustomerBlockHandler类用于自定义限流处理逻辑
【自定义限流处理类 CustomerBlockHandler】
package com.zcw.springcloud.alibaba.myhandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
/**
* @ClassName : CustomerBlockHandler
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-28 10:18
*/
public class CustomerBlockHandler {
public static CommonResult handlerException(BlockException blockException){
return new CommonResult(444,"自定义");
}
}
- RateLimitController
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
blockHandlerClass = CustomerBlockHandler.class,
blockHandler = "handlerException")
public CommonResult customerBlockHandler(){
return new CommonResult(200,"自定义限流规则",new Payment(2020L,"serial002"));
}
- Sentinel控制台配置
- 测试
- 进一步说明,我们可以配置多个兜底的方法:
属性说明讲解
下面是官方地址:
https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
Sentinel主要有三个核心的API
- SphU定义资源
- Tracer定义统计
- ContextUtil定义了上下文