Sentinel 控制台 需要具備下面幾個特性:
- 規則管理及推送,集中管理和推送規則。sentinel-core 提供 API 和擴展接口來接收信息。開發者需要根據自己的環境,選取一個可靠的推送規則方式;同時,規則最好在控制台中集中管理。
- 監控,支持可靠、快速的實時監控和歷史監控數據查詢。sentinel-core 記錄秒級的資源運行情況,並且提供 API 來拉取資源運行信息。當機器大於一台以上的時候,可以通過 Dashboard 來拉取,聚合,並且存儲這些信息。這個時候,Dashboard 需要有一個存儲媒介,來存儲歷史運行情況。
- 鑒權,區分用戶角色,來進行操作。生產環境下的權限控制是非常重要的,理論上只有管理員等高級用戶才有權限去修改應用的規則。
對於規則管理及推送,規則的推送有三種模式:
原始模式:API 將規則推送至客戶端並直接更新到內存中,擴展寫數據源(WritableDataSource)
這種模式簡單,無任何依賴。但是不保證一致性;規則保存在內存中,重啟即消失。所以不適用與生產環境,我們需要實現規則的持久話。Sentinel給我們提供了兩種持久化方法,就是規則的另外兩種推送模式。
Pull 模式:擴展寫數據源(WritableDataSource), 客戶端主動向某個規則管理中心定期輪詢拉取規則,這個規則中心可以是 RDBMS、文件 等。優點是簡單,無任何依賴;規則持久化。缺點是不保證一致性;實時性不保證,拉取過於頻繁也可能會有性能問題。
Push 模式:擴展讀數據源(ReadableDataSource),規則中心統一推送,客戶端通過注冊監聽器的方式時刻監聽變化,比如使用 Nacos、Zookeeper 等配置中心。這種方式有更好的實時性和一致性保證。生產環境下一般采用 push 模式的數據源。優點是規則持久化;一致性;快速。缺點是引入了第三方依賴。
Pull 模式【拉模式】
Pull模式架構圖
- FileRefreshableDataSource 定時從指定文件中讀取規則JSON文件【圖中的本地文件】,如果發現文件發生變化,就更新規則緩存。
- FileWritableDataSource 接收控制台規則推送,並根據配置,修改規則JSON文件【圖中的本地文件】。
實現方式
添加依賴
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-extension</artifactId>
</dependency>
持久化代碼
/**
* PULL規則持久化
*/
public class FileDataSourceInit implements InitFunc {
@Override
public void init() throws Exception {
String ruleDir = System.getProperty("user.home") + "/sentinel/rules";
String flowRulePath = ruleDir + "/flow-rule.json";
String degradeRulePath = ruleDir + "/degrade-rule.json";
String systemRulePath = ruleDir + "/system-rule.json";
String authorityRulePath = ruleDir + "/authority-rule.json";
String paramFlowRulePath = ruleDir + "/param-flow-rule.json";
this.mkdirIfNotExits(ruleDir);
this.createFileIfNotExits(flowRulePath);
this.createFileIfNotExits(degradeRulePath);
this.createFileIfNotExits(systemRulePath);
this.createFileIfNotExits(authorityRulePath);
this.createFileIfNotExits(paramFlowRulePath);
// 注冊一個可讀數據源,用來定時讀取本地的json文件,更新到規則緩存中
// 流控規則
ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(
flowRulePath,
flowRuleListParser
);
// 將可讀數據源注冊至FlowRuleManager
// 這樣當規則文件發生變化時,就會更新規則到內存
FlowRuleManager.register2Property(flowRuleRDS.getProperty());
WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(
flowRulePath,
this::encodeJson
);
// 將可寫數據源注冊至transport模塊的WritableDataSourceRegistry中
// 這樣收到控制台推送的規則時,Sentinel會先更新到內存,然后將規則寫入到文件中
WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);
// 降級規則
ReadableDataSource<String, List<DegradeRule>> degradeRuleRDS = new FileRefreshableDataSource<>(
degradeRulePath,
degradeRuleListParser
);
DegradeRuleManager.register2Property(degradeRuleRDS.getProperty());
WritableDataSource<List<DegradeRule>> degradeRuleWDS = new FileWritableDataSource<>(
degradeRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerDegradeDataSource(degradeRuleWDS);
// 系統規則
ReadableDataSource<String, List<SystemRule>> systemRuleRDS = new FileRefreshableDataSource<>(
systemRulePath,
systemRuleListParser
);
SystemRuleManager.register2Property(systemRuleRDS.getProperty());
WritableDataSource<List<SystemRule>> systemRuleWDS = new FileWritableDataSource<>(
systemRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerSystemDataSource(systemRuleWDS);
// 授權規則
ReadableDataSource<String, List<AuthorityRule>> authorityRuleRDS = new FileRefreshableDataSource<>(
authorityRulePath,
authorityRuleListParser
);
AuthorityRuleManager.register2Property(authorityRuleRDS.getProperty());
WritableDataSource<List<AuthorityRule>> authorityRuleWDS = new FileWritableDataSource<>(
authorityRulePath,
this::encodeJson
);
WritableDataSourceRegistry.registerAuthorityDataSource(authorityRuleWDS);
// 熱點參數規則
ReadableDataSource<String, List<ParamFlowRule>> paramFlowRuleRDS = new FileRefreshableDataSource<>(
paramFlowRulePath,
paramFlowRuleListParser
);
ParamFlowRuleManager.register2Property(paramFlowRuleRDS.getProperty());
WritableDataSource<List<ParamFlowRule>> paramFlowRuleWDS = new FileWritableDataSource<>(
paramFlowRulePath,
this::encodeJson
);
ModifyParamFlowRulesCommandHandler.setWritableDataSource(paramFlowRuleWDS);
}
private Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<FlowRule>>() {
}
);
private Converter<String, List<DegradeRule>> degradeRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<DegradeRule>>() {
}
);
private Converter<String, List<SystemRule>> systemRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<SystemRule>>() {
}
);
private Converter<String, List<AuthorityRule>> authorityRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<AuthorityRule>>() {
}
);
private Converter<String, List<ParamFlowRule>> paramFlowRuleListParser = source -> JSON.parseObject(
source,
new TypeReference<List<ParamFlowRule>>() {
}
);
private void mkdirIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
}
private void createFileIfNotExits(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
}
private <T> String encodeJson(T t) {
return JSON.toJSONString(t);
}
}
添加配置
在項目的 resources/META-INF/services 目錄下創建文件,名為 com.alibaba.csp.sentinel.init.InitFunc ,內容為:
上面FileDataSourceInit的包名類名全路徑
Push模式
Push模式架構圖
- 控制台推送規則:
- 將規則推送到Nacos或其他遠程配置中心
- Sentinel客戶端鏈接Nacos,獲取規則配置;並監聽Nacos配置變化,如發生變化,就更新本地緩存(從而讓本地緩存總是和Nacos一致)
- 控制台監聽Nacos配置變化,如發生變化就更新本地緩存(從而讓控制台本地緩存總是和Nacos一致)
微服務改造
添加依賴
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
添加配置
spring:
cloud:
sentinel:
datasource:
# 名稱隨意
flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-flow-rules
groupId: SENTINEL_GROUP
# 規則類型,取值見:
# org.springframework.cloud.alibaba.sentinel.datasource.RuleType
rule-type: flow
degrade:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-degrade-rules
groupId: SENTINEL_GROUP
rule-type: degrade
system:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-system-rules
groupId: SENTINEL_GROUP
rule-type: system
authority:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-authority-rules
groupId: SENTINEL_GROUP
rule-type: authority
param-flow:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-param-flow-rules
groupId: SENTINEL_GROUP
rule-type: param-flow
控制台改造
控制台改造主要是為規則實現,需要修改源碼,從https://github.com/alibaba/Sentinel 地址下載源碼,進入sentinel-dashboard目錄進行修改
DynamicRuleProvider:從Nacos上讀取配置
DynamicRulePublisher:將規則推送到Nacos上
- 修改pom.xml文件【sentinel-dashboard模塊下】
將:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
</dependency>
改為:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
-
找到sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos目錄,將整個目錄拷貝到 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos
-
修改 com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2
將:
@Autowired
@Qualifier("flowRuleDefaultProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
改為:
@Autowired
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
- 修改 sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html
將:
<!--<li ui-sref-active="active" ng-if="entry.appType==0">-->
<!--<a ui-sref="dashboard.flow({app: entry.app})">-->
<!--<i class="glyphicon glyphicon-filter"></i> 流控規則 V1</a>-->
<!--</li>-->
改為:
<li ui-sref-active="active" ng-if="entry.appType==0">
<a ui-sref="dashboard.flow({app: entry.app})">
<i class="glyphicon glyphicon-filter"></i> 流控規則 V1</a>
</li>
自此,流控規則更改為Push持久化,其他規則等照修改
編輯/啟動
- 回到項目主目錄,執行 mvn clean package -DskipTests
- 在項目的 target 目錄找到sentinel-dashboard.jar ,執行 java -jar sentinel-dashboard.jar 啟動控制台。
生產環境使用
推薦使用Push模式
也可以使用阿里雲提供的在線托管Sentinel控制台:AHAS