当前位置:网站首页>Sentinel的快速入门,三分钟带你体验流量控制
Sentinel的快速入门,三分钟带你体验流量控制
2022-06-29 19:58:00 【掉头发的王富贵】
什么是流量控制
流量控制在网络传输中是一个常用的概念,它用于调整网络包的发送数据。然而,从系统稳定性角度考虑,在处理请求的速度上,也有非常多的讲究。任意时间到来的请求往往是随机不可控的,而系统的处理能力是有限的。我们需要根据系统的处理能力对流量进行控制。Sentinel
作为一个调配器,可以根据需要把随机的请求调整成合适的形状,如下图所示:

流量控制设计理念
流量控制有以下几个角度:
资源的调用关系,例如资源的调用链路,资源和资源之间的关系;
运行指标,例如 QPS、线程池、系统负载等;
控制的效果,例如直接限流、冷启动、排队等。
Sentinel 的设计理念是让您自由选择控制的角度,并进行灵活组合,从而达到想要的效果。
上面介绍完了,下面带大家来快速体验一下sentinel,查看一下他的效果。
第一步,创建一个springboot工程

第二步,引入sentinel依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.7.2</version>
</dependency>

第三步,编写TestController类

第四步,编写sentinel的相关方法
@PostConstruct
public void initFlowRules() {
List<FlowRule> 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);
}

@PostConstruct这个注解的作用就是在这个TestController类被实例化的时候调用这个方法,
flowRule.setCount(2);
这里的意思就是控制一次请求在一秒内只能调用两次
第五步,编写测试方法
@GetMapping("/hello")
public String hello() {
try(Entry entry = SphU.entry("testSentinel")) {
return "hello sentinel";
} catch (BlockException e) {
e.printStackTrace();
return "系统繁忙";
}
}

第六步,测试我们的方法
如果我们正常调用:
如果我们一次点击超过一秒内两次:
仓库地址:
边栏推荐
- Configuration du Flume 4 - source personnalisée + sink
- Linux安装MySQL5
- Linux Installation mysql8
- 使用Gunicorn部署web.py应用
- JVM (4) Bytecode Technology + Runtime Optimization
- Game maker Foundation presents: Valley of belonging
- JMeter BeanShell explanation and thread calling
- 并查集(Union-Find)
- How to use filters in jfinal to monitor Druid for SQL execution?
- 从众伤害的是自己
猜你喜欢

Foxit software was invited to appear at the 2022 advanced manufacturing digital intelligence development forum

Flume configuration 4 - Custom source+sink

【编译原理】语义分析

How to set a pod to run on a specified node

JVM(4) 字节码技术+运行期优化

Canonical engineers are trying to solve the performance problem of Firefox snap

Configuration du Flume 4 - source personnalisée + sink

ASP.Net Core创建Razor页面上传多个文件(缓冲方式)(续)

数据链路层

童年经典蓝精灵之百变蓝爸爸数字藏品中奖名单公布
随机推荐
14.04 million! Sichuan provincial human resources and social security department relational database and middleware software system upgrade procurement bidding!
Following the crowd hurts you
JVM (2) garbage collection
14,04 millions! Appel d'offres pour la mise à niveau de la base de données relationnelle et du système logiciel Middleware du Département des ressources humaines et sociales de la province du Sichuan!
Common knowledge of ECS security settings
npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
Linux安装MySQL5
并查集(Union-Find)
Lock4j -- distributed lock Middleware -- customize the logic of lock acquisition failure
苹果iPhone手机升级系统内存空间变小不够如何解决?
网站压力测试工具——Webbench
一个超赞的开源的图片去水印解决方案
2022年深圳市福田区支持先进制造业发展若干措施
Flume配置3——拦截器过滤
Nacos problem
Deficiencies and optimization schemes in Dao
MySQL remote connection
As the "only" privacy computing provider, insight technology is the "first" to settle in the Yangtze River Delta data element circulation service platform
Dynamics crm: among locally deployed servers, sandbox, unzip, VSS, asynchronous and monitor services
童年经典蓝精灵之百变蓝爸爸数字藏品中奖名单公布