20、Sentinel流控

系列文章目录

1、父工程创建
2、支付模块构建和热部署
3、消费者订单模块
4、服务注册中心-Eureka
5、zookeeper没学习
6、服务注册中心-Consul
7、Eureka、Consul异同
8、服务调用-Ribbon
9、服务调用-OpenFeign

10、服务降级-Hystrix
11、服务降级-Hystrix(二)
12、服务熔断-Hystrix
13、服务网关-Gateway
14-17 在git上做配置中心,没有学习
17、请求链路跟踪-Sleuth
18、Spring Cloud Alibaba-Nacos注册中心与配置中心
19、Spring Cloud Alibaba-Nacos集群和持久化配置
20、Sentinel流控
21、Sentinel熔断降级、热点key限流
22、SentinelResource配置
23、Sentinel 服务熔断与持久化


1. 简介

1.1 是什么

微服务的流量控制、熔断组件。保护微服务。

1.2 Hystrix与Sentinel对比

Hystrix不足:
需要自己搭建监控平台(例如9001端口的)
没有Web页面可以进行操作

Sentinel:
单独一个组件,可以独立出来
直接界面化,可以细粒度配置

1.3 Sentinel下载

我下载的版本为 1.8.2 不同的版本在一些特性上可能有些不一样
下载地址

官方文档

1.4 Sentinel 构成

分为两部分

  1. 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
  2. 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。

1.5 Sentinel 运行

前提条件:

  1. java 8 环境
  2. 8080端口没有被占用
java -jar sentinel-dashboard-1.8.2.jar
  1. 访问 http://localhost:8080/ 看到sentinel界面就是成功
  2. 默认账户和密码都是sentinel

2. Sentinel初始化监控

2.1 新建工程

新建模块cloudalibaba-sentinel-service8401

2.2 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>cloud2021</artifactId>
        <groupId>com.chzu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-sentinel-service8401</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- commons -->
        <dependency>
            <groupId>com.chzu</groupId>
            <artifactId>cloud-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--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>
        <!--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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>


</project>

2.3 YML

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
    sentinel:
      transport:
        dashboard: localhost:8080 #配置Sentinel dashboard地址
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: '*'

feign:
  sentinel:
    enabled: true # 激活Sentinel对Feign的支持


2.4 主启动类

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelMain8401 {

    public static void main(String[] args) {
        SpringApplication.run(SentinelMain8401.class,args);
    }
}

2.5 业务类

Controller

@RestController
@Slf4j
public class SentinelController {

    @GetMapping("/testA")
    public String testA()
    {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB()
    {
        log.info(Thread.currentThread().getName()+"\t"+"...testB");
        return "------testB";
    }
}

2.5 测试

  1. 启动nacos sentinel
  2. 启动微服务
  3. 访问http://localhost:8080/(这时页面上什么也没有,需要发一次请求才会出现如下页面)
  4. 在这里插入图片描述

3. Sentinel流控规则简介

3.1 简单介绍

在这里插入图片描述

  1. 资源名:唯一名称,默认请求路径。
  2. 针对来源:Sentinel可以针对调用者进行限流,填写微服务名,默认default(不区分来源)。
  3. 阈值类型/单机阈值:
    • QPS(每秒钟的请求数量)︰当调用该API的QPS达到阈值的时候,进行限流。
    • 线程数:当调用该API的线程数达到阈值的时候,进行限流。
  4. 是否集群:不需要集群。
  5. 流控模式:
    • 直接:API达到限流条件时,直接限流。
    • 关联:当关联的资源达到阈值时,就限流自己。
    • 链路:只记录指定链路上的流量(指定资源从入口资源进来的流量,如果达到阈值,就进行限流)【API级别的针对来源】。
  6. 流控效果:
    • 快速失败:直接失败,抛异常。
    • Warm up:根据Code Factor(冷加载因子,默认3)的值,从阈值/codeFactor,经过预热时长,才达到设置的QPS阈值。
    • 排队等待:匀速排队,让请求以匀速的速度通过,阈值类型必须设置为QPS,否则无效。

3.2 实际操作

3.2.1 直接失败

  1. QPS直接失败
    配置详解:
    1秒钟内查询1次会成功,若在一秒钟访问次数超过1,就直接->快速失败,报默认错误
    在这里插入图片描述
    效果:
    快速点击访问http://localhost:8401/testA
    出现Blocked by Sentinel (flow limiting)

思考:
现在是直接调用默认报错信息,我们能不能自己做后续处理工作,类似fallback

  1. 线程直接快速失败
    线程数:当调用该API的线程数达到阈值的时候,进行限流。
    类比服务人员与顾客,此时只有一个服务人员,只能服务一个人。现在尽管来了很多的顾客,但是也只能服务一个,其他的顾客不被服务。
    在这里插入图片描述

3.2.2 关联

当关联的资源达到阈值时,就限流自己

我们设置资源关联 如 资源A与资源B关联, 当资源B达到阈值时,资源A被限流
举例:当支付接口达到阈值时,限流下订单的接口

设置testA
当关联的资源/testB的QPS阀值超过1时,就限流/testA的访问在这里插入图片描述
Postman测试
新建一个collection
在这里插入图片描述
右击新建的collection选择【Add Request】,填入url,点击右上角的【save】
在这里插入图片描述
点击collection,点击【Run】
在这里插入图片描述
在页面中填入参数,参数解释: 20个线程,访问间隔为0.3秒
在这里插入图片描述

在运行的同时访问testA,返回Blocked by Sentinel (flow limiting)

3.2.3 预热(Warm up)

默认coldFactor为3,即请求QPS 从 threshold / 3开始,经预热时长逐渐升至设定的QPS阈值 官方说明
案例说明:
系统初始化的阈值为10/ 3约等于3,即阈值刚开始为3;然后过了5秒后阈值才慢慢升高恢复到10
在这里插入图片描述

测试
快速点击http://localhost:8401/testB
结果:刚开始会出现Blocked by Sentinel (flow limiting),后续不会出现。

3.2.4 排队等待

严格控制请求通过的时间,请求以均匀的速度通过,阀值类型必须设成QPS,否则无效。

设置:/testA每秒1次请求,超过的话就排队等待,等待的超时时间为20000毫秒。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值