返回

Sentinel源码解读(四):剖析ProcessorSlot(中)

后端

AuthoritySlot:授权规则校验

Sentinel通过AuthoritySlot来实现对请求的授权校验,它可以控制哪些用户或角色能够访问受保护的资源。

public class AuthoritySlot extends ProcessorSlot {

    private String resourceName;

    private Set<String> roles;

    private String limitApp;

    private String requestOrigin;

    // ...
}

AuthoritySlot通过resourceName属性来指定受保护的资源名称,roles属性来指定允许访问该资源的角色,limitApp属性来限制只能从某些应用发起的请求,requestOrigin属性来限制只能从某些来源发起的请求。

SystemSlot:系统规则校验

Sentinel通过SystemSlot来实现对系统资源的保护,它可以根据CPU、内存、线程池等系统指标来控制请求的执行。

public class SystemSlot extends ProcessorSlot {

    private String resourceName;

    private double threshold;

    // ...
}

SystemSlot通过resourceName属性来指定受保护的资源名称,threshold属性来指定资源的使用阈值。当资源的使用超过阈值时,Sentinel会拒绝请求的执行。

FlowSlot:流控规则校验

Sentinel通过FlowSlot来实现对流量的控制,它可以根据QPS、并发数等流量指标来控制请求的执行。

public class FlowSlot extends ProcessorSlot {

    private String resourceName;

    private int count;

    private int intervalInMs;

    // ...
}

FlowSlot通过resourceName属性来指定受保护的资源名称,count属性来指定允许通过的请求数量,intervalInMs属性来指定允许通过的请求的时间间隔。当请求数量超过count或时间间隔超过intervalInMs时,Sentinel会拒绝请求的执行。

结语

Sentinel通过AuthoritySlot、SystemSlot和FlowSlot这三个Slot来实现对请求的授权、系统资源的保护和流量的控制。通过合理地配置这些Slot,我们可以对系统进行全面的保护,防止系统过载和资源耗尽,提高系统的稳定性和可用性。