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 实现了对分布式系统的高可用性和流量控制。

相关推荐
喵个咪5 分钟前
开箱即用的GO后台管理系统 Kratos Admin - 后端权限控制
后端·go·api
用户67570498850210 分钟前
如何判断两张图片的相似度?原来图片对比也可以如此简单!
后端
轻松Ai享生活11 分钟前
超越可观察性:使用 eBPF 修改系统调用行为
后端
一眼万年0416 分钟前
Kafka ReplicaManager 深度解析:副本管理的核心引擎
后端
梁凌锐20 分钟前
重构手法——代码健壮性增强类 | 防御性编程 | 引入断言
后端
闲敲棋子落灯华37 分钟前
java学习笔记(三)--java包的引入、访问控制、类的继承、super关键字、重载、重写、运算符、拆箱
java·后端
程序员岳焱40 分钟前
Java 使用 Spring AI 的 10 个实用技巧
java·后端·程序员
Jooolin1 小时前
Flask 入门到实战(2):用 SQLAlchemy 优雅操作数据库
后端·flask·ai编程
Kapaseker1 小时前
Android程序员初学Rust-通道
后端·rust
BingoGo1 小时前
PHP 8.5 将带来什么 🚀
后端