Sentinel-流量防卫兵

1.背景

1.1 简介

Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

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 学习参考

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: xx.xx.x.xxx   #指定本机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 规则配置

在Nacos配置中心中设置如下配置:

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 簇点链路数据为空

造成原因:部署微服务的服务器存在多个虚拟的ip地址,Sentinel控制台识别了其中一个的ip地址,但是该地址与控制台网络不通。
解决办法(如下两个方法都可以):
1.部署服务器设置一个固定的ip地址;
2.配置固定的客户端ip地址,如:
sentinel:
  transport:
    dashboard: xxx.168.16.13:8842  #启动本项目后需要请求一次才能向sentinel控制台注册
port: 8719  #当一个服务器部署多个应用时要配置不同port,单个应用可忽略
client-ip: xx.xx.4.230   #指定本机ip地址,避免多个虚拟地址,导致数据获取失败

4.2 实时监控数据为空

造成原因:部署维服务的服务器时间与Sentinel控制台所在服务器的时间不一致。
解决办法:调整两边服务器的时间,在差距为20秒以内, 数据可展示。

5.Sentinel控制台与Nacos配置中心数据一致性问题

Sentinel控制台可以通过簇点链路设置各种规则,但是规则信息不能落地存储。一旦Sentienl服务重启后,规则就会丢失。
解决方案可以将规则信息存储在Nacos中,这样就可以存储规则信息了。目前版本是在Nacos中设置规则信息后,可以在Sentinel控制台中查看,但在Sentinel控制台修改规则后,不能同步到Nacos中。
针对这样的情况,解决方案参考如下:
  • 项目组统一规定,规则信息只能基于Nacos配置,在Nacos中做修改调整,不可在Sentinel控制台操作规则信息。
个人建议:目前可以先基于Nacos配置统一管理,后续版本应该会支持双向同步,非必要情况下,不必造轮子处理。

6.Sentinel部分核心源码分析

本文分析源码版本:sentinel-core-1.8.1
Sentinel 将 ProcessorSlot 作为 SPI 接口进行扩,使得 Slot Chain 具备了扩展的能力。开发人员可以自行加入自定义的 slot 并编排 slot 间的执行顺序,从而可以给 Sentinel 添加自定义的功能。

6.1 默认slot执行顺序

slot实现类关系图
NodeSelectorSlot 的实现类调用关系:
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 构建默认责任链

加载实现了抽象类AbstractLinkedProcessorSlot的链路,若扩展时只实现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;
    }
}
历史调用链组装逻辑:调试代码时,发现新版本已经废弃这种调用组装逻辑了。(责任链模式,结合order顺序的模式,便于调整和控制)
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);
        }
    }
}
配置SPI自动扫描
 
posted @ 2022-07-05 15:52  无涯Ⅱ  阅读(1041)  评论(0编辑  收藏  举报