Sentinel


Sentinel簡介

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

Sentinel 具有以下特征:

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

完善的 SPI 擴展點:Sentinel 提供簡單易用、完善的 SPI 擴展接口。您可以通過實現擴展接口來快
速地定制邏輯。例如定制規則管理、適配動態數據源等。

Sentinel 的主要特性:

  Sentinel 與Hystrix的區別

遷移方案
Sentinel 官方提供了詳細的由Hystrix 遷移到Sentinel 的方法

 名詞解釋

Sentinel 可以簡單的分為 Sentinel 核心庫和 Dashboard。核心庫不依賴 Dashboard,但是結合
Dashboard 可以取得最好的效果。
使用 Sentinel 來進行熔斷保護,主要分為幾個步驟:
1. 定義資源
2. 定義規則
3. 檢驗規則是否生效
資源:可以是任何東西,一個服務,服務里的方法,甚至是一段代碼。
規則:Sentinel 支持以下幾種規則:流量控制規則、熔斷降級規則、系統保護規則、來源訪問控制規則
和 熱點參數規則。Sentinel 的所有規則都可以在內存態中動態地查詢及修改,修改之后立即生效
先把可能需要保護的資源定義好,之后再配置規則。也可以理解為,只要有了資源,我們就可以在任何
時候靈活地定義各種流量控制規則。在編碼的時候,只需要考慮這個代碼是否需要保護,如果需要保
護,就將之定義為一個資源。

Sentinel中的管理控制台
(1)獲取 Sentinel 控制台
您可以從官方 網站中 下載最新版本的控制台 jar 包,下載地址如下:

https://github.com/alibaba/Sentinel/releases/download/1.6.3/sentinel-dashboard-1.6.3.jar

 

(2)啟動
使用如下命令啟動控制台:
其中 - Dserver.port=8888 用於指定 Sentinel 控制台端口為 8888 。
從 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登錄功能,默認用戶名和密碼都是 sentinel 。可以參考 鑒權模塊文檔 配置用戶名和密碼。

[root@192 ~]# java -Dserver.port=8888 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar
INFO: log base dir is: /root/logs/csp/
INFO: log name use pid is: false

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)

2020-02-08 13:07:29.316  INFO 114031 --- [           main] c.a.c.s.dashboard.DashboardApplication   : Starting DashboardApplication on 192.168.180.137 with PID 114031 (/root/sentinel-dashboard-1.6.3.jar started by root in /root)
2020-02-08 13:07:29.319  INFO 114031 --- [           main] c.a.c.s.dashboard.DashboardApplication   : No active profile set, falling back to default profiles: default
2020-02-08 13:07:29.456  INFO 114031 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@59690aa4: startup date [Sat Feb 08 13:07:29 CST 2020]; root of context hierarchy
2020-02-08 13:07:33.783  INFO 114031 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)

啟動 Sentinel 控制台需要 JDK 版本為 1.8 及以上版本。

客戶端能接入控制台
控制台啟動后,客戶端需要按照以下步驟接入到控制台。

父工程引入 alibaba實現的SpringCloud

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>

 

子工程中引入 sentinel

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

 

( 2)配置啟動參數
在工程的application.yml中添加Sentinel 控制台配置信息

spring:
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.180.137:8888   #sentinel控制台的請求地址

 

這里的 spring.cloud.sentinel.transport.dashboard 配置控制台的請求路徑。

查看機器列表以及健康情況
默認情況下Sentinel 會在客戶端首次調用的時候進行初始化,開始向控制台發送心跳包。也可以配置
sentinel.eager=true ,取消Sentinel控制台懶加載。
打開瀏覽器即可展示Sentinel的管理控制台

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM