1.初识Sentinel
1.1.雪崩问题及解决⽅案
1.1.1.雪崩问题
微服务中,服务间调⽤关系错综复杂,⼀个微服务往往依赖于多个其它微服务。

如图,如果服务提供者I发⽣了故障,当前的应⽤的部分业务因为依赖于服务I,因此也会被阻塞。此时, 其它不依赖于服务I的业务似乎不受影响。


服务器⽀持的线程和并发数有限,请求⼀直阻塞,会导致服务器资源耗尽,从⽽导致所有其它服务都不可⽤,那么当前服务也就不可⽤了。
那么,依赖于当前服务的其它服务随着时间的推移,最终也都会变的不可⽤,形成级联失败,雪崩就发⽣了:

1.1.2.超时处理
解决雪崩问题的常⻅⽅式有四种:
•超时处理:设定超时时间,请求超过⼀定时间没有响应就返回错误信息,不会⽆休⽌等待
1.1.3.仓壁模式
仓壁模式来源于船舱的设计:


于此类似,我们可以限定每个业务能使⽤的线程数,避免耗尽整个tomcat的资源,因此也叫线程隔离。
1.1.4.断路器
断路器模式:由断路器统计业务执⾏的异常⽐例,如果超出阈值则会熔断该业务,拦截访问该业务的⼀切请求。
断路器会统计访问某个服务的请求数量,异常⽐例:


1.1.5.限流
流量控制:限制业务访问的QPS,避免服务因流量的突增⽽故障。

1.1.6.总结
什么是雪崩问题?
● 微服务之间相互调⽤,因为调⽤链中的⼀个服务故障,引起整个链路都⽆法访问的情况。
可以认为:
限流是对服务的保护,避免因瞬间⾼并发流量⽽导致服务故障,进⽽避免雪崩是⼀种预防措施。
超时处理、线程隔离、降级熔断是在部分服务故障时,将故障控制在⼀定范围,避免雪崩。是⼀种补救措施。
1.2.服务保护技术对⽐
在SpringCloud当中⽀持多种服务保护技术:
- Netfix Hystrix
- Sentinel
- Resilience4J
早期⽐较流⾏的是Hystrix框架,但⽬前国内实⽤最⼴泛的还是阿⾥巴巴的Sentinel框架,这⾥我们做下对⽐:

1.3.Sentinel介绍和安装
1.3.1.初识Sentinel
Sentinel是阿⾥巴巴开源的⼀款微服务流量控制组件。官⽹地址: https://sentinelguard.io/zh
cn/index.html
Sentinel 具有以下特征:
- 丰富的应⽤场景:Sentinel 承接了阿⾥巴巴近 10 年的双⼗⼀⼤促流量的核⼼场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填⾕、集群流量控制、实时熔断下游不可⽤应⽤等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接⼊应⽤的单台机器秒级数据,甚⾄ 500 台以下规模的集群的汇总运⾏情况。
- ⼴泛的开源⽣态:Sentinel 提供开箱即⽤的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引⼊相应的依赖并进⾏简单的配置即可快速地接⼊ Sentinel。
- 完善的 SPI 扩展点:Sentinel 提供简单易⽤、完善的 SPI 扩展接⼝。您可以通过实现扩展接⼝来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
1.3.2.安装Sentinel
1)下载
sentinel官⽅提供了UI控制台,⽅便我们对系统做限流设置。⼤家可以在 GitHub 下载。
2)运⾏
将jar包放到任意⾮中⽂⽬录,执⾏命令:java -jar sentinel-dashboard-1.8.1.jar
如果要修改Sentinel的默认端⼝、账户、密码,可以通过下列配置:
例如,修改端⼝:
java -Dserver .port = 8090 -jar sentinel-dashboard-1.8.1.jar
访问http://localhost:8080⻚⾯,就可以看到sentinel的控制台了:
需要输⼊账号和密码,默认都是:sentinel
登录后,发现⼀⽚空⽩,什么都没有:
这是因为我们还没有与微服务整合;懒加载


