spring-cloud-alibaba-3.1-Sentinel-入門

Sentinel: 分佈式系統的流量防衛兵

git地址: https://github.com/alibaba/sentinel

中文文檔: https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
官網: https://sentinelguard.io/zh-cn/

Sentinel:面向雲原生微服務的高可用流控防護組件。

Sentinel 是什麼?

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

Sentinel 具有以下特徵:

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

在這裏插入圖片描述

特性

在這裏插入圖片描述

下載安裝運行Dashboard

下載地址: sentinel-dashboard-1.7.1.jar

啓動命令: java -jar xxx.jar

訪問: http://vm1:8080

用戶名密碼: sentinel/sentinel/

服務集成sentinel- 本地Demo

maven依賴

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>x.y.2z</version>
</dependency>

測試用例

public class SimpleSentinelMainTest {


    public static void main(String[] args) throws InterruptedException {
        // 配置規則.
        initFlowRules();
        while (true) {
            // 1.5.0 版本開始可以直接利用 try-with-resources 特性
            try (Entry entry = SphU.entry("HelloWorld")) {
                // 被保護的邏輯
                System.out.println("hello world");

//                TimeUnit.MILLISECONDS.sleep(40L);
            } catch (BlockException ex) {
                // 處理被流控的邏輯
                System.out.println("blocked!");
            }
        }
    }


    private static void initFlowRules(){
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule();
        rule.setResource("HelloWorld");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        // Set limit QPS to 20.
        rule.setCount(20);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

springcloud集成

pom.xml

  <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.alibaba.cloud</groupId>
          <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
      </dependency>


      <!-- sentinel 使用nacos持久化 ———— 後續使用  -->
      <dependency>
          <groupId>com.alibaba.csp</groupId>
          <artifactId>sentinel-datasource-nacos</artifactId>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>

bootstrap.yaml

server:
  port: 8401
spring:
  application:
    name: sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: vm1:8848
      config:
        server-addr: vm1:8848
        ## 指定配置文件的格式
        file-extension: yaml
    sentinel:
      transport:
        dashboard: vm1:8080
        ## 從8719依次+1掃描,直到找到未被佔用端口爲之
        port: 8719
        ## sentinel 無法通過下面的preferred-networks來指定具體的ip,這裏手動指定。
        clientIp: 192.168.1.102
	
	### 多網卡,指定網卡
    inetutils:
      preferred-networks: 192.168.1.

management:
  endpoint:
    web:
      exposure:
        include: "*"

主類添加@EnableDiscoveryClient

應用添加@SentinelResource

@RestController
public class FlowLimitController {

    /**
     * 默認會將 @RequestMapping 的url作爲資源註冊
     */
    @RequestMapping("/testA")
    public String testA() {
        return "---- testA";
    }

    @RequestMapping("/testB")
    @SentinelResource(value = "testB")
    public String testB() {
        return "---- testB";
    }
}

默認會將 @RequestMapping 的url作爲資源註冊
Sentinel 支持通過 @SentinelResource註解定義資源並配置 blockHandlerfallback函數來進行限流之後的處理。

請求服務接口/testA
必須在請求完一個接口之後,才能在sentinel dashboard上顯示服務。

配置流控規則

在這裏插入圖片描述
設置/testA 接口,QPS閾值爲3

高頻次訪問/testA
不停的訪問 /testA接口,當QPS超過3時,會出現下列錯誤信息。
在這裏插入圖片描述

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