SpringCloud Alibaba Sentinel实现熔断与限流
服务使用中的各种问题
服务雪崩
服务降级
服务熔断
服务限流
安装Sentinel
sentinel组件由2部分构成
后台
前台8080
运行命令
前提
java8环境OK
8080端口不能被占用
命令
java -jar sentinel-dashboard-1.7.1.jar
访问sentinel管理界面
http://localhost:8080
登录账户密码均为 sentinel
初始化演示工程
启动Nacos8848成功
http://localhost:8848/nacos/#/login
创建Module cloudalibaba-sentinel-service8401
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"> <parent> <artifactId>wms</artifactId> <groupId>com.hk.wms</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>wms-sentinel-service8401</artifactId> <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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <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> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
YMLserver: port: 8401 spring: application: name: wms-sentinal-service cloud: nacos: discovery: #Nacos服务注册中心地址 server-addr: 192.168.1.131:8848 sentinel: transport: #配置Sentin dashboard地址 dashboard: 192.168.1.131:8080 # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口 port: 8719 service-url: nacos-user-service: http://wms-provider config-url: config-service: http://wms-config-3377 management: endpoints: web: exposure: include: '*'
主启动package com.hk.wms.wmssentinelservice8401; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class WmsSentinelService8401Application { public static void main(String[] args) { SpringApplication.run(WmsSentinelService8401Application.class, args); } }
业务类FlowLimitControllerpackage com.hk.wms.wmssentinelservice8401.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class FlowLimitController { @Value("${service-url.nacos-user-service}") String serverUrl ; @Value("${config-url.config-service}") String configUrl; @Autowired RestTemplate template; @GetMapping("/testA") public String testA() { return "----testA"; } @GetMapping("/testB") public String testB() { return "----testB"; } @GetMapping("/testC") public String testC() { return template.getForObject(serverUrl+"/provider/",String.class); } }
启动Sentinel8080
启动微服务8401
空空如也
Sentinel采用的懒加载说明
执行一次访问即可
http://localhost:8401/testA
http://localhost:8401/testB
sentinel8080正在监控微服务8401
流控规则
基本介绍
进一步解释说明
流控模式
直接(默认)
直接-> 快速失败
默认
配置及说明
测试
快速点击多次http://localhost:8401/testA
结果
思考
直接调用默认报错原因,技术方面OK,但是应该要有设置对应的报错信息和界面方便管理
关联
是什么
当关联的资源达到阈值时,就限流自己
当与A关联的资源B达到阈值后,就限流A自己
B惹事,A挂了
配置A
postman模拟并发密集访问testB
postman开启20个线程每隔0.3s请求/testB,导致A挂了
运行后发现testA挂了
链路
多个请求调用同一个微服务
测试
流控效果
直接-> 快速失败(默认的流控处理)
直接失败,抛出异常
Blocked by Sentinel(flowing limit)
源码
预热
说明
公式:阈值除以coldFactor(默认值为3),经过预热时长后才会阈值
官网
默认ColdFactor为3,即请求QPS从threashold/3开始,经预热时长逐渐升到设定的QPS阈值
限流冷启动
源码
com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpController
WarmUp配置
多次点击http://localhost:8401/testB
应用场景
排队等待
匀速排队,严格控制请求通过的间隔时间
官网
测试
postman设置请求发送速率即可
推荐阅读
-
SpringCloud Alibaba Sentinel实现熔断与限流
-
Spring Cloud Alibaba:Sentinel实现熔断与限流
-
SpringCloudAlibaba笔记——Sentinel实现熔断与限流
-
Sentinel实现熔断与限流
-
SpringCloud Alibaba Sentinel实现熔断与限流
-
SpringCloud Alibaba Sentinel实现熔断与限流
-
SpringCloud——熔断与限流 Sentinel
-
Spring Cloud Alibaba(四):Spring Cloud 使用 Sentinel 实现限流
-
Spring Cloud Alibaba之Sentinel实现熔断限流功能
-
SpringCloud-Alibaba-Sentinel服务降级,热点限流,服务熔断