15. SpringCloud Alibaba Sentinel实现熔断与限流
15.1 概述
15.1.1 官网
英文: https://github.com/alibaba/Sentinel
中文: https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
15.1.2 介绍
一句话解释,之前我们讲解过的Hystrix
15.1.3 去哪下
https://github.com/alibaba/Sentinel/releases
15.1.4 能干嘛
15.1.5 怎么玩
官网:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel
服务使用中的各种问题:
- 服务雪崩
- 服务降级
- 服务熔断
- 服务限流
15.2 安装Sentinel控制台
15.2.1 sentinel组件由2部分构成
15.2.2 安装步骤
1.下载
https://github.com/alibaba/Sentinel/releases
2.运行命令
前提:
- java8环境OK
- 8080端口不能被占用,sentinel内部有Tomcat…
启动:
java -jar sentinel-dashboard-1.7.0.jar
如果端口被占用可以: java -Dserver.port=8888 -jar sentinel-dashboard-1.7.0.jar
3.访问sentinel管理界面
访问: http://localhost:端口号 ,登录账号密码均为sentinel
15.3 初始化演示工程
15.3.1 前提条件
启动Nacos,访问http://192.168.174.128:8848/nacos/#/login 访问成功
15.3.2 建立sentinel8041
建Module—cloudalibaba-sentinel-service8401
POM
<dependencies> <!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <!--SpringCloud ailibaba sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- SpringBoot整合Web组件+actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--日常通用jar包配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </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> </dependency> </dependencies>
- YML
server: port: 8401 spring: application: name: cloudalibaba-sentinel-service cloud: nacos: discovery: server-addr: 192.168.174.128:8848 #Nacos服务注册中心地址 sentinel: transport: dashboard: localhost:8080 #配置Sentinel dashboard地址 port: 8719 #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口 management: endpoints: web: exposure: include: '*'
- 主启动
@EnableDiscoveryClient @SpringBootApplication public class MainApp8401 { public static void main(String[] args) { SpringApplication.run(MainApp8401.class, args); } }
- 业务类FlowLimitController
@RestController public class FlowLimitController { @GetMapping("/testA") public String testA() { return "------testA"; } @GetMapping("/testB") public String testB() { return "------testB"; } }
15.3.3 测试结果
- 启动Sentinel8888
- 启动微服务8401
- 启动8401微服务,查看sentienl控制台
空空如也,啥都没有
原因在于:Sentinel采用的懒加载
执行一次访问即可: 访问http://localhost:8401/testA 和http://localhost:8401/testB
结论:sentinel8080正在监控微服务8401
15.4 流控规则
15.4.1 基本介绍
进一步说明
QPS和线程数的区别
QPS:御敌于国门之外,即一次只能有QPS个请求数.
线程数:进来之后关门打狗,一次只能有线程数个任务被处理,多余的不进行处理…
15.4.2 流控模式
1.直接(默认)
直接->快速失败
- 配置及说明:
表示1秒钟内查询1次就是OK,若超过次数1,就直接-快速失败,报默认错误
测试:
快速点击访问http://localhost:8401/testA
结果:Blocked by Sentinel (flow limiting)
思考
直接调用默认报错信息,技术方面OK。 but,是否应该有我们自己的后续处理?类似有个fallback的兜底方法?
2.关联
是什么
当关联的资源达到阈值时,就限流自己
当与A关联的资源B达到阀值后,就限流A自己
B惹事,A挂了,如:支付接口达到阈值限制下订单的接口
配置A
当关联资源/testB的qps阀值超过1时,就限流/testA的Rest访问地址,当关联资源到阈值后限制配置好的资源名
postman模拟并发密集访问testB
正常访问testB成功
postman里新建多线程集合组
将访问地址添加进新新线程组
RUN
- 点击访问http://localhost:8401/testA ,进行测试
结果:由于大批量线程高并发访问B,发现testA挂了
3.链路.
多个请求调用了同一个微服务…