1.简介
下载地址
服务雪崩,服务降级,服务熔断,服务限流
2.安装sentinel控制台
sentinel分为两个部分:
核心库:不依赖任何框架,能够运行所有java运行环境,同时对Dubbo/Spring Cloud等框架有较好的支持
控制台:基于springboot开发,打包之后直接运行,不需要tomcat
下载之后是一个jar包,直接使用java -jar sentinel-dashboard-1.7.0.jar 运行
3.初始化演示
新建cloudalibaba-sentinel-service8401
<dependencies>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<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>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</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>
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080
port: 8719 #默认8719,假如被占用了会自动从8719开始依次+1扫描。直至找到未被占用的端口
management:
endpoints:
web:
exposure:
include: '*'
@RestController
public class FlowLimitController
{
@GetMapping("/testA")
public String testA() {
return "------testA";
}
@GetMapping("/testB")
public String testB() {
return "------testB";
}
}
启动之后sentinel没有东西,因为sentinel启用的是懒加载,必须访问服务之后才能显示
访问服务testa,b之后
4.流控规则
配置流控规则-QPS
超过每秒一次直接失败 ,同理线程数超过每秒一个失败
配置流控规则关联
当关联的资源b达到阈值之后,限制a。
流控效果
预热加载,刚开始10s内QPS值是5(三分之一),10s之后会变成15
排队等待:每秒通过15个,匀速通过,大量的请求过来也不会直接失效。排队时长为500ms
降级规则
@GetMapping("/testD")
public String testD()
{
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
log.info("testD 测试RT");
return "------testD";
}
RT数目
异常比例数
异常数
5. 热点key限流-通过资源名称配置
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
//int age = 10/0;
return "------testHotKey";
}
//兜底方法
public String deal_testHotKey (String p1, String p2, BlockException exception){
return "------deal_testHotKey,o(╥﹏╥)o";
}
代表第一个参数单击阈值为1,错误会返回兜底方法
配置参数例外项
添加第一个参数的参数例外项,这样就可以使第一个参数等于5的时候限流值很大,适用于搜索方法的常用搜索,热搜。
@SentinelResource这个注解不管异常,出现异常走异常
通过url地址限流
如果使用url地址来限流,会返回sentinel自带的处理信息,而不是自己创建的兜底方法
将兜底方法单独放在一个地方
public class customerBlockHandler {
public static CommonResult handle1(BlockException e){
return new CommonResult(2020,"自定义信息一");
}
public static CommonResult handle2(BlockException e){
return new CommonResult(2020,"自定义信息一");
}
}
@GetMapping("/byResource")
@SentinelResource(value = "byResource",blockHandlerClass = customerBlockHandler.class ,blockHandler = "handle1")
public CommonResult byResource() {
return new CommonResult(200, "按资源名称限流测试OK", new Payment(2020L, "serial001"));
}
通过注解选定类里面的方法兜底
6.规则持久化
以上设置的流控跪着,热点规则等都是不持久的,关闭服务之后全部没了
因此必须要将规则存放到nacos才行
修改cloudalibaba-sentinel-service8401
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
spring:
cloud:
sentinel:
datasource:
ds1:
nacos:
server-addr:localhost:8848
dataid:${spring.application.name}
groupid:DEFAULT_GROUP
data-type:json
rule-type:flow
通过yml配置寻找nacos上的配置项
通过这个设置流控规则,这样就可以将规则保留了