spring-cloud-alibaba.2.2.x Sentinel分佈式系統的流量防衛兵的簡介以及環境搭建

spring-cloud-alibaba.2.2.x Sentinel分佈式系統的流量防衛兵的簡介以及環境搭建

1、Sentinel簡介:分佈式系統的流量防衛兵

詳細見github的文檔https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D

​ 隨着微服務的流行,服務和服務之間的穩定性變得越來越重要。Sentinel 以流量爲切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。

Sentinel 具有以下特徵:

  • 豐富的應用場景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的範圍)、消息削峯填谷、集羣流量控制、實時熔斷下游不可用應用等。
  • 完備的實時監控:Sentinel 同時提供實時的監控功能。您可以在控制檯中看到接入應用的單臺機器秒級數據,甚至 500 臺以下規模的集羣的彙總運行情況。
  • 廣泛的開源生態:Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應的依賴並進行簡單的配置即可快速地接入 Sentinel。
  • 完善的 SPI 擴展點:Sentinel 提供簡單易用、完善的 SPI 擴展接口。您可以通過實現擴展接口來快速地定製邏輯。例如定製規則管理、適配動態數據源等。

Sentinel 的主要特性:

Sentinel 的開源生態:

Sentinel 分爲兩個部分:

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

2、搭建sentinel的控制檯服務

2.1、環境準備

sentinel 依賴 Java 環境來運行。如果您是從代碼開始構建並運行Nacos,還需要爲此配置 Maven環境,請確保是在以下版本環境中安裝使用:

64 bit OS,支持 Linux/Unix/Mac/Windows,推薦選用 Linux/Unix/Mac。
64 bit JDK 1.8+;
Maven 3.2.x+;

通過github的地址下載對應的系統的包https://github.com/alibaba/Sentinel/releases

2.2、啓動服務器

Linux/Unix/Mac

nohup java -jar sentinel-dashboard-1.7.2.jar >/dev/null 2>&1 &

Windows啓動命令

java -jar sentinel-dashboard-1.7.2.jar

通過地址http://localhost:8080/#/login 可以看到正常單頁面,就是成功了;

默認的登錄號/密碼:sentinel/sentinel

3.badger-spring-cloud-alibaba-sentinel sentinel服務的項目搭建

3.1、maven的pom文件如下,就是一個普通的web的springboot項目,加入了sentinel的包

這裏主要演示sentinel的使用,也說明sentinel的使用,可以不依賴其他的外部條件,限流的工作實在客戶端完成的;

上面的sentinel的控制檯,只是做監控的展示,以及配置使用;

下一篇說下,規則數據的持久化配置;持久化配置理解了,就更加容易的理解這兩句話的意思;

<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <groupId>com.badger</groupId>
    <artifactId>badger-spring-cloud-alibaba-sentinel</artifactId>
    <name>badger-spring-cloud-alibaba-sentinel</name>
    <description>服務熔斷、限流</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>
    <dependencies>
        <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>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <!-- 打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>badger-spring-cloud-alibaba-sentinel</finalName>
    </build>
</project>

3.2、yaml配置文件如下,基礎配置

server:
  port: 9000
spring:
  application:
    name: badger-spring-cloud-alibaba-sentinel
  cloud:
    sentinel: 
        transport:
          dashboard: 127.0.0.1:8080
          port: 8719
management:
  endpoints:
    web:
      exposure:
        include: '*'

3.3、主啓動類、以及業務類

@SpringBootApplication
public class SentinelApplication {

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

    @RestController
    public class DemoController {

        @GetMapping(value = "/hello/{name}")
        @SentinelResource(value = "sayHello")
        public String apiHello(@PathVariable String name) {
            return "Hello, " + name;
        }
    }
}

4、項目啓動,測試使用

4.1、項目啓動

sentinel在上面已經啓動,默認端口爲8080;

啓動應用badger-spring-cloud-alibaba-sentinel,端口爲9000

由於sentinel爲懶加載,調用接口http://localhost:9000/hello/abc

簇點鏈路可以看到,應用正常被sentinel監控到。

4.2、添加限流規則

對url爲/hello/{name}做限流處理,點擊流控設置,設置QPS,閾值爲1,表示1秒內,只能有1個請求通過

新增成功後,在流控規則裏,有一條新增的記錄,也可以直接在流控規則的菜單下,新增對應的流控規則;

4.3、測試限流規則

快速多次調用http://localhost:9000/hello/abc

可以看到頁面顯示默認的限流異常信息

Blocked by Sentinel (flow limiting)

說明應用的接口,被限流成功。

4.4、停止應用再啓動

停止應用badger-spring-cloud-alibaba-sentinel後;

再啓動;

再調用對應的url接口,會發現流控規則裏,上次新增的流控規則,沒有了,說明配置的規則數據,沒有持久化;

下一篇說下,規則數據的持久化配置;

詳細見github的文檔[https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D](

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