sentinel

sentinel

起男 541 2021-08-22

sentinel

基本概念

  • 资源:资源是sentinel的关键概念。它可以是java应用程序中的任何内容,可以是一个服务,也可以是一个方法,甚至可以是一段代码。总之就是sentinel要保护的东西
  • 规则:作用于资源之上,定义以什么样的方式保护资源,主要包括流量控制规则,熔断降级规则以及系统保护规则。也就是如何保护资源

主要功能

sentinel的主要功能就是容错,

  • 保证自己不被上游服务压垮:流量控制
  • 保证自己不被下游服务拖垮:熔断降级
  • 保证外界环境良好:系统负载保护

流控规则

流量控制,其原理是监控应用流量的qps(每秒查询率)或并发线程数等指标,当达到指定的阈值时对流量进行控制,以避免被瞬时的流量高峰冲垮,从而保障应用的高可用性

操作

控制台中:簇点链路->指定资源->添加流控

  • 资源名:唯一即可
  • 针对来源:指定对哪个微服务进行限流,默认指default,意思是不区分来源,全部限制
  • 阈值类型
    • qps:每秒请求数
    • 线程数:最大并发线程

流控模式

  • 直接:默认,接口达到限流条件时开启限流
  • 关联:当关联的资源达到限流条件时,开启限流
  • 链路:当从某个接口过来的资源达到限流条件时,开启限流

链路流控模式指的是,当从某个接口过来的资源达到限流条件时,开启限流。它的功能有点类似于针对来源配置项,区别在于:针对来源是针对上级微服务,而链路流控是针对上级接口,也就是说它的粒度更细

流控效果

  • 快速失败:默认,直接失败,抛出异常
  • warm up(预热):会有一个缓冲阶段,适用于将突然增大的流量转换为缓步增长的场景
  • 排队等待:让请求以均匀的速度通过,单机阈值为每秒通过数量,其余的排队等待

降级规则

降级规则就是设置当满足什么条件的时候,对服务进行降级处理

降级策略

  • rt:平均响应时间,单资源的平均响应时间超过阈值(单位ms)之后,资源进入准降级状态,如果接下来1s连续的5个请求,他们的rt全都超过阈值,接下来的时间窗口内,会进行降级

    rt最大4900,若要更多需要添加启动配置项

  • 异常比例:当资源的每秒异常总数占通过量的比值超过阈值后,接下来的时间窗口内,进入降级状态

  • 异常数:当资源近1分钟的异常数目超过阈值之后,记下来的时间窗口内,会进行服务降级

    由于系统统计时间窗口是分钟级别的,若时间窗口小于60s,则熔断结束后可能再次进入熔断状态

热点规则

热点参数流控规则是一种更细粒度的流控规则,它允许将规则具体到参数上。热点参数限流会统计参数中的热点参数,并根据配置的限流阈值与模式,对包含热点参数的资源调用生效
如果想使用热点规则,则需要再控制层的相关方法中使用@SentinelResource注解声明

参数

  • 参数索引:参数的下标

授权规则

很多时候需要根据调用来源来判断该次请求释放允许放行,这时候可以使用sentinel的来源访问控制的功能。来源访问控制根据资源的请求来源(origin)限制资源释放通过

  • 若配置白名单,则只有请求来源位于白名单内时才可通过
  • 若配置黑名单,则请求来源位于黑名单时不通过,其余的请求通过

流控应用

其实这个位置要填写的是来源标识,Sentinel提供了 RequestOriginParser 接口来处理来源

只要Sentinel保护的接口资源被访问,Sentinel就会调用 RequestOriginParser 的实现类去解析

访问来源

系统规则

系统保护规则是从应用级别的入口流量进行控制,从单台机器的总体load、rt、入口qps、cpu使用率和线程数五个维度进行监控应用数据,让系统尽可能抱在最大吞吐量的同时保证系统整体的稳定性

系统保护规则是应用整体维度的,而不是资源维度的,并且仅对入口流量(进入应用的流量)生效

自定义异常返回

/**
 * 异常处理
 */
@Component
public class ExceptionHandlerPage implements UrlBlockHandler {

    //BlockException 异常接口,包含Sentinel的五个异常
    // FlowException 限流异常
    // DegradeException 降级异常
    // ParamFlowException 参数限流异常
    // AuthorityException 授权异常
    // SystemBlockException 系统负载异常
    @Override
    public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException {
        response.setContentType("application/json;charset=utf-8"); 
        ResponseData data = null; 
        if (e instanceof FlowException) { 
            data = new ResponseData(-1, "接口被限流了..."); 
        } else if (e instanceof DegradeException) { 
            data = new ResponseData(-2, "接口被降级了..."); 
        }
        response.getWriter().write(JSON.toJSONString(data));
    }

    @Data
    @AllArgsConstructor//全参构造
    @NoArgsConstructor//无参构造
    class ResponseData {
        private int code;
        private String message;
    }
}

@SentinelResource

用于定义资源,并提供可选的异常处理和fallback配置项

属性作用
value资源名称
entryTypeentry类型,标记流量的方向,取值in/out,默认out
blockHandler处理BlockExcpetion(sentinel自身异常)的函数名称
blockHandlerClass存放blockHandler的类,对应的处理函数必须static修饰
fallback用于在抛出异常的时候提供fallback处理逻辑。fallback函数可以针对所有类型的异常进行处理
fallbackClass存放fallback的类。对应的处理函数必须static修饰
defaultFallback用于通用的fallback逻辑。默认fallback函数可以针对所有类型的异常进行处理。若同时配置了fallback和defaultFallback,以fallback为准
exceptionsToIgnore指定排除掉哪些异常。排除的异常不会计入异常统计,也不会进入fallback逻辑,而是原样抛出
exceptionsToTrace需要trace的异常

