当前位置:网站首页>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 "系统繁忙";
}
}

第六步,测试我们的方法
如果我们正常调用:
如果我们一次点击超过一秒内两次:
仓库地址:
边栏推荐
- 剑指 Offer 59 - II. 队列的最大值
- Linux Installation mysql8
- nacos 问题
- Zotero journal automatic matching update influence factor
- 使用Gunicorn部署web.py应用
- Canonical engineers are trying to solve the performance problem of Firefox snap
- 1404萬!四川省人社廳關系型數據庫及中間件軟件系統昇級采購招標!
- [boutique] detailed explanation of Pinia
- SSH命令及使用说明
- 雪花id,分布式唯一id
猜你喜欢

A keepalived high availability accident made me learn it again!

Game maker Foundation presents: Valley of belonging

Real time tracking of bug handling progress of the project through metersphere and dataease

JVM(4) 字節碼技術+運行期優化

npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed

畫虎國手孟祥順數字藏品限量發售,隨贈虎年茅臺

How important is it to make a silver K-line chart?

Detailed description of gaussdb (DWS) complex and diverse resource load management methods

As the "only" privacy computing provider, insight technology is the "first" to settle in the Yangtze River Delta data element circulation service platform

云服务器的安全设置常识
随机推荐
Sword finger offer 66 Building a product array
lock4j--分布式锁中间件--自定义获取锁失败的逻辑
Zotero journal Automatic Matching Update Influencing Factors
3-2 host discovery - layer 3 discovery
画虎国手孟祥顺数字藏品限量发售,随赠虎年茅台
Oracle11.2.0.4-rac cluster hang analysis record
[boutique] detailed explanation of Pinia
The era of data security solutions
Flume配置4——自定義Source+Sink
Linux Installation mysql5
Zotero期刊自动匹配更新影响因子
【Try to Hack】vulnhub narak
Flume-ng配置
Linux Installation mysql8
Flume configuration 1 - basic case
测试方法学习
JVM(2) 垃圾回收
Following the crowd hurts you
雪花id,分布式唯一id
ETCD数据库源码分析——服务端PUT流程