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

相关推荐
计算机毕设指导631 分钟前
基于Springboot学生宿舍水电信息管理系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven
计算机-秋大田39 分钟前
基于Spring Boot的兴顺物流管理系统设计与实现(LW+源码+讲解)
java·vue.js·spring boot·后端·spring·课程设计
羊小猪~~2 小时前
MYSQL学习笔记(九):MYSQL表的“增删改查”
数据库·笔记·后端·sql·学习·mysql·考研
豌豆花下猫2 小时前
Python 潮流周刊#90:uv 一周岁了,优缺点分析(摘要)
后端·python·ai
橘猫云计算机设计3 小时前
基于SSM的《计算机网络》题库管理系统(源码+lw+部署文档+讲解),源码可白嫖!
java·数据库·spring boot·后端·python·计算机网络·毕设
熬夜苦读学习3 小时前
Linux文件系统
linux·运维·服务器·开发语言·后端
坚定信念,勇往无前4 小时前
Spring Boot 如何保证接口安全
spring boot·后端·安全
程序员侠客行5 小时前
Spring事务原理详解 三
java·后端·spring·架构
Hello.Reader6 小时前
深入理解 Rust 的 `Rc<T>`:实现多所有权的智能指针
开发语言·后端·rust
yoona10206 小时前
Rust编程语言入门教程(八)所有权 Stack vs Heap
开发语言·后端·rust·区块链·学习方法