# sentinel-quickstart **Repository Path**: wangfugui-ma/sentinel-quickstart ## Basic Information - **Project Name**: sentinel-quickstart - **Description**: sentinel的quick-start - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-06-09 - **Last Updated**: 2023-12-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: sentinel, SpringBoot ## README ## 什么是流量控制 > 流量控制在网络传输中是一个常用的概念,它用于调整网络包的发送数据。然而,从系统稳定性角度考虑,在处理请求的速度上,也有非常多的讲究。任意时间到来的请求往往是随机不可控的,而系统的处理能力是有限的。我们需要根据系统的处理能力对流量进行控制。Sentinel > 作为一个调配器,可以根据需要把随机的请求调整成合适的形状,如下图所示: ![在这里插入图片描述](https://img-blog.csdnimg.cn/fd3a37c829dc4a56842c2e071cbd5d87.png) ## 流量控制设计理念 > 流量控制有以下几个角度: > > 资源的调用关系,例如资源的调用链路,资源和资源之间的关系; > > 运行指标,例如 QPS、线程池、系统负载等; > > 控制的效果,例如直接限流、冷启动、排队等。 > > Sentinel 的设计理念是让您自由选择控制的角度,并进行灵活组合,从而达到想要的效果。 上面介绍完了,下面带大家来快速体验一下sentinel,查看一下他的效果。 ## 第一步,创建一个springboot工程 ![在这里插入图片描述](https://img-blog.csdnimg.cn/dd7912cede4c4eec9044a9c6aae94dba.png) ## 第二步,引入sentinel依赖 ```java com.alibaba.csp sentinel-core 1.7.2 ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/8543a9cbbdd449709b9dcdc679752781.png) ## 第三步,编写TestController类 ![在这里插入图片描述](https://img-blog.csdnimg.cn/55ff6ebf25cc451291bcec67f2bae5f3.png) ## 第四步,编写sentinel的相关方法 ```java @PostConstruct public void initFlowRules() { List rules = new ArrayList<>(); FlowRule flowRule = new FlowRule(); flowRule.setResource("testSentinel"); flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); //限流一秒钟超过两次 flowRule.setCount(2); rules.add(flowRule); FlowRuleManager.loadRules(rules); } ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/5c311fd4cfaf459188d487112e1bfcfd.png) `@PostConstruct`这个注解的作用就是在这个TestController类被实例化的时候调用这个方法, flowRule.setCount(2); 这里的意思就是控制一次请求在一秒内只能调用两次 ## 第五步,编写测试方法 ```java @GetMapping("/hello") public String hello() { try(Entry entry = SphU.entry("testSentinel")) { return "hello sentinel"; } catch (BlockException e) { e.printStackTrace(); return "系统繁忙"; } } ``` ![在这里插入图片描述](https://img-blog.csdnimg.cn/388cde8a452843368019bfdeae7eb2ce.png) ## 第六步,测试我们的方法 如果我们正常调用: ![在这里插入图片描述](https://img-blog.csdnimg.cn/e7ed2ecffe2a4c7cb0cbe911986eb755.png) 如果我们一次点击超过一秒内两次: ![在这里插入图片描述](https://img-blog.csdnimg.cn/547fd0ab053c4ddfa07e8b2068219dd6.png) 仓库地址: [https://gitee.com/WangFuGui-Ma/sentinel-quickstart](https://gitee.com/WangFuGui-Ma/sentinel-quickstart)