Sentinel如何实现对分布式系统的高可用性和流量控制,看完这篇源码就懂了

Sentinel核心 API

Sentinel 的核心 API 处理流程通过 ProcessorSlotChain 实现。

ProcessorSlotChain 主要是一个处理器链,用于管理和执行所有的流量控制、熔断降级等处理规则,并最终返回结果或者抛出异常。下面我们通过源码的方式详细介绍一下 Sentinel 的 ProcessorSlotChain 实现。

ProcessorSlotChain处理器链

ProcessorSlotChain 是 Sentinel 核心的处理器链,它包含了多个 ProcessorSlot 节点,并负责管理这些节点的运行顺序。同时,ProcessorSlotChain 同时提供了 apply() 方法,用于接收和处理请求。

ProcessorSlotChain 的定义如下:

java 复制代码
public class ProcessorSlotChain {
    
    private final List<ProcessorSlot> slots = new ArrayList<>();
    
    public ProcessorSlotChain() {
        this(new ArrayList<>());
    }
    
    public ProcessorSlotChain(List<ProcessorSlot> slotList) {
        if (slotList != null) {
            slots.addAll(slotList);
        }
    }

    // 处理请求
    public Entry process(Context context, ResourceWrapper resourceWrapper, DefaultNode node,
                          int count, boolean prioritized, Object... args) throws Throwable {
        if (context == null) {
            return null;
        }
        return new ProcessorSlotEntry(slots, context, resourceWrapper, node, count, prioritized, args);
    }

    // 添加处理器节点
    public void addLast(ProcessorSlot processorSlot) {
        if (processorSlot != null && !slots.contains(processorSlot)) {
            slots.add(processorSlot);
        }
    }

    // 移除处理器节点
    public void remove(ProcessorSlot processorSlot) {
        slots.remove(processorSlot);
    }

    // 获取处理器节点列表
    public List<ProcessorSlot> getSlots() {
        return Collections.unmodifiableList(slots);
    }
    
}

在 ProcessorSlotChain 中,我们可以看到主要提供了以下几个方法:

  • process(): 处理请求的方法,接收一个 Context 对象和其他相关参数,并返回一个 Entry 对象。
  • addLast(): 向 ProcessorSlotChain 中添加一个 ProcessorSlot 节点。
  • remove(): 从 ProcessorSlotChain 中移除一个 ProcessorSlot 节点。
  • getSlots(): 获取 ProcessorSlotChain 中所有的 ProcessorSlot 节点。

ProcessorSlot接口

ProcessorSlot 接口是 Sentinel 处理器链中的节点接口,每个节点都应该实现这个接口。ProcessorSlot 主要通过其 entry() 方法来执行流量控制、熔断降级等操作,并将处理结果传递给下一个节点。

ProcessorSlot 的定义如下:

public interface ProcessorSlot { void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args) throws Throwable; } 在 ProcessorSlot 中,我们可以看到主要提供了一个 entry() 方法,这个方法用于进行流量控制等操作,并将处理结果传递给下一个 ProcessorSlot 节点。

ProcessorSlotEntry类

ProcessorSlotEntry 类是 ProcessorSlotChain 的核心实现类 ,它维护了 ProcessorSlotChain 中所有的 ProcessorSlot 节点,并负责执行整个处理流程。在 ProcessorSlotEntry 中,我们可以看到它首先会根据请求参数创建一个 Context 对象,然后依次执行 ProcessorSlot 节点的 entry() 方法

ProcessorSlotEntry 的定义如下:

java 复制代码
public class ProcessorSlotEntry extends Entry {

    private final List<ProcessorSlot> sortedSlots;
    private ProcessorSlot lastExecutedSlot = null;
    private int curIdx = 0;
    
    public ProcessorSlotEntry(List<ProcessorSlot> sortedSlots, Context context,
                              ResourceWrapper resourceWrapper, DefaultNode node,
                              int count, boolean prioritized, Object[] originArgs) {
        super(context, resourceWrapper, node, count, prioritized, originArgs);
        this.sortedSlots = sortedSlots;
    }

    @Override
    public void exit(int count, Object... args) throws Throwable {
        // 执行下一个节点
        if (lastExecutedSlot != null && curIdx < sortedSlots.size()) {
            lastExecutedSlot.exit(context, resourceWrapper, node, count, args);
            lastExecutedSlot = null;
        }
    }

    @Override
    public void fireEntry() throws Throwable {
        lastExecutedSlot = null;
        if (curIdx < sortedSlots.size()) {
            ProcessorSlot slot = sortedSlots.get(curIdx++);
            lastExecutedSlot = slot;
            slot.entry(context, resourceWrapper, node, count, prioritized, originArgs);
        }
    }
}

在 ProcessorSlotEntry 中,我们可以看到主要提供了以下几个方法:

  • exit(): 退出当前 ProcessorSlot 节点,并执行下一个 ProcessorSlot 节点。
  • fireEntry(): 进入当前 ProcessorSlot 节点,并执行 ProcessorSlot 中的 entry() 方法。

Sentinel API 处理流程

通过 ProcessorSlotChain、ProcessorSlot 和 ProcessorSlotEntry 这几个核心类的协作,完成了 Sentinel API 处理流程。

具体而言,当客户端向 Sentinel 发送请求后,API 接口会首先进入 ProcessorSlotChain 的第一个 ProcessorSlot 点,即 EntranceNode。

EntranceNode 主要负责对请求进行统计和流量控制,并根据请求的情况调用下一个 ProcessorSlot 节点。

在 ProcessorSlotChain 中,后续的 ProcessorSlot 节点依次进行流控、熔断降级等操作,并将处理结果传递给下一个 ProcessorSlot 节点进行处理。

最后,ProcessorSlotChain 的最后一个节点,即 ExitNode,会将请求结果返回给客户端,并统计监控数据。通过这个处理流程,Sentinel 实现了对分布式系统的高可用性和流量控制。

相关推荐
程序员岳焱6 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
麦兜*6 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
大只鹅7 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头7 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
IT_10247 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
bobz9658 小时前
动态规划
后端
stark张宇8 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
亚力山大抵9 小时前
实验六-使用PyMySQL数据存储的Flask登录系统-实验七-集成Flask-SocketIO的实时通信系统
后端·python·flask
超级小忍9 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端
CHENWENFEIc10 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试