前提启动nacos,Sentinel成功
- "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>cloud2020artifactId>
- <groupId>com.atguigu.springcloudgroupId>
- <version>1.0-SNAPSHOTversion>
- parent>
- <modelVersion>4.0.0modelVersion>
-
- <artifactId>cloudalibaba-sentinel-service8401artifactId>
-
- <properties>
- <maven.compiler.source>8maven.compiler.source>
- <maven.compiler.target>8maven.compiler.target>
- properties>
- <dependencies>
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
- dependency>
-
-
- <dependency>
- <groupId>com.alibaba.cloudgroupId>
- <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-openfeignartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-actuatorartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
- project>
- rver:
- port: 8401
-
- spring:
- application:
- name: cloudalibaba-sentinel-service
- cloud:
- nacos:
- discovery:
- #Nacos服务注册中心地址
- server-addr: localhost:8848
- sentinel:
- transport:
- #配置Sentinel dashboard地址
- dashboard: localhost:8080
- #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
- port: 8719
-
- management:
- endpoints:
- web:
- exposure:
- include: '*'
- sentinel:
- enabled: true
- @EnableDiscoveryClient
- @SpringBootApplication
- public class MainApp8401
- {
- public static void main(String[] args) {
- SpringApplication.run(MainApp8401.class, args);
- }
- }
- @RestController
- public class FlowLimitController
- {
-
- @GetMapping("/testA")
- public String testA()
- {
- return "------testA";
- }
-
- @GetMapping("/testB")
- public String testB()
- {
- return "------testB";
- }
- }
测试1
基本介绍
直接->快速失败
配置及说明
表示1秒钟内查询1次就是OK,若超过次数1,就直接-快速失败,报默认错误
测试2
快速点击访问http://localhost:8401/testA
设置效果
当关联资源/testB的qps阀值超过1时,就限流/testA的Rest访问地址,当关联资源到阈值后限制配置好的资源名
测试3
postman发送请求http://localhost:8401/testB
postman里新建多线程集合组
将访问地址添加进新新线程组
run
大批量线程高并发访问B,导致A失效了
点击访问http://localhost:8401/testA
运行后发现testA挂了
结果:
Blocked by Sentinel (flow limiting)
多个请求调用了同一个微服务
直接失败,抛出异常
Blocked by Sentinel (flow limiting)
源码
com.alibaba.csp.sentinel.slots.block.flow.controller.DefaultController
说明:公式:阈值除以coldFactor(默认值为3),经过预热时长后才会达到阈值
官网
默认coldFactor为3,即请求 QPS 从 threshold / 3 开始,经预热时长逐渐升至设定的 QPS 阈值。
默认 coldFactor 为 3,即请求QPS从(threshold / 3) 开始,经多少预热时长才逐渐升至设定的 QPS 阈值。
测试4
案例,阀值为10+预热时长设置5秒。
系统初始化的阀值为10 / 3 约等于3,即阀值刚开始为3;然后过了5秒后阀值才慢慢升高恢复到10多次点击http://localhost:8401/testB
刚开始不行,后续慢慢OK
应用场景
如:秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死,预热方式就是把为了保护系统,可慢慢的把流量放进来,慢慢的把阀值增长到设置的阀值。
匀速排队,让请求以均匀的速度通过,阀值类型必须设成QPS,否则无效。
设置含义:/testA每秒1次请求,超过的话就排队等待,等待的超时时间为20000毫秒。
官网
https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6
源码
com.alibaba.csp.sentinel.slots.block.flow.controller.RateLimiterController