sentinel入门
- 一、什么是Sentinel
- 1.1、流量控制
- 1.2、熔断降级
- 1.3、热点参数限流
- 1.4、系统负载保护
- 二、资源和规则的概念
- 三、Sentinel工作主流程
- 四、代码初体验
- 五、集成控制台
一、什么是Sentinel
Sentinel 是阿里巴巴开源的流量控制组件,主要用来保护微服务和分布式系统,防止因为流量过大或服务故障导致系统崩溃。可以简单理解为它是一个“保护盾”,用来保障系统的稳定性和高可用性。下面是几个通俗易懂的例子来说明 Sentinel 的作用:
1.1、流量控制
想象你是一家餐馆的老板,餐馆座位有限,突然来了很多客人,如果不加控制,餐馆很容易超负荷运转,服务质量下降。Sentinel 就像一个门卫,当客人太多时,会限制进入的人数,保证每个进来的人都能享受到优质的服务。
例子:你设置餐馆的座位数为100,当店内人数达到100时,Sentinel 会暂时阻止新的客人进入,直到有空位出现。
1.2、熔断降级
如果餐馆的厨师突然生病,菜品的质量和出餐速度都会受到影响。Sentinel 就像一个智能系统,当发现菜品质量下降时,会主动通知客人暂停点菜,等厨师恢复状态再继续接单。
例子:当你的系统某个服务响应时间变得很慢或不可用时,Sentinel 会暂时停止对该服务的调用,防止因为这个问题影响到整个系统。
1.3、热点参数限流
有时候某些菜品特别受欢迎,比如限量的招牌菜,可能会被一抢而空。Sentinel 可以对这些热门菜品进行限流,确保更多的客人都有机会尝到。
例子:你设置每个客人只能点一份招牌菜,当有客人重复点这道菜时,Sentinel 会进行限制。
1.4、系统负载保护
假如你的餐馆有一定的厨房容量,一次性接收太多订单可能会导致厨房忙不过来。Sentinel 会监控整个系统的负载情况,保证不会超出承受范围。
例子:当系统的 CPU 使用率超过设定阈值时,Sentinel 会自动降低请求的通过率,确保系统在高负载下仍能稳定运行。
二、资源和规则的概念
首先要了解两个概念“资源”和“规则”。资源是Sentinel中的一个关键概念,它可以是任何东西,比如服务、方法甚至是代码片段。一旦它被Sentinel API包装,它就被定义为资源,可以申请由Sentinel提供的保护。而规则是Sentinel保护资源的方式,如流量控制、并发限制和断路规则。规则可以动态修改,实时生效。
三、Sentinel工作主流程
整个限流熔断的过程是基于Slot chain
实现的,而Slot chain
内部是使用链表
来实现。在 Sentinel 里面,所有的资源都对应一个资源名称(resourceName
),每次资源被调用都会创建一个Entry
对象。Entry
可以通过对主流框架的适配自动创建,也可以通过注解的方式或调用SphU API
显式创建。Entry
创建的时候,同时创建一系列功能插槽(slot chain)
,这些插槽有不同的职责,例如:
-
NodeSelectorSlot 负责收集资源的路径,并将这些资源的调用路径,以树状结构存储起来,用于根据调用路径来限流降级;
-
ClusterBuilderSlot 用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据;
-
StatisticSlot 则用于记录、统计不同维度的 runtime 指标监控信息;
-
FlowSlot 则用于根据预设的限流规则以及前面 slot 统计的状态,来进行流量控制;
-
AuthoritySlot 则根据配置的黑白名单和调用来源信息,来做黑白名单控制;
-
DegradeSlot 通过统计信息以及预设的规则,来做熔断降级;
-
SystemSlot 通过系统的状态,例如 load1 等,来控制总的入口流量;
四、代码初体验
添加POM依赖
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.6.3</version>
</dependency>
使用代码控制访问QPS<=2
,当1秒内访问次数<=2
时,网页返回Hi,sentinel......!!!
;当1秒内访问次数>=2
时,网页返回busy...........
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
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.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;/*** @author hanson.huang* @version V1.0* @ClassName Sentinel* @Description TODO* @date 2024/7/26 15:30**/
@RestController
public class Sentinel {/*** 处理 /sentinel 的 GET 请求。* 该方法尝试进入一个 Sentinel 保护的资源。* 如果由于流量控制导致资源被阻止,则捕获 BlockException 并返回 "busy" 消息。* 否则,返回成功消息。** @return 表示请求是否成功或被阻止的字符串。*/@GetMapping("sentinel")public String sentinel() {// 声明一个Entry对象,用于后续的资源保护Entry entry = null;try {// 尝试进入资源保护,如果流量超过限制,会抛出BlockExceptionSphU.entry("sentinel");// 如果没有抛出异常,则返回成功消息return "Hi, sentinel......!!!";} catch (BlockException e) {// 如果流量超过限制,捕获BlockException并打印堆栈信息,返回繁忙消息e.printStackTrace();return "busy......!!!";} finally {// 确保在最终块中退出Entry,释放资源if (entry != null) {entry.exit();// 这个return语句会覆盖上面的return,因此不会被执行到return "this is final";}}}/*** 初始化流量控制规则。* 在 Spring 容器初始化时执行,用于设置 Sentinel 的流量控制规则。* 这里将资源 "sentinel" 的 QPS 限制设置为 2。*/@PostConstructprivate static void initFlowRules() {// 创建一个FlowRule列表,用于存储流量控制规则List<FlowRule> rules = new ArrayList<>();// 创建一个新的FlowRule对象,设置流量控制的相关参数FlowRule rule = new FlowRule();// 设置受保护的资源名称为"sentinel"rule.setResource("sentinel");// 设置流量控制的方式为QPS(每秒查询率)rule.setGrade(RuleConstant.FLOW_GRADE_QPS);// 设置QPS的阈值为2,即每秒最多允许2次访问rule.setCount(2);// 将这个流量控制规则添加到规则列表中rules.add(rule);// 加载这些规则到FlowRuleManager中,使其生效FlowRuleManager.loadRules(rules);}
}
来回刷新请求这个接口会爆busy…!!!
五、集成控制台
https://github.com/alibaba/Sentinel/releases
控制台是一个jar包,在该目录下打开cmd窗口,输入如下命令进行启动(要改成你下载的包名,我这里是sentinel-dashboard-1.8.8.jar):
java -Dserver.port=8083 -Dcsp.sentinel.dashboard.server=localhost:8083 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.8.jar
访问:http://127.0.0.1:8083
登录Sentinel控制台后,服务端不会第一时间注册到Sentinel控制台,需要主动触发下资源接口。( 初始账号密码均为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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.csdn.dev</groupId><artifactId>csdn-dev-boot</artifactId><version>${revision}</version></parent><packaging>jar</packaging><modelVersion>4.0.0</modelVersion><artifactId>sentinel-demo</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version><sentinel.transport.version>1.6.3</sentinel.transport.version><sentinel.annotation.aspectj>1.6.3</sentinel.annotation.aspectj><spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version></properties><dependencies><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.6.3</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--Sentinel集成控制台--><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>${sentinel.transport.version}</version></dependency><!--Sentinel注解依赖--><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-annotation-aspectj</artifactId><version>${sentinel.annotation.aspectj}</version></dependency><!--Sentinel集成SpringCloud--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel</artifactId><version>${spring-cloud-alibaba.version}</version></dependency></dependencies>
</project>
server:port: 10000
spring:application:name: csdn-sentinel-democloud:nacos:discovery:server-addr: 127.0.0.1:8848sentinel:transport:dashboard: localhost:8083
启动项目成功后,在浏览器中访问接口地址,正常情况下网页会出现Hello sentinel
,当访问量>2
时,会返回this is helloblockHandler.....
代码:GItHub