温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Sentinel限流的使用方法

发布时间:2021-06-22 16:40:30 来源:亿速云 阅读:256 作者:chen 栏目:大数据

这篇文章主要讲解了“Sentinel限流的使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Sentinel限流的使用方法”吧!

Sentinel 系列教程-使用Sentinel限流

前言: Sentinel 是由alibaba出品的,针对于系统负载保护的组件,其有丰富的流量防护手段和多样化的流量整型策略而被广大使用。 以下是转自Sentinel官方的介绍: 随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的轻量级流量控制组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护等多个维度来帮助您保障微服务的稳定性。 Sentinel-wiki Sentinel GitHub wiki 有兴趣的童鞋可以去了解。

本文以Sentinel 1.6.3 为例;

引入sentinel-core

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.6.3</version>
</dependency>

加载规则信息

private static final String RESOURCE_NAME = "hello";
/**
 * 加载限流规则
 */
public static void loadRules(){
    List<FlowRule> rules = new ArrayList<FlowRule>();
    FlowRule flowRule = new FlowRule();
    flowRule.setResource(RESOURCE_NAME); //资源名
    flowRule.setLimitApp("default");//default 代表对所有应用生效
    flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); //限流阈值类型
    flowRule.setCount(10); //阈值
    rules.add(flowRule);
    FlowRuleManager.loadRules(rules);
}

添加资源入口处理限流异常

/**
 * 执行方法
 * @param hello
 */
public static void hello(String hello){
    Entry entry = null;
    try {
        entry = SphU.entry(RESOURCE_NAME);
        log.info("args hello value is {}",hello);
    }catch (Exception e){
        if(FlowException.isBlockException(e)){
            log.error("block resourceName: {}",RESOURCE_NAME);
        }
    }finally {
        if(entry !=null){
            entry.exit();
        }
    }
}

模拟调用测试

public static void main(String[] args) {
    loadRules();
    for(int i=0;i<20;i++){
        hello("hello"+i);
    }
}

运行记录

15:26:08.453 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello0
15:26:08.458 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello1
15:26:08.458 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello2
15:26:08.458 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello3
15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello4
15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello5
15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello6
15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello7
15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello8
15:26:08.459 [main] INFO com.example.sentinel.sentineldemo.SampleFlowDemo - args hello value is hello9
15:26:08.492 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.492 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.493 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello
15:26:08.494 [main] ERROR com.example.sentinel.sentineldemo.SampleFlowDemo - block resourceName: hello

感谢各位的阅读,以上就是“Sentinel限流的使用方法”的内容了,经过本文的学习后,相信大家对Sentinel限流的使用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI