栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

阿里开源限流、降级工具Sentinel介绍与使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

阿里开源限流、降级工具Sentinel介绍与使用

Sentinel 是什么?

简言之,就是用于管理应用服务的流量控制、熔断降级的组件。当我们的服务有流量控制、熔断降级方面的需求时,可以考虑使用该组件。

Sentinel: 分布式系统的流量防卫兵
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统自适应保护等多个维度来帮助您保障微服务的稳定性。(摘自官方文档,更多介绍详见:https://sentinelguard.io/zh-cn/docs/introduction.html)

使用

使用方式主要是通过三个步骤:

  1. 定义资源resources (可以理解为Map中的key)
  2. 定义与资源相对应的规则rules (可以理解为Map中的value)
  3. 方法被调用时,通过Entry entry = SphU.entry(resource);进入Sentinel组件,通过resource找到对应rule,从而触发对应的限流或降级规则。
示例

官网教程中有一些demo,这里补充一个Springboot项目中的实际应用示例。
使用Apollo管理限流配置,更加灵活,可动态修改和加载。

@Configuration
@EnableApolloConfig
@Slf4j
public class SentinelAutoConfig {

    private static Map flowQpsRuleMap;

    @ApolloJSONValue("${sentinel.flowrule:{"sayHello":{"resource":"sayHello","count":1,"grade":1,"limitApp":"default"},"sayBye":{"resource":"sayBye","count":20,"grade":1,"limitApp":"default"},"testA":{"resource":"testA","count":10,"grade":1,"limitApp":"default"}}}")
    public void setFlowQpsRuleMap(Map data) {
        flowQpsRuleMap = data;
        log.info("update apollo flowQpsRuleMap:{}", flowQpsRuleMap);
        this.loadFlowQpsRules(flowQpsRuleMap);
    }

    private void loadFlowQpsRules(Map flowQpsRuleMap) {
        List flowRules = new ArrayList<>();
        flowQpsRuleMap.forEach((k, v) -> {
            FlowRule flowRule = JSON.parseObject(JSON.toJSONString(v), FlowRule.class);
            flowRules.add(flowRule);
        });
        FlowRuleManager.loadRules(flowRules);
        log.info("loadFlowQpsRules success, flowRules={}", FlowRuleManager.getRules());
    }

}
@Service
@Slf4j
public class SentinelService {

    @SentinelResource(value = "sayHello")
    public String sayHello(String name) {
        Entry entry = null;
        // 务必保证finally会被执行
        try {
            // 资源名可使用任意有业务语义的字符串
            entry = SphU.entry(name);
            // 被保护的业务逻辑
            return "Hello, " + name;
        } catch (BlockException e) {
            // 资源访问阻止,被限流或被降级
            // 进行相应的处理操作
            log.error("sayHello BlockException");
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }
        return null;
    }

    @SentinelResource(value = "sayBye")
    public String sayBye(String name) {
        return "Bye, " + name;
    }

    
    public String flowRuleTest(SentinelDTO sentinelDTO) {
        Entry entry = null;
        // 务必保证finally会被执行
        try {
            // 资源名可使用任意有业务语义的字符串
            entry = SphU.entry(sentinelDTO.getName());
            // 被保护的业务逻辑
            printName(sentinelDTO);
            return sentinelDTO.getName();
        } catch (BlockException e) {
            // 资源访问阻止,被限流或被降级
            // 进行相应的处理操作
            return handleBlockExecption(sentinelDTO);
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }

    }

    
    private String handleBlockExecption(SentinelDTO sentinelDTO) {
        log.error("flowRuleSayHello handleBlockExecption, sentinelDTO={}", sentinelDTO);
        return "handleBlockExecption";
    }

    private void printName(SentinelDTO sentinelDTO) {
        System.out.println(sentinelDTO.getName());
    }

}

写一个测试Controller,使用Jmeter调用,可观察到测试结果如下:
请求flowRuleTest方法,触发“testA”的限流规则,由于使用的压测工具设置的qps为100,所以观察到结果如下:前10次请求正常,之后触发限流,执行了handleBlockExecption逻辑


本文代码示例详见github:
https://github.com/dangershi/common-utils
如有帮到你,麻烦点个star,谢谢啦~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943345.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号