Sentinel-流量防卫兵
1.背景
1.1 简介
Sentinel 具有以下特征
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。
- 完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
Sentinel 的主要特性
- 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
- 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。
1.2 学习参考
- sentinel官方文档:《官方文档地址》
- sentinel源码解析:《sentinel是如何限流的》
- sentinel常规知识点:《sentinel连环17问》
1.3 章节介绍
- 基于Spring boot 对接Sentinel;
- Nacos配置Sentinel规则信息;
- 测试流控规则,系统保护规则,熔断规则;
- Sentinel控制台数据展示问题;
- Nacos规则存储与Sentinel修改数据同步问题;
- Sentinel责任链模式分析
2.项目构建
2.1 pom配置
<!-- nacos配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!-- sentinel配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<!-- sentinel 规则基于nacos存储-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.3</version>
</dependency>
2.2 项目参数配置
server:
servlet:
context-path: /sentinel-nacos-demo
spring:
application:
name: sentinel-nacos-demo
profiles:
active: local
cloud:
nacos:
config:
server-addr: xxx.xxx.xx.x:8848
#server-addr: xxx.xxx.xx.x:8848
group: ${spring.application.name}
file-extension: yaml
# 配置中心使用单独namespace
namespace: "study"
discovery:
server-addr: xxx.xxx.xx.x:8848
namespace: "study"
group: "sentinel-nocas-demo"
sentinel:
transport:
dashboard: xxx.xxx.xx.x:8842 #启动本项目后需要请求一次才能向sentinel控制台注册
port: 8719 #当一个服务器部署多个应用时要配置不同port,单个应用可忽略
client-ip: 10.32.4.230 #指定本机ip地址,避免多个虚拟地址,导致数据获取失败
datasource:
## 配置流程控制
## rule-type 配置表示该数据源中的规则属于哪种类型的规则(flow流控,degrade熔断降级,authority授权,system系统保护, param-flow热点参数限流, gw-flow, gw-api-group)
flow:
nacos:
server-addr: xxx.xxx.xx.x:8848
namespace: "study"
data-id: ${spring.application.name}-sentinel-flow-rules
group-id: sentinel-group
data-type: json
rule-type: flow
## 配置降级规则
degrade:
nacos:
server-addr: xxx.xxx.xx.x:8848
namespace: "study"
dataId: ${spring.application.name}-sentinel-degrade-rules
groupId: sentinel-group
data-type: json
rule-type: degrade
system:
nacos:
server-addr: xxx.xxx.xx.x:8848
namespace: "study"
dataId: ${spring.application.name}-sentinel-system-rules
groupId: sentinel-group
data-type: json
rule-type: system
2.3 规则配置
Sentinel 流控规则配置
[
{
"resource": "/sentinel/rule/flow",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
Sentinel 熔断规则配置
[
{
"resource": "/sentinel/rule/degrade",
"count": 1,
"grade": 0,
"timeWindow": 10,
"minRequestAmount": 1,
"statIntervalMs": 1000,
"slowRatioThreshold": 0.1
}
]
Sentinel 系统保护规则配置
[
{
"avgRt":1,
"highestCpuUsage":-1,
"highestSystemLoad":-1,
"maxThread":-1,
"qps":1000
}
]
2.4 规则统一拦截
@Component
public static class MyBlockExceptionHandler implements BlockExceptionHandler {
@Override
public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, BlockException e) throws Exception {
//Sentinel规则的详细信息
BaseResponse r = BaseResponse.error("sentinel-控制拦截");
if (e instanceof FlowException) {
r = BaseResponse.error("接口限流了",e.toString());
} else if (e instanceof DegradeException) {
r = BaseResponse.error( "服务降级了",e.toString());
} else if (e instanceof ParamFlowException) {
r = BaseResponse.error("热点参数限流了",e.toString());
} else if (e instanceof SystemBlockException) {
r = BaseResponse.error( "触发系统保护规则了",e.toString());
} else if (e instanceof AuthorityException) {
r = BaseResponse.error( "授权规则不通过",e.toString());
}
//返回json数据
response.setStatus(500);
response.setCharacterEncoding("utf-8");
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
new ObjectMapper().writeValue(response.getWriter(), r);
}
}
3.项目运行示例
3.1 规则拦截测试
Sentinel 流控规则测试
Sentinel 熔断规则测试
Sentinel 系统保护规则测试
3.2 Sentinel 控制台界面展示
实时监控
簇点链路
流控规则
4.控制台数据展示问题
4.1 簇点链路数据为空
sentinel:
transport:
dashboard: xxx.168.16.13:8842 #启动本项目后需要请求一次才能向sentinel控制台注册
port: 8719 #当一个服务器部署多个应用时要配置不同port,单个应用可忽略
client-ip: xx.xx.4.230 #指定本机ip地址,避免多个虚拟地址,导致数据获取失败
4.2 实时监控数据为空
5.Sentinel控制台与Nacos配置中心数据一致性问题
- 项目组统一规定,规则信息只能基于Nacos配置,在Nacos中做修改调整,不可在Sentinel控制台操作规则信息。
- 参考目前的一些同步方案,修改Sentinel源代码,支持在控制台变更规则信息后,同步到Nacos中。具体可参考《SpringBoot Sentinel Nacos (-规则双向同步-自定义响应 》
6.Sentinel部分核心源码分析
ProcessorSlot
作为 SPI 接口进行扩,使得 Slot Chain 具备了扩展的能力。开发人员可以自行加入自定义的 slot 并编排 slot 间的执行顺序,从而可以给 Sentinel 添加自定义的功能。6.1 默认slot执行顺序
1.定义实现类加载顺序
@Spi(isSingleton = false, order = Constants.ORDER_NODE_SELECTOR_SLOT) 2.定义NodeSelectorSlot实现类继承自抽象类
public class NodeSelectorSlot extends AbstractLinkedProcessorSlot<Object> 3.定义抽象类实现接口
public abstract class AbstractLinkedProcessorSlot<T> implements ProcessorSlot<T> 4.定义链路顶层接口
public interface ProcessorSlot<T>
6.2 构建默认责任链
ProcessorSlot
接口,是不能加入到责任链路中的。参考源码:public class DefaultSlotChainBuilder implements SlotChainBuilder { @Override
public ProcessorSlotChain build() {
ProcessorSlotChain chain = new DefaultProcessorSlotChain(); List<ProcessorSlot> sortedSlotList = SpiLoader.of(ProcessorSlot.class).loadInstanceListSorted();
for (ProcessorSlot slot : sortedSlotList) {
if (!(slot instanceof AbstractLinkedProcessorSlot)) {
RecordLog.warn("The ProcessorSlot(" + slot.getClass().getCanonicalName() + ") is not an instance of AbstractLinkedProcessorSlot, can't be added into ProcessorSlotChain");
continue;
} chain.addLast((AbstractLinkedProcessorSlot<?>) slot);
} return chain;
}
}
public abstract class ProcessorSlotChain extends AbstractLinkedProcessorSlot<Object> { /**
* Add a processor to the head of this slot chain.
*
* @param protocolProcessor processor to be added.
*/
public abstract void addFirst(AbstractLinkedProcessorSlot<?> protocolProcessor); /**
* Add a processor to the tail of this slot chain.
*
* @param protocolProcessor processor to be added.
*/
public abstract void addLast(AbstractLinkedProcessorSlot<?> protocolProcessor);
}
6.3 流程总结
- 采用责任链模式完成Sentinel的信息统计、熔断、限流等操作;
- 责任链中NodeSelectSlot负责选择当前资源对应的Node,同时构建node调用树;
- 责任链中ClusterBuilderSlot负责构建当前Node对应的ClusterNode,用于聚合同一资源对应不同Context的Node;
- 责任链中的StatisticSlot用于统计当前资源的调用情况,更新Node与其对用的ClusterNode的各种统计数据;
- 责任链中的FlowSlot根据当前Node对应的ClusterNode(默认)的统计信息进行限流;
- 资源调用统计数据(例如PassQps)使用滑动时间窗口进行统计;
- 所有工作执行完毕后,执行退出流程,补充一些统计数据,清理Context。
6.4 编写一个自定义拦截Slot
/**
* 编写一个自定义限流链路
*
* @author wangling
* @date 2022/07/05
*/
@Spi(order = -3000)
public class TestMySlot extends AbstractLinkedProcessorSlot<DefaultNode> { @Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode obj, int count, boolean prioritized, Object... args)
throws Throwable {
try {
fireEntry(context, resourceWrapper, obj, count, prioritized, args);
throw new BusinessException("TestMySlot-测试");
} catch (Exception e) {
throw e;
} catch (Throwable e) {
RecordLog.warn("Unexpected entry exception", e);
} } @Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
try {
fireExit(context, resourceWrapper, count, args);
} catch (Throwable e) {
RecordLog.warn("Unexpected entry exit exception", e);
}
}
}
Sentinel-流量防卫兵的更多相关文章
- alibaba/Sentinel 分布式 系统流量防卫兵
Sentinel: 分布式系统的流量防卫兵 Sentinel 是什么? 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 以流量为切入点,从流量控制.熔断降级.系统负载保护等多 ...
- Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探
目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战 Springboot: 2.1.8.RELEASE SpringCloud: Greenwich.SR2 ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战 在阅读本文前,建议先阅读<Spring Cloud Alibaba | Sentinel:分布式系 ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则 前面几篇文章较为详细的介绍了Sentinel的使用姿势,还没看过的小伙伴可以访问以下链接查看: &l ...
- Sentinel: 分布式系统的流量防卫兵
前言 在 Spring Cloud 体系中,熔断降级我们会使用 Hystrix 框架,限流通常会在 Zuul 中进行处理,Zuul 中没有自带限流的功能,我们可以自己做限流或者集成第三方开源的限流框架 ...
- Sentinel分布式系统的流量防卫兵
Sentinel 是什么?官网:https://github.com/alibaba/Sentinel/wiki/介绍 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 以流量 ...
- 🏆【Alibaba中间件技术系列】「Sentinel技术专题」分布式系统的流量防卫兵的基本介绍(入门源码介绍)
推荐资料 官方文档 官方demo Sentinel 是什么? 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度保护 ...
- SpringCloud Alibaba (三):Sentinel 流量控制组件
SpringCloud Alibaba (三):Sentinel 流量控制组件 Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 是面向分布式服务架构 ...
随机推荐
- EmlParse:一款超轻量级的批量解析EML格式电子邮件的工具
工具特点 1.绿色纯天然,无任何依赖库,文件大小不到150K: 2.可批量解析EML格式的电子邮件: 3.可提取EML文件中的正文和附件到指定目录: 4.可生成HTML格式的邮件列表清单,方便用户进行 ...
- PostgreSQL培训认证讲师招募
中国PostgreSQL考试认证中心 以"知"之名,携手共进!不蒂于知识共享,技术传承,更愿为讲师耕织嫁衣,助您成为PostgreSQL圈的意见领袖,用知识改变世界! 一.入选讲师 ...
- React ant table 用 XLSX 导出excel文件
近期做了一个react ant design 的table转换成excel 的功能 总结下 首先我们会自己定义下 antdesign 的table的columns其中有可能有多语言或者是render方 ...
- 在windows上安装elasticsearch7.6
在windows上安装elasticsearch7.6 安装Java1.8 下载Java1.8 提取码:yi0c 链接:https://pan.baidu.com/s/1mNd2Yg-k6ob91bO ...
- 如何使用Shell写一个显示目录结构的命令?
公众号关注 「开源Linux」 回复「学习」,有我为您特别筛选的学习资料~ 在Linux中使用Shell写一个显示目录结构的命令,快速寻找目录结构. 1.代码 #!/usr/bin/env bash ...
- springcloud + nacos实现共用基础服务(灰度版本)
背景: 当我们使用微服务时,若想在本地联调就需要启动多个服务,为了避免本地启动过多服务,现将注册中心等基础服务共用.当我们在服务A开发时,都是注册到同一个nacos,这样本地和开发环境的服务A就会同时 ...
- Centos7上安装python3.7
Centos7安装python3.7 由于python2和python3在很大程度上有些不同,因为需要,就以决定安装python3.x的版本,但由于Centos上自安装的就是pyhton2.6.所以在 ...
- 【多线程】线程同步 synchronized
由于同一进程的多个线程共享同一块存储空间 , 在带来方便的同时,也带来了访问 冲突问题 , 为了保证数据在方法中被访问时的正确性 , 在访问时加入 锁机制synchronized , 当一个线程获得对 ...
- MySQL主从配置及haproxy和keepalived搭建
本篇文章主要介绍如何搭建MySQL主主配置.主从配置.haproxy.keepalived,已经搭建过程中的一些问题.本次运行环境是在docker中,也会介绍一些docker的知识 docker 小知 ...
- linux篇-linux LAMP yum版安装
LAMP(linux.apache.mysql.php),是四个套件的合成,简单讲就是要把php运行在linux上,需要依赖apache和mysql数据库. 1 准备好一个linux系统(centos ...