Spring Cloud Alibaba基础教程:Sentinel
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统自适应保护等多个维度来保障微服务的稳定性。
1. 基本概念
1.1 资源
资源是 sentinel 中的一个关键概念,它代表受保护的内容,可以是程序中的任何内容,比如一个变量,一段代码等等。
1.2 规则
针对资源的控制规则,比如流量控制、熔断降级以及系统保护规则等,并且 sentinel 支持实时调整规则。
2. 原理
Sentinel 在熔断降级的设计理念上与 Hystrix 完全不一样。
- Hystrix 采用线程池的方式对资源进行隔离,这样做的好处是资源与资源做到了彻底的隔离,但是由于使用了线程池,存在上下文切换消耗,对性能有一定的影响。
- Sentinel 主要采取两种方式进行控制
- 通过并发线程数控制:限定访问资源的最大并发线程数,超过则直接拒绝后续请求,直到之前堆积的请求处理完毕才能接受新的请求。好处是没有线程池,不需要而外的上下文切换消耗。
- 通过请求响应时间对资源降级:当依赖的资源出现响应时间过长之后,后续所有请求都会被直接拒绝,直到过了指定的时间窗口期才会重新恢复。
3. 实战
3.1 部署控制台
下载 Sentinel,下载完成之后启动控制台
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

3.2 创建Spring Boot项目
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
配置文件
spring.application.name=sentinel-demo
server.port=9090
spring.cloud.sentinel.transport.dashboard=localhost:8080
添加Endpoint
@RestController
@SpringBootApplication
public class SentinelDemoApplication {
@GetMapping("echo")
public String echo(String message) {
return "Echo: Hello " + message;
}
public static void main(String[] args) {
SpringApplication.run(SentinelDemoApplication.class, args);
}
}
启动项目,测试Endpoint可以正常访问。接下来配置 Sentinel,添加流控规则

测试
curl 'http://localhost:9090/echo?message=bro'
可以看出来,如果在短时间内发出多个请求,会出现请求响应延迟和被拒绝的情况。说明配置的限流策略已经生效。
Blocked by Sentinel (flow limiting)
Sentinel 支持更多的流控策略,具体可以参考官方文档
Spring Cloud Alibaba基础教程:Sentinel的更多相关文章
- Spring Cloud Alibaba基础教程:Nacos的集群部署
继续说说生产环境的Nacos搭建,通过上一篇<Spring Cloud Alibaba基础教程:Nacos的数据持久化>的介绍,我们已经知道Nacos对配置信息的存储原理,在集群搭建的时候 ...
- Spring Cloud Alibaba基础教程:Nacos的数据持久化
前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...
- Spring Cloud Alibaba基础教程:Nacos配置的多文件加载与共享配置
前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...
- Spring Cloud Alibaba基础教程:Nacos配置的多环境管理
前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式> ...
- Spring Cloud Alibaba基础教程:Nacos配置的加载规则详解
前情回顾: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方式(Res ...
- Spring Cloud Alibaba基础教程:使用Nacos作为配置中心
通过本教程的前两篇: <Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现> <Spring Cloud Alibaba基础教程:支持的几种服务消费方 ...
- Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)
通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...
- Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现
自Spring Cloud Alibaba发布第一个Release以来,就备受国内开发者的高度关注.虽然Spring Cloud Alibaba还没能纳入Spring Cloud的主版本管理中,但是凭 ...
- Spring Cloud Alibaba基础教程:Nacos 生产级版本 0.8.0
昨晚Nacos社区发布了第一个生产级版本:0.8.0.由于该版本除了Bug修复之外,还提供了几个生产管理非常重要的特性,所以觉得还是有必要写一篇讲讲这次升级,在后续的文章中也都将以0.8.0版本为基础 ...
- Spring Cloud Alibaba基础教程-Nacos(三)
在Spring Cloud Alibaba基础教程-Nacos(二)当中学习了,如何使用 nacos图形化界面操作 ,使用Nacos部署集群,下面我们开始Nacos最后一篇的学习 ,如果对你有帮助,记 ...
随机推荐
- SVN报错“Failed to run the WC DB work queue associated with”解决办法
最近在更新SVN上的ISO代码时,失败报错: Failed to run the WC DB work queue associated with "目录/文件",clean u ...
- 『无为则无心』Python基础 — 4、Python代码常用调试工具
目录 1.Python的交互模式 2.IDLE工具使用说明 3.Sublime3工具的安装与配置 (1)Sublime3的安装 (2)Sublime3的配置 4.使用Sublime编写并调试Pytho ...
- 利用SPI机制实现责任链模式中的处理类热插拔
最近看到责任链模式的时候每增加一个处理类,就必须在责任链的实现类中手动增加到责任链中,具体代码基本就是list.add(new FilterImpl()),可以看到每次增加一个处理类,就必须添加一行上 ...
- Flask一分钟Mock一个API
如果安装了Python,并且安装了Flask: pip install flask 那么就可以在短短一分钟内Mock出来一个API,而且只需要用到一个文件. 彻底告别在线Mock网站无法指定请求方法, ...
- DOS命令行(10)——reg/regini-注册表编辑命令行工具
注册表的介绍 注册表(Registry,台湾.港澳译作登錄檔)是Microsoft Windows中的一个重要的数据库,用于存储系统和应用程序的设置信息. 1. 数据结构 注册表由键(key,或称 ...
- 你的电脑适合升级 Win11 吗?「GitHub 热点速览 v.21.26」
作者:HelloGitHub-小鱼干 WhyNotWin11 是个有意思的项目,本以为是从 360 度"抨击" Windows 11 的不好用之处,但它是一个实实在在地从硬件角度告 ...
- 在 NUC980 上运行 RT-Thread
NUC980 & RT-Thread (1) NUC980 nuc980 是新塘推出的基于 ARM926EJ-S,集成 64 MB 或 128 MB DDR-II 的处理器,主频可以达到300 ...
- mysql的主从复制延迟问题--看这一篇就够了
在之前我们已经讲解了一主一从,双主双从的mysql集群搭建,在单机应用的时候看起来没有问题,但是在企业的生产环境中,在很多情况下都会有复制延迟的问题. 主从复制的原理我们在此处就不再赘述了,之 ...
- SQL 清除数据库日志
exec sp_detach_db 'MCS4WLSQM','true' --运行上一个命令. --改名LOG文件后再关闭上一行代码,然后再运行下一行代码 exec sp_attach_single_ ...
- nohup启动 jar 不输出日志
简单暴力:nohup java -jar xxx.jar >/dev/null 2>&1 &