Sentinel: 分布式系统的流量防卫兵
git地址: https://github.com/alibaba/sentinel
中文文档: https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
官网: https://sentinelguard.io/zh-cn/
Sentinel
:面向云原生微服务的高可用流控防护组件。
Sentinel 是什么?
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
- 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
特性
下载安装运行Dashboard
下载地址: sentinel-dashboard-1.7.1.jar
启动命令: java -jar xxx.jar
访问: http://vm1:8080
用户名密码: sentinel/sentinel/
服务集成sentinel- 本地Demo
maven依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>x.y.2z</version>
</dependency>
测试用例
public class SimpleSentinelMainTest {
public static void main(String[] args) throws InterruptedException {
// 配置规则.
initFlowRules();
while (true) {
// 1.5.0 版本开始可以直接利用 try-with-resources 特性
try (Entry entry = SphU.entry("HelloWorld")) {
// 被保护的逻辑
System.out.println("hello world");
// TimeUnit.MILLISECONDS.sleep(40L);
} catch (BlockException ex) {
// 处理被流控的逻辑
System.out.println("blocked!");
}
}
}
private static void initFlowRules(){
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Set limit QPS to 20.
rule.setCount(20);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
springcloud集成
pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- sentinel 使用nacos持久化 ———— 后续使用 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yaml
server:
port: 8401
spring:
application:
name: sentinel-service
cloud:
nacos:
discovery:
server-addr: vm1:8848
config:
server-addr: vm1:8848
## 指定配置文件的格式
file-extension: yaml
sentinel:
transport:
dashboard: vm1:8080
## 从8719依次+1扫描,直到找到未被占用端口为之
port: 8719
## sentinel 无法通过下面的preferred-networks来指定具体的ip,这里手动指定。
clientIp: 192.168.1.102
### 多网卡,指定网卡
inetutils:
preferred-networks: 192.168.1.
management:
endpoint:
web:
exposure:
include: "*"
主类添加@EnableDiscoveryClient
应用添加@SentinelResource
@RestController
public class FlowLimitController {
/**
* 默认会将 @RequestMapping 的url作为资源注册
*/
@RequestMapping("/testA")
public String testA() {
return "---- testA";
}
@RequestMapping("/testB")
@SentinelResource(value = "testB")
public String testB() {
return "---- testB";
}
}
默认会将 @RequestMapping 的url作为资源注册
Sentinel 支持通过@SentinelResource
注解定义资源并配置blockHandler
和fallback
函数来进行限流之后的处理。
请求服务接口/testA
必须在请求完一个接口之后,才能在sentinel dashboard
上显示服务。
配置流控规则
设置/testA
接口,QPS阈值为3
;
高频次访问/testA
不停的访问 /testA
接口,当QPS超过3时,会出现下列错误信息。