规则持久化

sentinel的规则默认是保持在内存中的,当微服务重启后,原本设置的规则将会丢失,这样是很麻烦的

sentinel规则持久化使用的是本地文件,本地文件数据源会定时轮询文件的变更,读取规则。这样我们既可以在应用本地直接修改文件来更新规则,也可以通过sentinel控制台推送规则

首先sentinel控制台通过api将规则推送至客户端并更新到内存中,接着注册的写数据源会将新的规则保持到本地的文件中

  1. 配置类

    import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
    import com.alibaba.csp.sentinel.datasource.*;
    import com.alibaba.csp.sentinel.init.InitFunc;
    import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
    import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
    import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
    import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
    import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
    import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
    import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
    import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
    import com.alibaba.csp.sentinel.slots.system.SystemRule;
    import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
    import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.TypeReference;
    import org.springframework.beans.factory.annotation.Value;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    
    
    public class FilePersistence implements InitFunc {
    
        @Value("spring.application.name")
        private String applicationName;
    
        private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
                source,
                new TypeReference<List<FlowRule>>() {
                }
        );
    
        private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
                source,
                new TypeReference<List<DegradeRule>>() {
                }
        );
    
        private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
                source,
                new TypeReference<List<SystemRule>>() {
                }
        );
    
        private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
                source,
                new TypeReference<List<AuthorityRule>>() {
                }
        );
    
        private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
                source,
                new TypeReference<List<ParamFlowRule>>() {
                }
        );
    
        @Override
        public void init() throws Exception {
            // TIPS: 如果你对这个路径不喜欢,可修改为你喜欢的路径
            String ruleDir = System.getProperty("user.home") + "/sentinel-rules/" + applicationName;
            String flowRulePath = ruleDir + "/flow-rule.json";
            String degradeRulePath = ruleDir + "/degrade-rule.json";
            String systemRulePath = ruleDir + "/system-rule.json";
            String authorityRulePath = ruleDir + "/authority-rule.json";
            String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
    
            this.mkdirIfNotExits(ruleDir);
            this.createFileIfNotExits(flowRulePath);
            this.createFileIfNotExits(degradeRulePath);
            this.createFileIfNotExits(systemRulePath);
            this.createFileIfNotExits(authorityRulePath);
            this.createFileIfNotExits(paramFlowRulePath);
    
            // 流控规则
            ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(
                    flowRulePath,
                    flowRuleListParser
            );
    
            // 将可读数据源注册至FlowRuleManager
            // 这样当规则文件发生变化时,就会更新规则到内存
            FlowRuleManager.register2Property(flowRuleRDS.getProperty());
            WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
                    flowRulePath,
                    this::encodeJson
            );
    
            // 将可写数据源注册至transport模块的WritableDataSourceRegistry中
            // 这样收到控制台推送的规则时,Sentinel会先更新到内存,然后将规则写入到文件中
            WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
    
            // 降级规则
            ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
                    degradeRulePath,
                    degradeRuleListParser
            );
            DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
            WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
                    degradeRulePath,
                    this::encodeJson
            );
            WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
    
            // 系统规则
            ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
                    systemRulePath,
                    systemRuleListParser
            );
            SystemRuleManager.register2Property(systemRuleRDS.getProperty());
            WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
                    systemRulePath,
                    this::encodeJson
            );
            WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
    
            // 授权规则
            ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
                    authorityRulePath,
                    authorityRuleListParser
            );
            AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
            WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
                    authorityRulePath,
                    this::encodeJson
            );
            WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
    
            // 热点参数规则
            ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
                    paramFlowRulePath,
                    paramFlowRuleListParser
            );
            ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
            WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
                    paramFlowRulePath,
                    this::encodeJson
            );
            ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
        }
    
        private void mkdirIfNotExits(String filePath) throws IOException {
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        }
    
        private void createFileIfNotExits(String filePath) throws IOException {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
        }
    
        private <T> String encodeJson(T t) {
            return JSON.toJSONString(t);
        }
    }
    
    
  2. 添加配置

    在resources下创建配置目录META-INF/services,然后添加文件com.alibaba.csp.sentinel.init.InitFunc

    在文件中添加配置类的全路径

feign整合sentinel

  1. 导入依赖

    		<dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            </dependency>
    
  2. 配置文件中开启feign对sentinel的支持

    feign:
      sentinel:
        enabled: true
    
  3. 创建容错类:容错类要求必须实现被容错的接口,并为每个方法实现容错方案

  4. 为被容错的接口指定容错类

    @FeignClient(value = "被调用服务名",fallback = 容错类.class)
    

容错获取异常

在feign整合sentinel后,当调用失败时会执行容错类中的容错方案,但是这样就不不会抛出异常,也叫难以定位问题

  1. 创建容错工厂类:实现接口FallbackFactory接口的泛型是被容错接口

  2. 实现create方法,方法的参数就是发生的异常,可以自行进行处理;返回值是被容错接口,可用匿名内部类的形式返回容错类

  3. 为不被容错的接口指定容错类

    @FeignClient(value = "被调用服务名",fallbackFactory = 容错工厂类.class)
    

    fallback和fallbackFactory不可同时使用