Sentinel實現熔斷與限流

Spring Alibaba Sentinel 簡介

隨着微服務的流行,服務和服務之間的穩定性變得越來越重要。在大規模微服務架構的場景下,避免服務出現雪崩,要減少停機時間,要儘可能的提高服務可用性。限流和降級是一個非常重要的手段,具體實施方法可以歸納爲八字箴言,分別是限流,降級,熔斷和隔離。Sentinel 以流量爲切入點,從流量控制熔斷降級、系統負載保護等多個維度保護服務的穩定性。

官網地址https://github.com/alibaba/Sentinel

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

Sentinel 的主要特性:
在這裏插入圖片描述
Sentinel 目前已經針對 Servlet、Dubbo、Spring Boot/Spring Cloud、gRPC 等進行了適配,用戶只需引入相應依賴並進行簡單配置即可非常方便地享受 Sentinel 的高可用流量防護能力。

Sentinel與Hystrix的區別

詳情參考https://yq.aliyun.com/articles/633786/
下面簡單介紹:

  1. Hystrix常用的線程池隔離會造成線程上下切換的overhead比較大;
  2. Hystrix使用的信號量隔離對某個資源調用的併發數進行控制,效果不錯,但是無法對慢調用進行自動降級;
  3. Sentinel通過併發線程數的流量控制提供信號量隔離的功能;
  4. Sentinel支持的熔斷降級維度更多,可對多種指標進行流控、熔斷,且提供了實時監控和控制面板,功能更爲強大。

下載安裝Sentinel

Sentinel 分爲兩個部分組成

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

地址:https://github.com/alibaba/Sentinel/releases/tag/1.7.2

在這裏插入圖片描述

運行
  1. java -jar sentinel-dashboard-1.7.2.jar

  2. 運行如圖
    在這裏插入圖片描述

  3. 瀏覽器訪問控制檯 : http://localhost:8080

  4. 登錄賬號密碼都是 sentinel

  5. 如圖
    在這裏插入圖片描述

  6. 登錄成功後暫時沒有任何數據
    在這裏插入圖片描述

開始整合Sentinel

  1. 創建SpringBoot模塊, 引入一個 web 模塊即可
    在這裏插入圖片描述
  2. pom.xml文件中引入
<!--  引入sentinel相關的依賴-->
<dependency>
   <groupId>com.alibaba.csp</groupId>
   <artifactId>sentinel-datasource-nacos</artifactId>
 </dependency>
 <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 </dependency>

 <!-- nacos註冊中心客戶端依賴 -->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   <version>0.2.2.RELEASE</version>
 </dependency>
 
 <!-- 引入feign的依賴 -->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
   <version>2.2.2.RELEASE</version>
 </dependency>
 
 <!-- 引入 hystrix 依賴 -->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
   <version>2.2.1.RELEASE</version>
 </dependency>
 
 <!-- SpringCloud的依賴 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--spring cloud alibaba 2.1.0.RELEASE-->
      <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.1.0.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  1. yml文件中的配置
spring:
  application:
    name: nacos-consumer-user

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848   #Nacos服務註冊中心地址
    sentinel:
      transport:
        port: 8719  # 默認是 8716
        dashboard: localhost:8080   #配置Sentinel dashboard地址
      datasource:
        ds1:
          nacos:
            server-addr: localhost:8848
            dataId: nacos-consumer-user-sentinel
            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

server:
  port: 9000

feign:
  hystrix:
    enabled: true   # 啓用支持 hystrix
  sentinel:
    enabled: true   # 激活Sentinel對Feign的支持

  1. 再次出現就可以查看到請求的數據
    在這裏插入圖片描述
    在這裏插入圖片描述
  2. 添加流控規則 訪問 http://localhost:9000/user?id=1 每秒請求數量超過1次的請求都會被sentinel直接快速返回失敗
    在這裏插入圖片描述
    以上就是對 Sentinel 熔斷及數據流監控的整合了

感謝閱讀, 如有什麼更好的建議或方法 ,可以留言或進羣交流:1101584918

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