zoukankan      html  css  js  c++  java
  • 一个名叫Sentinel-Rules-SDK的组件,使得Sentinel的流控&熔断规则的配置更加方便

    原文链接:一个名叫Sentinel-Rules-SDK的组件,使得Sentinel的流控&熔断规则的配置更加方便

    1 Sentinel 是什么?

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

    2 Sentinel 具有以下特征:

    丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
    完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
    广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
    完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

    好了,其他的就不多做介绍了,大家可以看官网:https://github.com/alibaba/Sentinel/wiki/介绍

    3 如何接入 Sentinel

    之前我写过一篇文章讲Dubbo如何引入Sentinel做熔断限流,而其中为资源配置限流规则和熔断规则有两种方式:

    • 硬代码初始化:我们可以利用 static 静态代码块来初始化,但是如果项目每次要为资源修改规则,就要修改项目代码,然后重新打包部署
    • 利用控制台来配置:因为硬代码的不方便,所以一般我们会利用控制台来为资源配置流控和熔断规则。这个只能在项目启动后获取到所有资源,然后一个一个配置,但是这时候如果大流量打过来,就很容易导致系统崩溃。

    所以暂时最好的实践就是:硬代码+控制台

    在项目启动时,可以利用硬代码先将资源初始化好,接着控制台可以随时调整根据线上的运行情况调整规则。

    4 Sentinel-Rules-SDK

    但是,在我看来,我还是比较喜欢将初始化规则维护在配置文件中,由配置中心统一管理Sentinel的流控规则和熔断规则。

    因为有了上面的实践和思考,所以自己研发了一款简单易用的组件,将资源的流控&熔断规则放在配置文件中。

    • 在项目启动时,SDK会读取配置文件中的配置,然后进行 Sentinel 规则的初始化;
    • 如果团队还用了 Sentinel 提供的控制台,当带 Sentinel 规则的接口被调用时,就会将所有的限流规则和熔断规则上传到 Sentinel 控制台中;
    • 接下来,我们可以在控制台上看实时监控反馈的情况,根据需要来调整流控规则和熔断规则,控制台调整后的规则会同步到项目的内存中。

    4.1 使用介绍

    引入依赖:

    <dependency>
        <groupId>com.github.howinfun</groupId>
        <artifactId>sentinel-rules-sdk</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    

    自己在本地拉代码,然后deploy,最后引入依赖

    规则配置:

    属性基本和Sentinel 提供的 FlowRule 和 DegradeRule 一一对应上,满足基本功能点。
    详情看下面例子:

    # 开启组件功能
    sentinel.rules.enabled=true
    # 流控&熔断规则配置
    sentinel.rules.flowRuleList[0].resource=sayHello
    sentinel.rules.flowRuleList[0].grade=1
    sentinel.rules.flowRuleList[0].count=1
    
    sentinel.rules.degradeRuleList[0].resource=sayHello
    sentinel.rules.degradeRuleList[0].grade=2
    sentinel.rules.degradeRuleList[0].count=1
    sentinel.rules.degradeRuleList[0].timeWindow=1
    sentinel.rules.degradeRuleList[0].minRequestAmount=1
    sentinel.rules.degradeRuleList[0].statIntervalMs=10000
    
    sentinel.rules.flowRuleList[1].resource=sayHi
    sentinel.rules.flowRuleList[1].grade=1
    sentinel.rules.flowRuleList[1].count=1
    
    sentinel.rules.degradeRuleList[1].resource=sayHi
    sentinel.rules.degradeRuleList[1].grade=2
    sentinel.rules.degradeRuleList[1].count=1
    sentinel.rules.degradeRuleList[1].timeWindow=1
    sentinel.rules.degradeRuleList[1].minRequestAmount=1
    sentinel.rules.degradeRuleList[1].statIntervalMs=10000
    

    使用介绍已经结束,就是这么简单,毕竟原理其实就是拿到配置的内容,然后进行初始化。下面看看功能是如何实现的。

    4.2 实现介绍

    核心类有两个:SentinelRulesProperties 和 SentinelRulesGenerateConfig

    看名称我们就知道这两个类的功能点了。第一个是属性类,负责读取配置文件中的规则。第二个是配置类,负责初始化流控规则和熔断规则。

    SentinelRulesProperties:

    SentinelRulesProperties 非常简单,只是利用 @ConfigurationProperties 注解来完成配置规则的注入。
    当然了,我们要记得找个地方利用 @EnableConfigurationProperties 将这个属性类加上,不然读取不了配置文件中的规则内容。

    /**
     * Sentinel规则配置
     * @author winfun
     * @date 2021/3/4 4:26 下午
     **/
    @Data
    @ConfigurationProperties(prefix = "sentinel.rules")
    public class SentinelRulesProperties {
    
        private Boolean enabled;
        private List<SentinelFlowRule> flowRuleList;
        private List<SentinelDegradeRule> degradeRuleList;
    }
    

    SentinelRulesGenerateConfig:

    SentinelRulesGenerateConfig 配置类也很简单,主要是利用 Spring 提供的监听器,监听ContextRefreshedEvent事件,即Spring容器初始化完毕,接着利用 ApplicationContext 来获取上面的属性类,然后读取流控规则和熔断规则,最后进行初始化。

    /**
     * Sentinel规则自动生成配置类
     * @author winfun
     * @date 2021/3/4 4:29 下午
     **/
    @Configuration
    @ConditionalOnProperty(prefix = "sentinel.rules", name = "enabled", havingValue = "true")
    @EnableConfigurationProperties({SentinelRulesProperties.class})
    public class SentinelRulesGenerateConfig implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {
    
        private ApplicationContext applicationContext;
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            SentinelRulesProperties sentinelRulesProperties =
                    this.applicationContext.getBean(SentinelRulesProperties.class);
            List<SentinelFlowRule> flowRuleList = sentinelRulesProperties.getFlowRuleList();
            List<SentinelDegradeRule> degradeRuleList = sentinelRulesProperties.getDegradeRuleList();
    
            final List<FlowRule> flowRules = new ArrayList<>();
            final List<DegradeRule> degradeRules = new ArrayList<>();
            // 处理流控规则
            flowRuleList.forEach(sentinelFlowRule -> {
                FlowRule flowRule = new FlowRule();
                BeanUtils.copyProperties(sentinelFlowRule,flowRule);
                flowRules.add(flowRule);
            });
            // 处理熔断规则
            degradeRuleList.forEach(sentinelDegradeRule -> {
                DegradeRule degradeRule = new DegradeRule();
                BeanUtils.copyProperties(sentinelDegradeRule,degradeRule);
                degradeRules.add(degradeRule);
            });
            FlowRuleManager.loadRules(flowRules);
            DegradeRuleManager.loadRules(degradeRules);
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    }
    

    如果大家对这个组件还感兴趣的话,可以到Github上看看:https://github.com/Howinfun/study-in-work-and-life/tree/master/sentinel-rules-sdk

    今天,你学习了吗
  • 相关阅读:
    yjh_study_command
    installed_oracle_can't_use
    Database 2 Day DBA guide_Chapter3
    oracle_great_integration_译文
    oracle_set_autocommit
    Database 2 Day DBA guide_Chapter2
    linux_base_commond_two
    linux_base_commond_one
    Oracle_11gR2_概念_第06章_数据字典和动态性能视图_英文词汇
    angular 自定义select选项,tab切换!!!
  • 原文地址:https://www.cnblogs.com/Howinfun/p/14539527.html
Copyright © 2011-2022 走看看