SpringCloud-Alibaba之Sentinel熔断与限流


一、下载安装运行

http://localhost:8080进行访问

登录账号和密码均为sentinel

二、创建工程,并注册到nacos服务中心

  1. 依赖spring-cloud-starter-alibaba-nacos-discovery,spring-cloud-starter-alibaba-sentinel
    sentine-datasource-nacos (持久化)
  2. 配置文件
yml 复制代码
server:
	port: 8401
spring:
	application:
		name: cloudalibaba-sentinel-service
	cloud:
		nacos:
			discovery:
				server-addr: localhost:8848
		sentinel:
			transport:
				dashboard: localhost:8080 # 8080监控8401
				port: 8719 # 假如被占用会自动从8719开始依此+1扫描,直至找到未被占用的端口
management:
	endpoints:
		web:
			exposure:
				include: '*'
  1. 启动类
java 复制代码
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401{
	
	public static void main(String[] args){
		SpringApplication.run(MainApp8401.class,args);
	}
}
  1. 业务类
java 复制代码
@RestController
public class FlowlimitController{
	
	@GetMapping("/testA")
	public String testA(){
		return "------testA";
	}
	@GetMapping("/testB")
	public String testB(){
		return "-------testB";
	}
}

三、启动sentinel java -jar sentinel-dashboard-1.7.0.jar 启动微服务8401,查看sentinel控制台

Sentinel流控规则

QPS:每秒钟请求数量达到阈值进行限流,连续多次刷新请求即可。

线程数:调用该请求的线程数达到阈值进行限流,再代码中添加thread.sleep(3),然后多次刷新请求即可模拟出来

关联资源,当关联资源/testB的QPS阈值超过1,就限流/testA的Rest访问地址

利用postman进行模拟

Sentinel流控效果

严格控制请求通过的间隔时间,就是让请求匀速通过,对应的是漏桶算法。

降级规则


RT:平均响应时间,秒级。

  • 平均响应时间超出阈值且在时间窗口内通过的请求>=5,两个条件同时满足触发降级
  • 窗口期过后关闭断路器
  • RT最大4900(更大需要通过-Dcsp.sentinel.statistic.max.rt=XXXX才能生效)
java 复制代码
@GetMapping("/testD")
public String testD(){
	try{
		TimeUnit.SECONDS.sleep(1);
	}catch(InterruptedException e){
		e.printStackTrace();
	}

	log.info("testD测试RT");
	return "testD"
}

异常比例:秒级

  • QPS>=5且异常比例(秒级统计)超过阈值触发降级,时间窗口结束,关闭降级
java 复制代码
@GetMapping("/testD")
public String testD(){
	
	log.info("testD异常比例");
	int age = 10/0;
	return "testD"
}

异常数:分钟级

  • 异常数(分钟统计)超过阈值时,触发降级,时间窗口结束后,关闭降级
java 复制代码
@GetMapping("/testE")
public String testE(){
	log.info("testE测试异常数");
	int age = 10/0;
	return "testE 测试异常数";
}

热点Key限流

商品ID为参数,统计一段时间内最常购买的商品ID并进行限制

用户ID为参数,统计一段时间内频繁访问的用户ID进行限制

java 复制代码
@GetMapping("/testHotKey")
@sentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
public String testHotKey(@RequeestParam(value = "p1",required = false) String p1,
						 @RequeestParam(value = "p2",required = false) String p2){
	return "--------testE 测试异常数";
}

public String deal_testHotKey(String p1,String p2,BlockException exception){
	return "--------deal_testHotKey 测试异常数";
}

对第0个参数p1,进行阈值限定。

参数例外项,对参数指定的值进行阈值设定,如下所示:当参数为5,限流阈值改为200

系统规则

自定义限流处理逻辑

java 复制代码
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class,
					blockHandler = "handlerException2") //指定处理限流的类以及方法
public CommonResult customerBlockHandler(){
	return new CommonResult(200,"按客户自定义",new Payment(2020L,"serial002"));
}
java 复制代码
/**
	另外创建一个handler包
	
	创建CustomerBlockHandler类自定义限流处理逻辑
	可以定义多个方法
*/
public class CustomerBlockHandler{

	public static CommonResult handlerException(BlockException exception){
		return new CommonResult(444,"按客户自定义自定义处理",new Payment(2020L,"serial002"));
	}

	public static CommonResult handlerException2(BlockException exception){
		return new CommonResult(444,"按客户自定义自定义处理",new Payment(2020L,"serial002"));
	}
}

@SentinelResource注解的属性

Controller中

java 复制代码
@SentinelResource(value = "fallback",fallback = "handlerFallback") //只负责业务异常


@SentinelResource(value = "fallback",blockHandler = "blockHandler") //赋值sentinel控制台的设置出现异常

若blockHandler和fallback都进行了配置,则被限流降级而抛出BlockException时只会进入blockHandler处理逻辑

远程调用接口@FeignClinet进行Sentinel组合

Sentinel规则持久化

将限流配置规则持久化到Nacos保存,只要刷新rest地址,sentinel控制台就能显示出流控规则。

一、工程中引入依赖

sentinel-datasource-nacos

二、配置文件中

三、nacos中添加配置列表,添加json配置规则

四、sentinel控制台就可以查到流控规则

相关推荐
CoderYanger1 小时前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第七弹):图书管理系统(强制登录)
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
陈如水21 小时前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring
行百里er1 天前
【Spring Insight 技术分享】如何给 AI 接口调用做滑动窗口限流
spring boot·后端·spring cloud
Resean02231 天前
Spring 源码分析(六)循环依赖与三级缓存
java·spring·缓存
行百里er2 天前
【Spring Insight 技术分享】跨服务调用链路是怎么串起来的?
后端·spring·开源
Forget the Dream2 天前
Spring AI 2.0 系列(三):ChatClient 与 ChatModel:Spring AI 2.0 的两层职责
java·人工智能·spring·springai
皮皮林5512 天前
为什么越来越多人用 Spring AI ?
spring
Resean02233 天前
Spring 源码分析(七)AOP 原理(上):代理的创建
java·后端·spring
Tangq一定可以的3 天前
Spring AI Advisor 详解 - 给大模型调用加上“切面“,日志/过滤/记忆一键搞定
java·后端·spring
杨凯凡3 天前
Spring Framework 7 原生重试与并发限制实战:@Retryable、@ConcurrencyLimit 能替代 Resilience4j 吗?
数据库·spring·sqlserver