【SpringCloudAlibaba】Sentinel監控工具@SentinelResource練習,實現自己的兜底策略

前言

本篇博客主要是向大家介紹:@SentinelResource ,它用於定義資源,並提供可選的異常處理和 fallback 配置項。 通過demo演示,爲大家介紹相關的屬性。


按資源名稱限流

  • 啓動Nacos成功
    在這裏插入圖片描述
  • 啓動Sentinel成功
    在這裏插入圖片描述
  • 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>
        <dependency>
            <groupId>com.zcw</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--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>

  • 創建Controller類

package com.zcw.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName : RateLimitController
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 09:36
 */
@RestController
public class RateLimitController {
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handleException")
    public CommonResult byResource(){
        return new CommonResult(200,"按自願名稱限流測試OK",
                new Payment(2020L,"serial001"));
    }
    public CommonResult handleException(BlockException blockException){
        return new CommonResult(444,blockException.getClass().getCanonicalName()+"\t服務不可用");
    }
}


  • 配置流控規則:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

  • 連續點擊時,出現如下圖所示:
    在這裏插入圖片描述
    表示1秒鐘內查詢次數大於1,就跑到我們自定義的處流,限流
  • 關閉8401服務測試一下下:
    在這裏插入圖片描述
    在這裏插入圖片描述
    竟然是臨時的。。。。

按照Url地址限流

通過訪問的URL來限流,會返回Sentinel自帶默認的限流處理信息。
在這裏插入圖片描述

 @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl(){
        return new CommonResult(200,"按url限流測試OK",new Payment(2020L,"serial002"));
    }


在這裏插入圖片描述

  • 修改我們Sentinel控制檯
    在這裏插入圖片描述
    在這裏插入圖片描述
    瘋狂點擊會報如下錯誤:
    在這裏插入圖片描述
  • 竟然沒有返回:如下結果,百思不得其解
    在這裏插入圖片描述

上面兜底方案面臨的問題

1.系統默認的,沒有體現我們自己的業務要求
2.依照現有條件,我們自定義的處理方法又和業務代碼耦合在一塊,不直觀。
3.每個業務方法都添加一個兜底的,那代碼膨脹加劇。
4.全局統一的處理方法沒有體現。

自定義限流處理邏輯

  • 創建CustomerBlockHandler類用於自定義限流處理邏輯
    【自定義限流處理類 CustomerBlockHandler】

package com.zcw.springcloud.alibaba.myhandler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zcw.springcloud.entities.CommonResult;
import com.zcw.springcloud.entities.Payment;

/**
 * @ClassName : CustomerBlockHandler
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-05-28 10:18
 */
public class CustomerBlockHandler {
    public static CommonResult handlerException(BlockException blockException){
        return  new CommonResult(444,"自定義");
    }
}


  • RateLimitController
    在這裏插入圖片描述
 @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException")
    public CommonResult customerBlockHandler(){
        return new CommonResult(200,"自定義限流規則",new Payment(2020L,"serial002"));
    }
  • Sentinel控制檯配置
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 測試
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 進一步說明,我們可以配置多個兜底的方法:
    在這裏插入圖片描述

屬性說明講解

下面是官方地址:
https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
在這裏插入圖片描述
Sentinel主要有三個核心的API

  • SphU定義資源
  • Tracer定義統計
  • ContextUtil定義了上下文
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章