前言
最近在搭建SpringCloudAlibaba相关的组件,今天主要是练习的是Sentinel,可视化界面的仪表盘,进行实时监控,流控、速率控制、服务熔断、服务降级
Sentinel:
- 核心库(java客户端):
不依赖任何框架/库,能够运行于所有java运行时环境,同时对Dubbo/SpringCloud等框架也有较好的支持。 - 控制台(Dashboard):
基于SpringBoot开发,打包后可以直接运行,不需要额外的Tomcat等应用容器。
下载地址:
https://github.com/alibaba/Sentinel/releases
Sentinel控制台
- 运行:
Sentinel监控
首先需要启动我们的服务注册中心—nacos
初始化工程 - 创建新的model工程8401
- 修改POM
<?xml version="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>com.zcw.springcloud2020508</artifactId>
<groupId>com.zcw</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-sentinel-service8401</artifactId>
<dependencies>
<!--SpringCloud alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud alibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud alibaba sentinel-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--SpringBoot 整合web组件+actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--日常通用jar包配置-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
- 创建YML
server:
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: '*'
- 创建启动类
package com.zcw.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @ClassName : SentinelServiceApplication8401
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-27 09:38
*/
@EnableDiscoveryClient
@SpringBootApplication
public class SentinelServiceApplication8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceApplication8401.class,args);
}
}
- 创建业务类
package com.zcw.springcloud.alibaba.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName : FlowLimitController
* @Description :
* @Author : Zhaocunwei
* @Date: 2020-05-27 11:18
*/
@RestController
public class FlowLimitController {
@GetMapping("/testA")
public String testA(){
return "---------testA";
}
@GetMapping("/testB")
public String testB(){
return "---------testB";
}
}
根据上面反映,我们需要调用接口进行测试查看:
通过接口调用,我们下面已经展示成功了,哈哈
流程规则
1.资源名:唯一名称,默认请求路径
2.针对来源:Sentinel可以针对调用者进行限流,填写微服务明,默认default(不区分来源)
3.阈值类型/单机阈值:
- QPS(每秒钟的请求数量):当调用该api的QPS达到阈值的时候,进行限流
- 线程数: 当调用该api的线程数达到阈值的时候,进行限流
4.是否集群:不需要
5.流控模式: - 直接:api达到限流条件时,直接限流
- 关联: 当关联的资源达到阈值时,就限流自己
- 链路: 只记录指定链路上的流量(指定资源从入口资源进来的流量,如果达到阈值,就进行限流)【api级别的针对来源】
6.流控效果: - 快速失败:直接失败,抛异常
- Warm Up:根据codeFactor(冷加载因子,默认3)的值,从阈值/codeFactor,经过预热时长,才达到设置的QPS阈值
- 排队等待:匀速排队,让请求以匀速的速度通过,阈值类型必须设置为QPS,否则失效
Demo 展示
流控
- 流控模式:
- 流控效果:
每次请求睡一下:
流控关联 - 当关联的资源到达阈值时,就限流自己
- 当与A关联的资源B到达阈值后,就限流A自己
- B惹事,A挂了
当关联资源/testB的QPS阈值超过1时,就限流/testA 的Rest访问地址,当关联资源到阈值后限制配置好的资源名
postman模拟并发秘籍访问testB
访问我们的testA发现报错:
流控效果:
- 预热
公式:阈值除以coldFactor(默认值为3),经过预热时长后才会达到阈值
源码:
应用场景:
秒杀系统在开启的瞬间,会有很多流量上来,很有可能把系统打死,预热方式就是把为了保护系统,可慢慢的把流量放进来,慢慢的把阈值增长到设置的阈值。 - 排队等待
- 测试:
降级
-
RT
配置:
压力测试:
-
异常比例
-
异常数
时间窗口一定要大于等于60秒