在微服务架构中,服务和服务之间的稳定性变得越来越重要。Sentinel以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保证服务的稳定性。
Sentinel提供了机器发现、健康情况管理、监控(单机和集群)、规则管理和推送等功能。它还提供了一个轻量级的开源控制台(Sentinel Dashboard)。
客户端需要引入Transport模块来与Sentinel控制台进行通信。可以通过pom.xml引入JAR包
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
Sentinel支持Zuul,但需要额外加上Sentinel对网关支持的依赖
<!--Sentinel的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--Sentinel对网关支持的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<!--Zull的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
Sentinel支持Spring Cloud Gateway,但需要额外加上Sentinel对网关支持的依赖
<!--Sentinel的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--Sentinel对网关支持的依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<!--Spring Cloud Gateway的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-gateway</artifactId>
</dependency>
在使用Endpoint(端点)之前,需要在Maven中添加Actuator依赖,并在配置文件中开启对Endpoint的访问支持。
在Sentinel Endpoint中暴露的信息有:规则信息、日志目录、当前实例的IP地址、Sentinel Dashboard地址、应用与Sentinel Dashboard的心跳频率等。
步骤:
1、下载Sentinel客户端
https://github.com/alibaba/Sentinel/releases
2、启动控制台
java -jar sentinel-dashboard.jar
3、进入控制台
访问http://localhost:8080访问控制台,账号和密码都是sentinel
进入控制台后,单击左侧导航栏中的"流控规则"按钮可以新建流控规则
流控规则的参数:
参数名 | 说明 |
---|---|
资源名 | 限流规则的作用对象,如:@SentinelResource中Value字段的值、相对URI、其他图片文件资源名 |
流控应用 | 流控针对的调用来源(服务级别)。若为default,则不区分调用来源 |
阀值类型 | QPS(每秒查询率)或线程数 |
单机阀值 | 限流阀值。超过这个值就执行流控规则 |
流控模式 | 直接、关联、链路 |
流控方式 | 直接拒绝、Warm Up、匀速排队 |
基于调用关系的流量控制有3种模式:
Sentinel还提供了降级规则来处理流控
重要概念:
系统保护规则是应用整体维度,而不是资源维度。它仅对入口流量生效。
阀值类型: