• SpringCloud-Sentinel


    一、介绍

    (1)提供界面配置配置服务限流、服务降级、服务熔断
    (2)@SentinelResource的blockHandler只处理后台配置的异常,运行时异常fallBack处理,且资源名为value时才生效,走兜底方法

    二、安装并启动sentinel

    (1)官网
    (2)运行java -jar sentinel-dashboard-1.8.6.jar
    (3)访问http://localhost:8080/
    在这里插入图片描述
    注:sentinel是懒加载的,访问接口后才会显示

    三、搭建项目

    (1)编写pom.xml

    <?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>demo20220821</artifactId>
            <groupId>com.wsh.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloudalibaba-sentinel-service</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-sentinel</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>com.wsh.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </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.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    (2)编写application.yml

    server:
      port: 8401
    
    spring:
      application:
        name: cloudalibaba-sentinel-service
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
        sentinel:
          transport:
            dashboard: localhost:8080
            port: 8719
    
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    (3)编写启动类

    package com.wsh.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @ClassName SentinelService8401
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/19
     * @Version V1.0
     **/
    @SpringBootApplication
    @EnableDiscoveryClient
    public class SentinelService8401 {
        public static void main(String[] args) {
            SpringApplication.run(SentinelService8401.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    (4)编写Controller

    package com.wsh.springcloud.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @ClassName TestController
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/19
     * @Version V1.0
     **/
    @RestController
    public class TestController {
    
        @GetMapping("/test")
        public String test(){
            return "test";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    四、服务限流

    在这里插入图片描述

    (1)打开界面
    在这里插入图片描述
    (2)阈值
    QPS:每秒请求数
    并发线程数:处理请求的线程数
    在这里插入图片描述
    (3)关联
    在这里插入图片描述

    /test炸了,/test1也炸

    在这里插入图片描述

    (4)预热,阈值先从阈值/3开始,在预热时长(单位:秒)内逐渐上升
    在这里插入图片描述
    (5)排队等待,阈值类型必须设置为QPS,超出阈值的请求将会排队,等待的超时时间设置为20秒
    在这里插入图片描述

    五、服务降级

    (1)满足条件后,服务在规定时间内熔断
    (2)慢调用比例
    在这里插入图片描述
    (3)异常比例
    在这里插入图片描述
    (4)异常数
    在这里插入图片描述

    六、热点参数限流

    (1)要用@SentinelResource定义资源名和兜底方法

        @GetMapping("/test2")
        @SentinelResource(value = "test2", blockHandler = "test2_solve")
        public String test2(@RequestParam("name") String name) {
            log.info("test2");
            return "test2";
        }
        public String test2_solve(String name, BlockException blockException){
            return "block";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (2)
    在这里插入图片描述
    (3)定义规则时指定参数的值
    在这里插入图片描述

    七、兜底方法编写方式优化

    (1)

        @GetMapping("/test3")
        @SentinelResource(value = "test3",  blockHandlerClass = BlockHandler.class, blockHandler = "test3_solve")
        public String test3() {
            log.info("test3");
            return "test3";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (2)编写BlockHandler类

    package com.wsh.springcloud.handler;
    
    import com.alibaba.csp.sentinel.slots.block.BlockException;
    
    /**
     * @ClassName BlockHandler
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/19
     * @Version V1.0
     **/
    public class BlockHandler {
    
        public static String test3_solve(BlockException blockException){
            return "block";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (3)
    在这里插入图片描述

    八、配置fallback、blockHandler

    (1)exceptionsToIgnore 用于忽略异常,不走fallback

        @GetMapping("/test3")
        @SentinelResource(value = "test3", blockHandlerClass = BlockHandler.class, blockHandler = "test3_solve",
        fallbackClass = FallBackHandler.class, fallback = "test3_solve1",
        exceptionsToIgnore = NullPointerException.class)
        public String test3(@RequestParam("name") String name) {
            if (name.equals("wsh")){
                throw new IllegalArgumentException();
            }
            return "test3";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (2)

    public class BlockHandler {
    
        public static String test3_solve(String name, BlockException blockException){
            return "block";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    public class FallBackHandler {
    
        public static String test3_solve1(String name, Throwable throwable){
            return "block1";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    九、配置openFeign

    (1)编写pom.xml

    <?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>demo20220821</artifactId>
            <groupId>com.wsh.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloudalibaba-consumer-order84</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-sentinel</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>com.wsh.springcloud</groupId>
                <artifactId>cloud-api-common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </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.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    (2)编写application.yml

    server:
      port: 84
    
    spring:
      application:
        name: cloudalibaba-consumer-order
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
        sentinel:
          transport:
            dashboard: localhost:8080
            port: 8719
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
    server-url: http://cloudalibaba-provider-payment
    
    feign:
      sentinel:
        enabled: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    (3)编写启动类

    package com.wsh.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    /**
     * @ClassName ConsumerOrder84
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/19
     * @Version V1.0
     **/
    @EnableDiscoveryClient
    @EnableFeignClients
    @SpringBootApplication
    public class ConsumerOrder84 {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerOrder84.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    (4)编写PaymentService

    package com.wsh.springcloud.service;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    /**
     * @ClassName PaymentService
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/19
     * @Version V1.0
     **/
    @FeignClient(value = "cloudalibaba-provider-payment", fallback = PaymentServiceHandler.class)
    public interface PaymentService {
    
        @GetMapping("/payment/test")
        public String test();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (5)编写fallback类

    package com.wsh.springcloud.service;
    
    import org.springframework.stereotype.Component;
    
    /**
     * @ClassName PaymentServiceHandler
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/19
     * @Version V1.0
     **/
    @Component
    public class PaymentServiceHandler implements PaymentService{
        @Override
        public String test() {
            return "fallback";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    (6)编写Controller

    package com.wsh.springcloud.controller;
    
    import com.wsh.springcloud.service.PaymentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @ClassName TestController
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/19
     * @Version V1.0
     **/
    @RestController
    public class TestController {
    
        @Autowired
        private PaymentService paymentService;
    
        @GetMapping("/consumer/test")
        public String test(){
            return paymentService.test();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    (7)编写服务提供者Controller

    package com.wsh.springcloud.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @ClassName TestController
     * @Description: TODO
     * @Author wshaha
     * @Date 2023/10/18
     * @Version V1.0
     **/
    @RestController
    public class TestController {
    
        @Value("${server.port}")
        private String port;
    
        @GetMapping("/payment/test")
        public String test(){
            int i = 1 / 0;
            return "test: " + port;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    (8)运行
    在这里插入图片描述

    十、配置持久化

    (1)将规则持久化到nacos保存,只能先在nacos里编写好才有效
    (2)pom.xml增加依赖

            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-datasource-nacos</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4

    (3)修改application.yml

        sentinel:
          transport:
            dashboard: localhost:8080
            port: 8719
          datasource:
            dsl:
              nacos:
                server-addr: localhost:8848
                dataId: cloudalibaba-sentinel-service
                groupId: DEFAULT_GROUP
                data-type: json
                rule-type: flow
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (4)nacos里创建配置
    在这里插入图片描述

    [
        {
            "resource": "/consumer/test",
            "limitApp": "default",
            "grade": "1",
            "count": 1,
            "strategy": 0,
            "controlBehavior": 0,
            "clusterMode": false
        }
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

  • 相关阅读:
    Elasticsearch的倒排索引是什么?
    禾匠旧版对接微信小程序发货系统(发货信息管理 接口)
    IPSG应用在网络中的位置
    leetcode20. 有效的括号 [简单题]
    mysql基于SSM的自习室管理系统毕业设计源码201524
    水果店圈子:水果店坏水果应该怎么处理,水果店卖剩下的水果如何处理
    Linux提权的几种常用方法
    二分算法(2)
    LeetCode[112]路径总和
    Masked Image Training for Generalizable Deep Image Denoising 论文阅读笔记
  • 原文地址:https://blog.csdn.net/qq_25243147/article/details/133917600