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

相关推荐
2401_857622664 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
2402_857589364 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
哎呦没6 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
_.Switch6 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
杨哥带你写代码7 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
AskHarries8 小时前
读《show your work》的一点感悟
后端
A尘埃8 小时前
SpringBoot的数据访问
java·spring boot·后端
yang-23078 小时前
端口冲突的解决方案以及SpringBoot自动检测可用端口demo
java·spring boot·后端
Marst Code8 小时前
(Django)初步使用
后端·python·django
代码之光_19808 小时前
SpringBoot校园资料分享平台:设计与实现
java·spring boot·后端