sentinel快速入门指南

1 篇文章 0 订阅
1 篇文章 0 订阅

针对版本1.8.0+,目前最新版本1.8.2

官方入门文档 可以大概了解看一下

关注点/功能

第一部分入门关注点、不需要了解太深,先会用,有效果再说

  • sentinel 干什么

sentinel 流控、限流、熔断、系统保护(根据当前系统资源例如cpu使用率等设置阈值)sentinel会自适应的调节流控,说白了sentinel就是一个可以让服务更健康稳定且最大限度增加吞吐量的一个框架

  • sentinel 怎么用

本篇主要针对 Springboot 以及 SpringCloudAlibaba的使用方案,而且以sentinel的视角出发来分析为啥要这么用

  • sentinel 怎么和现有流行框架 SpringBoot/Cloud、Cloud Gatway网关结合使用

sentinel提供现有很多种框架结合方案,SpringBoot/Cloud等都有, 主流框架适配 该地址为官方的文档地址

SpringBoot使用

这里使用1.8.0版本,跟一个 官方文档

  • 引入jar
		<!-- aspectj -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
            <version>1.8.0</version>
        </dependency>
最基础用法

可以跑一下看看,会发现被限流了,有个地方,就是sentinel限流是按照resource来限流的,如果把testFlow方法改成非静态方法,并且BasicSentinel 对象不同的情况下,也是会触发限流的,因为sentinel是根据resource来限流的

public class BasicSentinel {

    public static void main(String[] args) throws InterruptedException {
        //设置流控规则
        initFlowQpsRule("BasicSentinel.testFlow");
        //线程调用,因为面对是QPS限流,这里线程尽量多一点,并且每次循环等待1秒
        //便于sentinel统计
        for (int count = 0; count < 20; count++) {
            for (int i = 0; i < 30; i++) {
                new Thread(() -> {
                    System.out.println("线程名称:" + Thread.currentThread().getName()
                            + ",返回值:" + BasicSentinel.testFlow("循环值:" + Thread.currentThread().getName()));
                }).start();
            }
            Thread.sleep(1000);
        }
        Thread.sleep(1000 * 25);
    }

    public static void initFlowQpsRule(String resourceName) {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule(resourceName);
        // qps 限制,最大20
        rule.setCount(20);
        // 设置限制等级,按QPS限制
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // 设置限制应用,名称就 默认吧
        rule.setLimitApp("default");
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
    
    /**
     * 测试流控规则
     */
    public static String testFlow(String hello) {
        try (Entry entry = SphU.entry("BasicSentinel.testFlow")) {
            // 被保护的业务逻辑
            return hello;
        } catch (BlockException ex) {
            // 资源访问阻止,被限流或被降级
            return "触发流控";
        }
    }
}
注解使用方式

注解方式,需要结合SpringBoot,而且也不像基础版本,可以静态方法,静态方法肯定不行滴,注解方式使用Spring aop实现的,大概示例

@Configuration
public class AnnotationSentinel {

    public AnnotationSentinel() {
    	//这里初始化一下规则
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule("AnnotationSentinel.hello");
        // qps 限制,最大20
        rule.setCount(20);
        // 设置限制等级,按QPS限制
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // 设置限制应用,名称就 默认吧
        rule.setLimitApp("default");
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }

    public void test() {
    }

    //测试用,可以放到SpringBoot的 Application中测试,也可以自己写测试文件
//    public void test(){
//        //context为ApplicationContext
//        AnnotationSentinel.Test test = context.getBean(AnnotationSentinel.Test.class);
//        for (int count = 0; count < 20; count++) {
//            for (int i = 0; i < 30; i++) {
//                new Thread(() -> {
//                    System.out.println("线程名称:" + Thread.currentThread().getName()
//                            + ",返回值:" + test.hello("循环值:" + Thread.currentThread().getName()));
//                }).start();
//            }
//            Thread.sleep(1000);
//        }
//        Thread.sleep(1000 * 25);
//    }

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
    	//把SentinelResourceAspect切面类放入SpringBean中
        return new SentinelResourceAspect();
    }

    @Component
    public static class Test {
        // 原函数
        @SentinelResource(value = "AnnotationSentinel.hello", blockHandler = "exceptionHandler")
        public String hello(String s) {
            return s;
        }

        // Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
        public String exceptionHandler(String s, BlockException ex) {
            return "触发流控";
        }
    }
}
结合web使用

大部分情况咱们还是直接就结合Http api接口使用 官方文档

maven引入

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-web-servlet</artifactId>
            <version>1.8.0</version>
        </dependency>

这个就不过多介绍了,官方文档写的很清楚,而且都是在上面基础版上,扩展出来的,所以没什么好过多介绍的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值