上一篇:SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(10)-- Linux版Nacos+MySQL生产环境配置

一、Sentinel

1、官网

https://github.com/alibaba/Sentinel
中文:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D

2、是什么

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring

3、去哪下

https://github.com/alibaba/Sentinel/releases
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_02

4、能干嘛

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_03

5、怎么玩

https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_04

二、安装Sentinel控制台

sentinel组件由2部分组成
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_05
后台+前台8080
安装步骤:

1、下载

https://github.com/alibaba/Sentinel/releases
下载到本地sentinel-dashboard-1.7.0.jar
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_06

2、运行命令

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_07

java -jar sentinel-dashboard-1.7.0.jar 

3、访问sentinel管理界面

http://localhost:8080
登录账号密码均为sentinel

三、初始化演示工程

1、启动Nacos8848成功

http://localhost:8848/nacos/#/login

2、Module

①、cloudalibaba-sentinel-service8401
②、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>cloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <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>

</project>
③、YML
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: '*'
④、主启动
package com.atguigu.springcloud.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401
{
    public static void main(String[] args) {
        SpringApplication.run(MainApp8401.class, args);
    }
}
⑤、业务类FlowLimitController
package com.atguigu.springcloud.alibaba.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @GetMapping("/testB")
    public String testB() {

        return "------testB";
    }



}

3、启动Sentinel8080

java -jar sentinel-dashboard-1.7.0

4、启动微服务8401

5、启动8401微服务后查看sentienl控制台

①、空空如也,啥都没有
②、Sentinel采用的懒加载说明

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_08
效果
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_09

③、结论

sentinel8080正在监控微服务8401

四、流控规则

1、基本介绍

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_10
进一步解释说明
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_11
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_12

2、流控模式

①、直接(默认)
1)、直接->快速失败(系统默认)
2)、配置及说明、

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_13

3)、测试

快速点击访问http://localhost:8401/testA
结果:Blocked by Sentinel (flow limiting)
思考???-> 直接调用默认报错信息,技术方面OK but,是否应该有我们自己的后续处理?类似有一个fallback的兜底方法?

②、关联
1)、是什么?

当关联的资源达到阈值时,就限流自己
当与A关联的资源B达到阈值后,就限流自己
B惹事,A挂了

2)、配置A

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_14

3)、postman模拟并发密集访问testB

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_15
访问testB成功
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_16
postman里新建多线程集合组
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_17
将访问地址添加进新线程组
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_18
Run:大批量线程高并发访问B,导致A失效了

4)、运行后发现testA挂了

点击访问http://localhost:8401/testA
Blocked by Sentinel (flow limiting)

③、链路

多个请求调用了同一个微服务

3、流控效果

①、直接->快速失败(默认的流控处理)

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_19

②、预热
1)、说明

公式:阈值除以coldFactor(默认值为3),经过预热时长后才会达到阈值

2)、官网

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_20
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_21
默认coldFactor为3,即请求QPS从threshold/3开始,经预热时长逐渐升至设定的QPS阈值。
限流 冷启动:
https://github.com/alibaba/Sentinel/wiki/%E9%99%90%E6%B5%81—%E5%86%B7%E5%90%AF%E5%8A%A8

3)、源码

com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpController
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_22

4)、Warmup配置

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_23

5)、多次点击http://localhost:8401/testB

刚开始不行,后续慢慢OK

6)、应用场景

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_24

③、排队等待

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_25
匀速排队,阈值必须设置为QPS
官网
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_26
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_27
源码:com.alibaba.csp.sentinel.slots.block.flow.controller.RateLimiterController
测试:
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_28

五、降级规则

1、基本介绍

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_29
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_30
进一步说明
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_31
Sentinel的断路器是没有半开状态的

2、降级策略实战

①、RT

是什么
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_32
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_33
测试:
代码

    @GetMapping("/testD")
    public String testD()
    {
        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        log.info("testD 测试RT");

        return "------testD";
    }

配置
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_34
jmeter压测
结论:
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_35
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_36

②、异常比例

是什么
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_37
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_38
测试
代码

@GetMapping("/testD")
    public String testD()
    {

        log.info("testD 测试RT");
        int age = 10/0;
        return "------testD";
    }

配置
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_39
jmeter
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_40
结论
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_41

③、异常数

是什么
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_42
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_43
异常数是按照分钟统计的
测试
代码:

@GetMapping("/testE")
public String testE()
{
    log.info("testE 测试异常数");
    int age = 10/0;
    return "------testE 测试异常数";
}

配置:http://localhost:8401/testE
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_44
jmeter

六、热点key限流

1、基本介绍

是什么
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_45

2、官网

https://github.com/alibaba/Sentinel/wiki/热点参数限流

3、承上启下复习start

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_46
@SentinelResource

4、代码

@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";  
}

com.alibaba.csp.sentinel.slots.block.BlockException

5、配置

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_47
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_48

6、测试

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_限流_49

7、参数例外项

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_50
配置:
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_51

8、其他

手贱添加异常看看…
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_52

七、系统规则

1、是什么

https://github.com/alibaba/Sentinel/wiki/%E7%B3%BB%E7%BB%9F%E8%87%AA%E9%80%82%E5%BA%94%E9%99%90%E6%B5%81

2、各项配置参数说明

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_maven_53

3、配置全局QPS

八、@SentinelResource

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(12)-- SpringCloud Alibaba Sentinel @SentinelResource

九、服务熔断功能

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(13)-- SpringCloud Alibaba Sentinel 服务熔断功能

十、规则持久化

1、是什么

一旦我们重启应用,Sentinel规则将消失,生产环境需要将配置规则进行持久化

2、怎么玩

将限流配置规则持久化进Nacos保存,只要刷新8401某个rest地址,sentinel控制台的流控规则就能看到,只要Nacos里面的配置不删除,针对8401上Sentinel上的流控规则持续有效

3、步骤

①、修改cloudalibaba-sentinel-service8401
②、POM
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
③、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
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: cloudalibaba-sentinel-service
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

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

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

添加Nacos数据源配置

spring:
   cloud:
    sentinel:
    datasource:
     ds1:
      nacos:
        server-addr:localhost:8848
        dataid:${spring.application.name}
        groupid:DEFAULT_GROUP
        data-type:json
            rule-type:flow
④、添加Nacos业务规则配置

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_54
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_github_55
内容解析

[
    {
         "resource": "/retaLimit/byUrl",
         "limitApp": "default",
         "grade":   1,
         "count":   1,
         "strategy": 0,
         "controlBehavior": 0,
         "clusterMode": false    
    }
]
⑤、启动8401后刷新sentinel发现业务规则有了

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_56

⑥、快速访问测试接口

http://localhost:8401/rateLimit/byUrl
SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_官网_57

⑦、停止8401再看sentinel

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_58

⑧、重新启动8401再看sentinel

SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(11)-- SpringCloud Alibaba Sentinel实现熔断与限流_spring_59

下一篇:SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(12)-- SpringCloud Alibaba Sentinel @SentinelResource