1.4.微服务整合Sentinel
我们在shop-order中整合sentinel,并连接sentinel的控制台,步骤如下:
1)调整pom
调整后如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--项目基本信息--><groupId>com.apesource</groupId><artifactId>springcloud_alibaba</artifactId><version>1.0-SNAPSHOT</version><!--pom:父文件--><packaging>pom</packaging><!--父项目的子模块--><modules><module>shop_common</module><module>shop_user</module><module>shop_product</module><module>shop_order</module></modules><!--父工程--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.9.RELEASE</version></parent><!--依赖版本的锁定--><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-cloud.version>Hoxton.SR10</spring-cloud.version><spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version></properties><!--dependencyManagement所包含的坐标,子项目不会直接继承,需要声明才可继承--><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>
2)引入sentinel坐标
<!--sentinel-->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
3)配置控制台
修改application.yaml⽂件,添加下⾯内容:
spring:cloud:sentinel:transport:dashboard: localhost:8090

后改后的结果如下:
server:port: 8091
spring:application:name: service-orderdatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername: rootpassword: 123456cloud:nacos:discovery:server-addr: 127.0.0.1:8848web-context-unify: falsesentinel:transport:dashboard: localhost:8090
logging:level:com.apesource: debug
feign:client:config:default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置loggerLevel: FULL # 日志级别httpclient:enabled: true # 开启feign对HttpClient的支持max-connections: 200 # 最大的连接数max-connections-per-route: 50 # 每个路径的最大连接数
4)访问order-service的任意端点
运行order和product,打开浏览器,访问http://localhost:8091/order/prod/21,这样才能触发sentinel的监控。
然后再访问sentinel的控制台,查看效果


2.流量控制
雪崩问题虽然有四种⽅案,但是限流是避免服务因突发的流量⽽发⽣故障,是对微服务雪崩问题的预防。我们先学习这种模式。
2.1.簇点链路
当请求进⼊微服务时,⾸先会访问DispatcherServlet,然后进⼊Controller、Service、Mapper,这样的⼀个调⽤链就叫做簇点链路。簇点链路中被监控的每⼀个接⼝就是⼀个资源。
默认情况下sentinel会监控SpringMVC的每⼀个端点(Endpoint,也就是controller中的⽅法),因此SpringMVC的每⼀个端点(Endpoint)就是调⽤链路中的⼀个资源。

流控、熔断等都是针对簇点链路中的资源来设置的,因此我们可以点击对应资源后⾯的按钮来设置规则:
● 流控:流量控制
● 降级:降级熔断
● 热点:热点参数限流,是限流的⼀种
● 授权:请求的权限控制
2.1.1快速⼊⻔
点击资源/order/prod/{pid}后⾯的流控按钮,就可以弹出表单。

QPS即每秒查询率,QPS = req/sec = 请求数/秒,即每秒的响应请求数,也即是最⼤吞吐能⼒.
2.2.流控模式
在添加限流规则时,点击⾼级选项,可以选择三种流控模式:
● 直接:统计当前资源的请求,触发阈值时对当前资源直接限流,也是默认的模式
● 关联:统计与当前资源相关的另⼀个资源,触发阈值时,对当前资源限流
● 链路:统计从指定链路访问到本资源的请求,触发阈值时,对指定链路限流

快速⼊⻔测试的就是直接模式。
2.2.1.关联模式
关联模式:统计与当前资源相关的另⼀个资源,触发阈值时,对当前资源限流.
配置规则:

语法说明:当/write资源访问量触发阈值时,就会对/read资源限流,避免影响/write资源
使⽤场景:⽐如⽤户⽀付时需要修改订单状态,同时⽤户要查询订单。查询和修改操作会争抢数据库锁,产⽣竞争。业务需求是优先⽀付和更新订单的业务,因此当修改订单业务触发阈值时,需要对查询订单业务限流。
需求说明:
● 在OrderController新建两个端点:/order/query和/order/update,⽆需实现业务
● 配置流控规则,当/order/ update资源被访问的QPS超过5时,对/order/query请求限流
1)定义/order/query端点和/order/update端点,模拟订单查询和订单更新。

2)配置流控规则
对哪个端点限流,就点击哪个端点后⾯的按钮。我们是对订单查询/order/query限流,因此点击它后⾯的按钮:

3)在Jmeter测试
可以看到1000个⽤户,100秒,因此QPS为10,超过了我们设定的阈值:5
请求的⽬标是/order/update,这样这个断点就会触发阈值



