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 是面向分布式服务架构 ...
随机推荐
- docker基础_docker引擎内部原理
docker引擎内部原理 docker主要由以下主要组件构成:docker客户端.docker守护进程(daemon).containerd.runc.shim daemon daemon的主要功能包 ...
- 【虚拟机】VMware-Ubuntu-安装与卸载
VMware-Ubuntu 一.Ubuntu虚拟机安装 1.安装Ubuntu镜像 下载Ubuntu镜像文件,下载地址:http://www.ubuntu.com,点击download 下一步,到如下页 ...
- Exception in thread "main" java.awt.AWTError: Assistive Technology not found: org.GNOME.Accessibilit
系统环境 Ubuntu 20.04 focal 问题分析 该异常出现的原因,从谷歌上可以得到答案 one of the more common causes of this exception is ...
- python连接redis、redis字符串操作、hash操作、列表操作、其他通用操作、管道、django中使用redis
今日内容概要 python连接redis redis字符串操作 redis之hash操作 redis之列表操作 redis其他 通用操作,管道 django中使用redis 内容详细 1.python ...
- JavaWeb之如何把请求数据转成实体类
JavaWeb之如何把请求数据转成实体类 自己写个工具类加入下面两个静态方法 自定一个注解类DateTimeFormatting 调用方式User user = util.ObjectFromMap( ...
- 2022 Java生态系统报告:Java 11超Java 8、Oracle在缩水、Amazon在崛起!
近日,New Relic发布了最新的2022 Java生态系统报告,这份报告可以帮助我们深入的了解Java体系的最新使用情况,下面就一起来看看2022年,Java发展的怎么样了,还是Java 8 YY ...
- Linux虚拟网络技术学习
一个执着于技术的公众号 地方 背景 在Linux虚拟化技术中,网络层面,通常重要的三个技术分别是Network Namespace.veth pair.以及网桥或虚拟交换机技术.今天就通过实验带大家一 ...
- REST APIs
REST APIs 旨在通过HTTP 的动作语义METHOD, 以替代各种传统CRUD 操作所带来的命名问题,例如 "/userAdd"."/userDelete&quo ...
- Spring Boot+微信小程序_保存微信登录者的个人信息
1. 前言 微信小程序开发平台,提供有一类 API,可以让开发者获取到微信登录用户的个人数据.这类 API 统称为开放接口. Tip:微信小程序开发平台,会把微信登录用户的个人信息分为明文数据和敏感数 ...
- 590. N-ary Tree Postorder Traversal - LeetCode
Question 590. N-ary Tree Postorder Traversal Solution 题目大意:后序遍历一个树 思路: 1)递归 2)迭代 Java实现(递归): public ...