SpringCloud
十九、SpringCloud Alibaba Sentinel实现熔断与限流
简介
官网:https://github.com/alibaba/sentinel
中文版:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
文档:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel
下载
下载:https://github.com/alibaba/Sentinel/releases
普通运行
.jar
文件:直接
java -jar XXX
命令运行即可
docker
#拉取sentinel镜像
docker pull bladex/sentinel-dashboard
#运行sentinel(docker里的sentinel是8858端口)
docker run --name sentinel -d -p 8858:8858 bladex/sentinel-dashboard
#把nacos和mysql也启动起来
在浏览器输入:
http://ip:8858/#/login
(因为linux出了点问题,重装了,所以ip也变了)
账号和密码都是
sentinel
控制台使用说明:https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0
初始化演示工程
- 新建模块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> <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.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-sentinal-service cloud: nacos: discovery: #Nacos服务注册中心地址(改成自己的服务器ip地址,本地用localhost) server-addr: localhost:8848 sentinel: transport: #配置Sentin dashboard地址(改成自己的服务器ip地址,本地用localhost) dashboard: localhost:8080 # 默认8719端口,假如被占用了会自动从8719端口+1进行扫描,直到找到未被占用的 端口 # 8719端口是跟sentinel交互的HTTP client port: 8719 management: endpoints: web: exposure: include: '*'
- 主启动类
@EnableDiscoveryClient @SpringBootApplication public class MainApp8401 { public static void main(String[] args) { SpringApplication.run(MainApp8401.class, args); } }
- controller
@RestController public class FlowLimitController { @GetMapping("/testA") public String testA() { return "----testA"; } @GetMapping("/testB") public String testB() { return "----testB"; } }
-
测试,启动8401,然后刷新sentinel后台页面(因为sentinel采用懒加载策略,所以需要调用服务后才在后台显示)
在浏览器分别输入,然后刷新sentinel后台页面:
http://localhost:8401/testA
http://localhost:8401/testB
流控规则
流量控制
可在流控规则处新建:
也可簇点链路处指定添加:
表示1秒钟内请求一次就OK,若超过次数1,就直接-快速失败,报默认错误
每秒请求数超过1个就会限流。
阈值类型
QPS与线程数的区别
QPS是直接挡在外面,而线程数是有多少个线程在处理,放进来后,有线程是空闲状态就对请求进行处理,都没空闲,就限流(关门打狗)。
流控模式
直接
关联
A去调用B,B如果资源不足了,就限流A。
此时不管调用多少次A都不会限流,而此时超过1秒调用1次B,则会限流A。
使用Postman进行多次访问。
用postman20个线程每0.3秒给B发送请求,然后在去访问A,发现如下图,A被阻塞
链路
在网上搜关于链路的是用下面这个例子,然而显示不了限流的效果。
新建一个TestService
@Service
public class TestService {
@SentinelResource("getTest")
public void getTest(){
System.out.println("getTest...");
}
}
然后在FlowLimitController添加修改:
@Resource
TestService testService;
@GetMapping("/testA")
public String testA() {
testService.getTest();
return "----testA";
}
@GetMapping("/testB")
public String testB() {
testService.getTest();
return "----testB";
}
然后重新启动8401,输入
http://localhost:8401/testA
和
http://localhost:8401/testB
给getTest添加流控链路规则。
流控效果
流控效果只有QPS有,线程数没有。
快速失败
预热(Warm Up)
对testA新增流控规则
一开始的阈值为10/3(默认为3,冷加载因子)=3阈值;用5秒渐渐增加到10的阈值
然后输入
http://localhost:8401/testA
,不停刷新,前5秒会限流,5秒后只要每秒不超过10个请求,就不会限流。
排队等待
让请求以均匀的速度通过,单机阈值为每秒通过数量,其余的排队等待; 它还会让设置一个超时时间,当请求超过超时间时间还未处理,则会被丢弃。
在testB方法中添加
然后重启8401
postman发起的每秒10个请求进行排队,testB每秒处理一个请求。
降级规则
时间窗口:降级多少时间(秒)
降级策略实战
RT
请求数1秒内大于5个,且一个请求处理时间大于设定的值,进入熔断后降级
在FlowLimitController中添加
@GetMapping("/testC")
public String testC(){
//暂停1秒钟线程
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
log.info(Thread.currentThread().getName()+"\t"+"。。。testC,测试 R T");
return "----testC";
}
启动8401,在浏览器输入
http://localhost:8401/testC
,然后在sentinel设置testC降级规则。
请求处理完成的时间为200毫秒(阈值),超过这个时间熔断降级进入时间窗口期不处理请求,1秒后退出时间窗口期,继续处理请求。(前提是一秒请求数超过5个,如果请求数没超过5个,就算请求处理的时间超过阈值也不会熔断降级)
说白了就是,请求处理是否能在200毫秒完成,不能就拉闸一秒
然后用Jmeter进行压力测试。·
说明:
上面我们方法需要1秒钟处理时间;
8401设置/testC,需要在200毫秒内容完成,不然就会1秒钟时间窗口熔断;
上面用Jmeter设置向
http://localhost:8401/testC
循环发送请求1秒钟10个请求
(官方设定1秒内请求大于5个,且对应时刻的平均响应时间均超过阈值)
意思就是:1s>200ms,1秒请求10>官方设定的5,所以↓↓↓
必然完成不了上面对8401RT的设置,所有就跳闸报错了
异常比例
异常比例达到设定的百分比,进入熔断后降级
修改testC
@GetMapping("/testC")
public String testC(){
log.info("testC 异常比例");
int age = 10/0;
return "----testC";
}
启动8401,浏览器输入
http://localhost:8401/testC
,然后在后台设置testD的降级规则。
处理请求出错超过
0.2
(20%,阈值),熔断降级,进入时间窗口期不处理请求,
3秒
后退出时间窗口期继续处理请求。(前提也是每秒请求数超过5个,防止不会熔断降级)
用Jmeter进行测试
异常数
异常大于等于设定的值,进入熔断后降级
因为是按照一分钟内的异常数对阈值进行比较,如果时间窗口小于60秒,可能会再次进入熔断状态。所以时间窗口一定要大于等于60秒。
在FlowLimitController里添加:
@GetMapping("/testE")
public String testE() {
log.info("testE 测试异常数");
int age = 10 / 0;
return "----testE 测试异常数";
}
启动8401,浏览器输入
http://localhost:8401/testE
,然后在后台设置testE的降级规则。
一分钟内,如果访问处理出现异常的次数超过5次,熔断降级,进入时间窗口期不处理请求,61秒后退出时间窗口期继续处理请求。(时间窗口必须大于等于60秒,防止再次熔断降级)
刷新5次,
http://localhost:8401/testE
热点key限流
豪猪歌中的@HystrixCommand和现在的@SentinelResource相似度百分数90%
在FlowLimitController中添加:
@GetMapping("/testHotKey")
@SentinelResource(value ="testHotKey",blockHandler = "deal_testHotKey")
//value值:随意设定,只要名称唯一,id唯一名字
//blockHandler:指定哪个方法作为兜底方法
public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
@RequestParam(value = "p2",required = false)String p2){
return "-------testHotKey";
}
//兜底方法
public String deal_testHotKey(String p1, String p2, BlockException blockException){
return "-------deal_testHotKey";
// sentinel的默认提示都是: Blocked by Sentinel (flow limiting)
}
重启8401,浏览器输入
http://localhost:8401/testHotKey
,然后在后台对testHotKey进行热点规则配置。
如果每秒的访问请求带有
索引为0
的参数的数量超过1,进入统计窗口期,然后调用兜底方法,1秒后退出统计窗口期,继续处理请求。
【指定是方法中参数体内的第一个参数】如果第一个(参数索引开始为0)参数的QPS每秒请求次数大于1,就降级1秒钟,并调用兜底方法。
http://localhost:8401/testHotKey?p1=1
http://localhost:8401/testHotKey?p1=1&p2=2
如果设置热点规则,而@SentinelResource注解里
没有指明blockHandler兜底方法
,就会把直接把错误页面信息打印到前台。
参数例外项
特定配置,设置参数等于某个值,并为其设定阈值,如果超过就限流
高级选项只有在热点规则里有,簇点链路无法配置高级选项。
索引0的参数的值为vip,则其限流阈值为200
http://localhost:8401/testHotKey?p1=1
http://localhost:8401/testHotKey?p1=vip
给testHotKey方法添加
int i = 1 / 0;
异常。
然后重新测试,发现兜底方法不适用于异常,有异常会直接打印到页面。
系统规则
Sentinel 系统自适应限流从
整体维度
对应用入口流量进行控制,结合应用的 Load、CPU 使用率、总体平均 RT、入口 QPS 和并发线程数等几个维度的监控指标,通过自适应的流控策略,让系统的入口流量和系统的负载达到一个平衡,让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。
针对整个系统的,每秒访问量超过1(阈值),限流。
http://localhost:8401/testA
http://localhost:8401/testB
@SentinelResource
按资源名称限流+后续处理
资源名称
在pom添加
<!--换成你们直接的包名-->
<!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.angenin.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
新建RateLimitController:【前面的controller里面方法太多了,重写个,不然晕了】
@RestController
public class RateLimitController {
@GetMapping("/byResource")
@SentinelResource(value = "byResource",blockHandler = "handleException")
public CommonResult byResource() {
return new CommonResult(200,"按照资源名称限流测试",new Payment(2020L,"serial001"));
}
//兜底方法
public CommonResult handleException(BlockException exception) {
return new CommonResult(444,exception.getClass().getCanonicalName() + "\t 服务不可用");
}
}
启动8401
http://localhost:8401/byResource
用资源名称进行配置兜底方法才生效。
多次刷新页面
按照URL地址限流+后续处理
URL地址:
在RateLimitController中添加:
@GetMapping("/rateLimit/byUrl")
@SentinelResource(value = "byUrl") //没有兜底方法,系统就用默认的
public CommonResult byUrl() {
return new CommonResult(200,"按照byUrl限流测试",new Payment(2020L,"serial002"));
}
启动8401
http://localhost:8401/rateLimit/byUrl
多次刷新:【不指定兜底方法,就用系统默认的】
上面兜底方案面临的问题
客户自定义限流处理逻辑
新建myhandler.CustomerBlockHandler自定义限流处理类:
public class CustomerBlockHandler {
public static CommonResult handlerException(BlockException exception) {
return new CommonResult(444,"按照客户自定义限流测试,Glogal handlerException ---- 1");
}
public static CommonResult handlerException2(BlockException exception) {
return new CommonResult(444,"按照客户自定义限流测试,Glogal handlerException ---- 2");
}
}
在RateLimitController中添加:
//CustomerBlockHandler
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
blockHandlerClass = CustomerBlockHandler.class,
blockHandler = "handlerException2")
//blockHandlerClass:指定用哪个类做阻塞类
//blockHandler:指定上面指定类中的哪个方法为阻塞方法
public CommonResult customerBlockHandler() {
return new CommonResult(200,"按照客户自定义限流测试",new Payment(2020L,"serial003"));
}
启动8401
http://localhost:8401/rateLimit/customerBlockHandler
【url配置不会走兜底方法】
用资源名称进行配置兜底方法才生效。
多次刷新:
更多注解属性说明
服务熔断功能
feign 和 ribbon 是 Spring Cloud 的 Netflix 中提供的两个实现软负载均衡的组件,Ribbon 和 Feign 都是用于调用其他服务的,方式不同。Feign 则是在 Ribbon 的基础上进行了一次改进,采用接口的方式,将需要调用的其他服务的方法定义成抽象方法即可,不需要自己构建 http 请求。不过要注意的是抽象方法的注解、方法签名要和提供服务的方法完全一致。
服务降级是出现异常切换到兜底方案不会自动恢复过来,但是熔断则是服务降级,但是过一段时间会慢慢恢复
Ribbon系列
新建提供者
- 新建模块cloudalibaba-provider-payment9003
- pom
<dependencies> <!-- SpringCloud ailibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- 引用自己定义的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.achang.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </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.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: 9003 spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: localhost:8848 #nacos management: endpoints: web: exposure: include: '*'
- 主启动类
@SpringBootApplication @EnableDiscoveryClient //nacos服务注册发现 public class PaymentMain9003 { public static void main(String[] args) { SpringApplication.run(PaymentMain9003.class,args); } }
-
controller
直接map模拟dao层
@RestController public class PaymentController { @Value("${server.port}") //spring的注解 private String serverPort; public static HashMap<Long, Payment> map = new HashMap<>(); static { map.put(1L,new Payment(1L,"1111")); map.put(2L,new Payment(2L,"2222")); map.put(3L,new Payment(3L,"3333")); } @GetMapping(value = "/paymentSQL/{id}") public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) { Payment payment = map.get(id); CommonResult<Payment> result = new CommonResult<>(200,"from mysql,serverPort: " + serverPort,payment); return result; } }
- 按照9003新建9004
新建消费者
- 新建模块cloudalibaba-consumer-nacos-order84
- pom
<dependencies> <!-- SpringCloud ailibaba nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- SpringCloud ailibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- 引用自己定义的api通用包,可以使用Payment支付Entity --> <dependency> <groupId>com.achang.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </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.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: 84 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #nacos sentinel: transport: dashboard: localhost:8080 #sentinel port: 8719 #消费者将去访问的微服务名称 server-url: nacos-user-service: http://nacos-payment-provider #激活Sentinel对Feign的支持 feign: sentinel: enabled: true
- 主启动类
@EnableDiscoveryClient @SpringBootApplication @EnableFeignClients //开启Feign功能 public class OrderMain84 { public static void main(String[] args) { SpringApplication.run(OrderMain84.class,args); } }
- config
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced //Ribbon负载均衡 public RestTemplate getRestTemplate() { return new RestTemplate(); } }
- controller
@RestController @Slf4j public class CircleBreakerController { public static final String SERVICE_URL = "http://nacos-payment-provider"; @Resource private RestTemplate restTemplate; @RequestMapping("/consumer/fallback/{id}") @SentinelResource(value = "fallback") //没有配置 public CommonResult<Payment> fallback(@PathVariable Long id) { CommonResult<Payment> result = restTemplate.getForObject( SERVICE_URL + "/paymentSQL/" + id,CommonResult.class,id); if(id == 4){ throw new IllegalArgumentException("IllegalArgument,非法参数异常..."); }else if(result.getData() == null) { throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常"); } return result; } }
启动9003,9004,84
http://localhost:84/consumer/fallback/1
测试是否可实现ribbon
轮循调用
提供者的服务
http://localhost:84/consumer/fallback/4
http://localhost:84/consumer/fallback/5
只配置fallback
只负责程序业务异常
修改84的CircleBreakerController类的
fallback
方法中的@SentinelResource注解,并在类中添加
@SentinelResource(value = "fallback",fallback ="handlerFallback") //只配置fallback(只负责程序业务异常)
//fallback兜底
public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
Payment payment = new Payment(id,"null");
return new CommonResult(444,"异常handlerFallback,exception内容: " + e.getMessage(), payment);
}
重新运行
http://localhost:84/consumer/fallback/4
http://localhost:84/consumer/fallback/5
只配置blockHandler
只负责sentinel控制台配置违规
修改@SentinelResource注解,并在类中添加
@SentinelResource(value = "fallback", blockHandler = "blockHandler") //只配置blockHandler(只负责sentinel控制台配置违规)
//blockHandler兜底
public CommonResult blockHandler(@PathVariable Long id,BlockException e) {
Payment payment = new Payment(id,"null");
return new CommonResult(444,"blockHandler-sentinel 限流,BlockException: " + e.getMessage(), payment);
}
重启项目
访问
http://localhost:84/consumer/fallback/1
,然后在sentinel后台进行配置。
http://localhost:84/consumer/fallback/5
前两次会出现如下
第三次就走我们规定的blockHandler的兜底方法
因为没配置指定fallback兜底方法,所以会直接显示错误页面,配置了blockHandler兜底方法,所以当sentinel配置违规会执行blockHandler兜底方法。
配置fallback和blockHandler
修改@SentinelResource注解
重启项目,输入
http://localhost:84/consumer/fallback/1
,然后到后台配置。
http://localhost:84/consumer/fallback/1
多次刷新执行blockHandler兜底方法。
http://localhost:84/consumer/fallback/5
执行fallback兜底方法。
http://localhost:84/consumer/fallback/5
多次刷新执行blockHandler兜底方法。
当@SentinelResource注解fallback和blockHandler都指定后,然后同时符合,
优先执行blockHandler兜底方法
。
忽略属性
修改@SentinelResource注解:
@SentinelResource(value = "fallback",
fallback ="handlerFallback",
blockHandler = "blockHandler",
exceptionsToIgnore = {IllegalArgumentException.class})
//如果出现exceptionsToIgnore中的异常,不运行fallback兜底方法。
http://localhost:84/consumer/fallback/4
id为4时,报的是IllegalArgumentException,非法参数异常
原本应该由fallback指定的兜底方法处理,但是因为配置了exceptionsToIgnore
所以就fallback就不处理了
http://localhost:84/consumer/fallback/5
Feign系列
通过一个接口,标注@FeignClient注解用fallback指定兜底类,value指定要在注册中心找哪个提供者的服务;接口的方法要与提供者提供的服务参数一致
修改84
- pom
<!--前面已添加了--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
- yml
#前面也已经添加了 #激活Sentinel对Feign的支持 feign: sentinel: enabled: true
- 主启动类上添加
(也已经添加了)@EnableFeignClients
@EnableDiscoveryClient @SpringBootApplication @EnableFeignClients //开启Feign功能 public class OrderMain84 { public static void main(String[] args) { SpringApplication.run(OrderMain84.class,args); } }
-
service
PaymentService接口
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class) public interface PaymentService { @GetMapping(value = "/paymentSQL/{id}") public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id); }
PaymentFallbackService实现类
上面fallback规定的兜底类
@Component //组件加入ioc容器,不要忘记了 public class PaymentFallbackService implements PaymentService{ @Override public CommonResult<Payment> paymentSQL(Long id) { return new CommonResult<>(444,"服务降级返回,---PaymentFallbackService",new Payment(id,"ErrorSerial")); } }
-
controller
CircleBreakerController中添加:
//======= OpenFeign @Resource private PaymentService paymentService; @GetMapping(value = "/consumer/paymentSQL/{id}") public CommonResult< Payment > paymentSQL(@PathVariable("id") Long id){ return paymentService.paymentSQL(id); }
启动项目,
http://localhost:84/consumer/paymentSQL/1
关闭9003和9004,执行@FeignClient的fallback兜底方法PaymentFallbackService。
熔断限流框架对比
规则持久化
半成品持久化,需要手动配置到nacos
修改8401
- pom
<!--之前添加了--> <!-- SpringCloud ailibaba sentinel-datasource-nacos 持久化需要用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
- yml
spring: application:.. cloud:.. nacos:.. sentinel:.. transport:.. datasource: ds1: nacos: server-addr: localhost:8848 #nacos dataId: ${spring.application.name} groupId: DEFAULT_GROUP data-type: json rule-type: flow feign: sentinel: enabled: true #激活Sentinel 对Feign的支持
-
在nacos后台添加配置:
与sentinel上的流控配置相对应
看在数据库是否有↑↑此配置信息, 是有的↓↓ 启动8401[ { "resource": "/rateLimit/byUrl", "limitApp": "default", "grade": 1, "count": 1, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ]
多次刷新http://localhost:8401/rateLimit/byUrl
停止8401
再查看sentinel后台,发下之前配置的流控规则消失
再次启动8401,因为sentinel是懒加载模式,所以需要先访问
,然后查看sentinel后台,发现出现了。 多次刷新http://localhost:8401/rateLimit/byUrl
实现sentinel配置的持久化。http://localhost:8401/rateLimit/byUrl