Sentinel簡介
隨着微服務的普及,服務調用的穩定性變得越來越重要。Sentinel以“流量”為突破口,在流量控制、斷路、負載保護等多個領域進行工作,保障服務可靠性。
通俗來講:用來在微服務系統中保護微服務對的作用 如何 服務雪崩 服務熔斷 服務降級 就是用來替換hystrix
特性
-
豐富的應用場景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的范圍)、消息削峰填谷、集群流量控制、實時熔斷下游不可用應用等。
-
完備的實時監控:Sentinel 同時提供實時的監控功能。您可以在控制台中看到接入應用的單台機器秒級數據,甚至 500 台以下規模的集群的匯總運行情況。
-
廣泛的開源生態:Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應的依賴並進行簡單的配置即可快速地接入 Sentinel。
sentinel提供了兩個服務組件:
一個是 sentinel 用來實現微服務系統中服務熔斷、降級等功能。 這點和hystrix 類似
一個是 sentinel dashboard 用來監控微服務系統中流量調用等情況 流控 熔斷 降級 配置。 這點和hystrix dashboard類似
Sentinel的構建
1、下載Sentinel https://github.com/alibaba/Sentinel/
2、上傳到linux服務器
3、啟動
java -Dserver.port=9191 -jar sentinel-dashboard-1.7.2.jar
4、訪問9191端口的web管理頁面
5、輸入賬號密碼為sentinel進行登陸
Sentinel的使用
1、停止nacos集群
2、啟動nacos單機模式
./nacos/bin/startup.sh -m standalone
3、新建一個子模塊springcloudAlibaba-sentinel-8998
4、添加相關依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos client-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--引入sentinel依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
5、編寫配置文件
server.port=8998
spring.application.name=SENTINEL
#nacos server地址
spring.cloud.nacos.server-addr=localhost:8848
#開啟sentinel保護
spring.cloud.sentinel.enabled=true
#指定sentinel dashboard web管理地址
spring.cloud.sentinel.transport.dashboard=localhost:9191
#指定sentinel組件與sentinel dashboard通信地址
#dashboard端口號默認8719
spring.cloud.sentinel.transport.port=8719
6、編寫controller
package com.study.springcloudAlibaba.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@RequestMapping("/demo")
public String demo(){
return "demo ok!!!";
}
}
7、啟動項目,調用一次/demo,再去觀察sentinel
Sentinel中對系統保護的五大規則
一、流控規則:流量控制(flow control
- 其原理是監控應用流量的 QPS 或並發線程數等指標,當達到指定的閾值時對流量進行控制,以避免被瞬時的流量高峰沖垮,從而保障應用的高可用性。
二、降級規則:熔斷降級(Degrade Service)
- 其原理是監控應用中的資源調用請求,達到指定閥值時自動出發熔斷降級
三、熱點規則:熱點參數規則(ParamFlow)
- 其原理是很多時候我們希望統計某個熱點數據中訪問頻次最高的 TOP K 數據,並對其進行限制
四、系統規則:系統自適應限流(SysetmFlow)
- 其原理是 Sentinel 系統自適應限流從整體維度對應用入口流量進行控制
五、授權規則:來源訪問控制(黑白名單)規則(AuthorityRule)
- 很多時候,我們需要根據調用來源來判斷次該請求是否允許放行,這時候可以使用 Sentinel 的來源訪問控制(黑白名單控制)的功能。來源訪問控制根據資源的請求(origin)限制資源是否通過,若配置白名單則只有請求來源白名單內時才可通過;若配置黑名單則請求所在黑名單時不通過,其余的請求通過。