Sentinel(十三)之动态规则扩展
轉載自??動態規則擴展
規則
Sentinel 的理念是開發者只需要關注資源的定義,當資源定義成功后可以動態增加各種流控降級規則。Sentinel 提供兩種方式修改規則:
- 通過 API 直接修改 (loadRules)
- 通過?DataSource?適配不同數據源修改
手動通過 API 修改比較直觀,可以通過以下幾個 API 修改不同的規則:
FlowRuleManager.loadRules(List<FlowRule> rules); // 修改流控規則 DegradeRuleManager.loadRules(List<DegradeRule> rules); // 修改降級規則手動修改規則(硬編碼方式)一般僅用于測試和演示,生產上一般通過動態規則源的方式來動態管理規則。
DataSource 擴展
上述?loadRules()?方法只接受內存態的規則對象,但更多時候規則存儲在文件、數據庫或者配置中心當中。DataSource?接口給我們提供了對接任意配置源的能力。相比直接通過 API 修改規則,實現?DataSource?接口是更加可靠的做法。
我們推薦通過控制臺設置規則后將規則推送到統一的規則中心,客戶端實現?ReadableDataSource?接口端監聽規則中心實時獲取變更,流程如下:
DataSource?擴展常見的實現方式有:
- 拉模式:客戶端主動向某個規則管理中心定期輪詢拉取規則,這個規則中心可以是 RDBMS、文件,甚至是 VCS 等。這樣做的方式是簡單,缺點是無法及時獲取變更;
- 推模式:規則中心統一推送,客戶端通過注冊監聽器的方式時刻監聽變化,比如使用?Nacos、Zookeeper 等配置中心。這種方式有更好的實時性和一致性保證。
Sentinel 目前支持以下數據源擴展:
- Pull-based: 動態文件數據源、Consul,?Eureka
- Push-based:?ZooKeeper,?Redis,?Nacos,?Apollo,?etcd
拉模式拓展
實現拉模式的數據源最簡單的方式是繼承?AutoRefreshDataSource?抽象類,然后實現?readSource()?方法,在該方法里從指定數據源讀取字符串格式的配置數據。比如?基于文件的數據源。
推模式拓展
實現推模式的數據源最簡單的方式是繼承?AbstractDataSource?抽象類,在其構造方法中添加監聽器,并實現?readSource()?從指定數據源讀取字符串格式的配置數據。比如?基于 Nacos 的數據源。
控制臺通常需要做一些改造來直接推送應用維度的規則到配置中心。功能示例可以參考?AHAS Sentinel 控制臺的規則推送功能。改造指南可以參考?在生產環境中使用 Sentinel 控制臺。
注冊數據源
通常需要調用以下方法將數據源注冊至指定的規則管理器中:
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId, parser); FlowRuleManager.register2Property(flowRuleDataSource.getProperty());若不希望手動注冊數據源,可以借助 Sentinel 的?InitFunc?SPI 擴展接口。只需要實現自己的?InitFunc?接口,在?init?方法中編寫注冊數據源的邏輯。比如:
package com.test.init;public class DataSourceInitFunc implements InitFunc {@Overridepublic void init() throws Exception {final String remoteAddress = "localhost";final String groupId = "Sentinel:Demo";final String dataId = "com.alibaba.csp.sentinel.demo.flow.rule";ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));FlowRuleManager.register2Property(flowRuleDataSource.getProperty());} }接著將對應的類名添加到位于資源目錄(通常是?resource?目錄)下的?META-INF/services?目錄下的?com.alibaba.csp.sentinel.init.InitFunc?文件中,比如:
com.test.init.DataSourceInitFunc這樣,當初次訪問任意資源的時候,Sentinel 就可以自動去注冊對應的數據源了。
示例
API 模式:使用客戶端規則 API 配置規則
Sentinel Dashboard?通過客戶端自帶的規則 API來實時查詢和更改內存中的規則。
注意: 要使客戶端具備規則 API,需在客戶端引入以下依賴:
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>x.y.z</version> </dependency>拉模式:使用文件配置規則
這個示例展示 Sentinel 是如何從文件獲取規則信息的。FileRefreshableDataSource?會周期性的讀取文件以獲取規則,當文件有更新時會及時發現,并將規則更新到內存中。使用時只需添加以下依賴:
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-extension</artifactId><version>x.y.z</version> </dependency>推模式:使用 Nacos 配置規則
Nacos?是阿里中間件團隊開源的服務發現和動態配置中心。Sentinel 針對 Nacos 作了適配,底層可以采用 Nacos 作為規則配置數據源。使用時只需添加以下依賴:
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId><version>x.y.z</version> </dependency>然后創建?NacosDataSource?并將其注冊至對應的 RuleManager 上即可。比如:
// remoteAddress 代表 Nacos 服務端的地址 // groupId 和 dataId 對應 Nacos 中相應配置 ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty());詳細示例可以參見?sentinel-demo-nacos-datasource。
推模式:使用 ZooKeeper 配置規則
Sentinel 針對 ZooKeeper 作了相應適配,底層可以采用 ZooKeeper 作為規則配置數據源。使用時只需添加以下依賴:
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-zookeeper</artifactId><version>x.y.z</version> </dependency>然后創建?ZookeeperDataSource?并將其注冊至對應的 RuleManager 上即可。比如:
// remoteAddress 代表 ZooKeeper 服務端的地址 // path 對應 ZK 中的數據路徑 ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty());詳細示例可以參見?sentinel-demo-zookeeper-datasource。
推模式:使用 Apollo 配置規則
Sentinel 針對?Apollo?作了相應適配,底層可以采用 Apollo 作為規則配置數據源。使用時只需添加以下依賴:
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-apollo</artifactId><version>x.y.z</version> </dependency>然后創建?ApolloDataSource?并將其注冊至對應的 RuleManager 上即可。比如:
// namespaceName 對應 Apollo 的命名空間名稱 // ruleKey 對應規則存儲的 key // defaultRules 對應連接不上 Apollo 時的默認規則 ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ApolloDataSource<>(namespaceName, ruleKey, defaultRules, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})); FlowRuleManager.register2Property(flowRuleDataSource.getProperty());詳細示例可以參見?sentinel-demo-apollo-datasource。
推模式:使用 Redis 配置規則
Sentinel 針對 Redis 作了相應適配,底層可以采用 Redis 作為規則配置數據源。使用時只需添加以下依賴:
<!-- 僅支持 JDK 1.8+ --> <dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-redis</artifactId><version>x.y.z</version> </dependency>Redis 動態配置源采用 Redis PUB-SUB 機制實現,詳細文檔參考:https://github.com/alibaba/Sentinel/tree/master/sentinel-extension/sentinel-datasource-redis
總結
以上是生活随笔為你收集整理的Sentinel(十三)之动态规则扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么给电脑硬盘设置密码如何加密电脑硬盘
- 下一篇: Sentinel(十五)之在生产环境中使