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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SentinelDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<sentinel.version>1.8.6</sentinel.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
</parent>
<dependencies>
<!--Sentinel核心依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>${sentinel.version}</version>
</dependency>
<!--transport模块,以支持Sentinel控制台-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>${sentinel.version}</version>
</dependency>
<!--提供sentinel注解支持模块-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>${sentinel.version}</version>
</dependency>
<!--以下是spring boot web的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
此处我们添加了transport模块支持Sentinel控制台之间通信,添加了支持资源注解@SentinelResource的依赖。
Application启动类和配置和Spring Boot 一样,此处不再赘述。
注:如果是Spring Cloud Alibaba的项目,直接引入下面的start即可,上面Sentinel相关的依赖都可以不用加入
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
AOP配置
因为spring boot中使用了AOP,Sentinel的注解支持使用了Aspectj,所以需要添加如下配置
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SentinelAspectConfiguration {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}
注:此配置只需要在Spring Boot下配置,如果你是使用的Spring Cloud Alibaba,此处可以跳过。
测试代码
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("test")
public class FlowController {
@PostConstruct
public void initRole(){
List<FlowRule> rules = new ArrayList<>(1);
FlowRule rule = new FlowRule();
// 设置规则匹配的资源名称
rule.setResource("myFlowResource");
// 规则作用类型(此为QPS类型,还有一种为FLOW_GRADE_THREAD,线程数类型)
// 默认为QPS类型
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// 按Grade设置,此处为限制 QPS 为 2.
rule.setCount(2);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
@RequestMapping("flow1")
@SentinelResource(value = "myFlowResource",blockHandler = "block1")
public Map<String,Object> flow1(){
Map<String,Object> rs = new HashMap<>();
rs.put("success",true);
rs.put("msg","flow1通过流控正常返回");
return rs;
}
public Map<String,Object> block1(BlockException ex){
Map<String,Object> rs = new HashMap<>();
rs.put("success",false);
rs.put("msg","flow1被流控");
return rs;
}
@RequestMapping("flow2")
@SentinelResource(value = "myFlowResource",blockHandler = "block2")
public Map<String,Object> flow2(){
Map<String,Object> rs = new HashMap<>();
rs.put("success",true);
rs.put("msg","flow2通过流控正常返回");
return rs;
}
public Map<String,Object> block2(BlockException ex){
Map<String,Object> rs = new HashMap<>();
rs.put("success",false);
rs.put("msg","flow2被流控");
return rs;
}
}
此处为多维度验证的代码,我们在此一并解释
- @PostConstruct Spring的注解,标识Bean的init方法。在Bean加载后执行。此处我们是为了在请求之前执行流控规则的代码。(此处也可以通过实现 InitFunc 接口,以SPI扩展的方式初始化规则配置,这个我们后面再说)
- initRole方法,不用多说,定义了资源myFlowResource对应的流控规则,规则为QPS不超过2(设定为2是为了方便自我测试与验证)。
- flow1和flow2两个接口,我们都定义为了同一个资源名称。还记得我们上一篇文章介绍的吗?
machine-root
/ \
/ \
EntranceNode1 EntranceNode2
/ \
/ \
DefaultNode(nodeA) DefaultNode(nodeA)
————————————————
https://blog.csdn.net/forlinkext/article/details/128917305
调用同一个资源nodeA,根据上下文的不同,是不同的调用树。虽然可以对多段代码配置同样的资源名,但我们不建议这样做。
@SentinelResource注解详解
@SentinelResource 用于定义资源,并提供可选的异常处理和 fallback 配置项。 @SentinelResource 注解包含以下属性:
- value:资源名称,必需项(不能为空)
- entryType:entry 类型,可选项(默认为 EntryType.OUT)
- blockHandler / blockHandlerClass: blockHandler 对应处理 BlockException 的函数名称,可选项。
- blockHandler 函数访问范围需要是 public
- 返回类型需要与原方法相匹配
- 参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。
- blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- fallback:fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。fallback 函数签名和位置要求:
- 返回值类型必须与原函数返回值类型一致;
- 方法参数列表需要和原函数一致,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
- fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- defaultFallback(since 1.6.0):默认的 fallback 函数名称,可选项,通常用于通用的 fallback 逻辑(即可以用于很多服务或方法)。默认 fallback 函数可以针对所以类型的异常(除了 exceptionsToIgnore 里面排除掉的异常类型)进行处理。若同时配置了 fallback 和 defaultFallback,则只有 fallback 会生效。defaultFallback 函数签名要求:
- 返回值类型必须与原函数返回值类型一致;
- 方法参数列表需要为空,或者可以额外多一个 Throwable 类型的参数用于接收对应的异常。
- defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 fallbackClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析。
- exceptionsToIgnore(since 1.6.0):用于指定哪些异常被排除掉,不会计入异常统计中,也不会进入 fallback 逻辑中,而是会原样抛出。
注:若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。若未配置 blockHandler、fallback 和 defaultFallback,则被限流降级时会将 BlockException 直接抛出。
一般推荐将 @SentinelResource 注解加到服务实现上,而在 Web 层直接使用 Spring Cloud Alibaba 自带的 Web 埋点适配
后续文章我们将以此文为基础添加各种规则,还有匹配Sentinel控制台等等。