【SpringCloudAlibaba】Sentinel監控工具實戰

前言

最近在搭建SpringCloudAlibaba相關的組件,今天主要是練習的是Sentinel,可視化界面的儀表盤,進行實時監控,流控、速率控制、服務熔斷、服務降級


Sentinel:

  • 核心庫(java客戶端):
    不依賴任何框架/庫,能夠運行於所有java運行時環境,同時對Dubbo/SpringCloud等框架也有較好的支持。
  • 控制檯(Dashboard):
    基於SpringBoot開發,打包後可以直接運行,不需要額外的Tomcat等應用容器。

官網:
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_spring_cloud_alibaba_sentinel

下載地址:
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秒
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述
    在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章