1. 关联概念
官方解释:当关联的资源达到阈值时,就限流当前资源
通俗解释来说,比如那我们的程序,现在有testA接口和testB接口,当testA关联的资源testB达到阈值后,就限流testA。例如在一个电商系统中,当支付系统达到阈值,就限流下单系统,防止更多的订单产生的支付打垮了支付系统。
2. 具体演示
资源入库代码
当关联资源/testB的QPS阈值超时1时,就限流/testA,代码如下:
package com.example.sentinel.service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FlowLimitController {
@GetMapping("/testA")
public String testA(){
return "-----testA";
}
@GetMapping("/testB")
public String testB(){
//返回js再次请求,保证QPS超过1,用于演示效果
return "-----testB<script>setInterval(function(){window.location.reload();},500)</script>";
}
}
sentinel dashboard 设置
3. 效果演示
经历以上两个步骤后,就可以启动项目来测试了。
但是,我们要让资源/testB 的QPS保持超过1 ,这样我们访问资源/testA才会被流控
为了演示这种效果,我在testB资源返回时返回了演示500毫秒再次请求的js,用于保证/testB 的QPS保持超过1
setInterval(function(){
window.location.reload();
},500);
这个时候我们来看网页中testA接口的效果,就可以看到/testA的关联资源被流控了