Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】
文章目錄
- 一、拉模式架構(gòu)
- 二、原理簡(jiǎn)述
- 三、編寫(xiě)
- 3.1 加依賴(lài)
- 3.2 寫(xiě)代碼
- 3.3 配置
- 四、優(yōu)缺點(diǎn)分析
- 五、你可能會(huì)有的疑問(wèn)
- 六、參考文檔
- 七、案例測(cè)試
- 7.1. 添加流控規(guī)則
- 7.2. 服務(wù)停止
- 7.3. 重新啟動(dòng)服務(wù)
- 7.4. 調(diào)用接口
- 7.5. 查看流控規(guī)則
本文實(shí)現(xiàn)基于拉模式的Alibaba Sentinel規(guī)則持久化。
一、拉模式架構(gòu)
圖片來(lái)自官方。
引用自 https://github.com/alibaba/Sentinel/wiki/在生產(chǎn)環(huán)境中使用-Sentinel
二、原理簡(jiǎn)述
FileRefreshableDataSource 定時(shí)從指定文件中讀取規(guī)則JSON文件【圖中的本地文件】,如果發(fā)現(xiàn)文件發(fā)生變化,就更新規(guī)則緩存。
FileWritableDataSource 接收控制臺(tái)規(guī)則推送,并根據(jù)配置,修改規(guī)則JSON文件【圖中的本地文件】。
三、編寫(xiě)
修改Spring Cloud Alibaba微服務(wù)。
3.1 加依賴(lài)
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-extension</artifactId> </dependency>3.2 寫(xiě)代碼
package com.itmuch.contentcenter.sentinel;import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler; import com.alibaba.csp.sentinel.datasource.*; import com.alibaba.csp.sentinel.init.InitFunc; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager; import com.alibaba.csp.sentinel.slots.system.SystemRule; import com.alibaba.csp.sentinel.slots.system.SystemRuleManager; import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference;import java.io.File; import java.io.IOException; import java.util.List;/*** 拉模式規(guī)則持久化** @author itmuch.com*/ public class FileDataSourceInit implements InitFunc {@Overridepublic void init() throws Exception {// TIPS: 如果你對(duì)這個(gè)路徑不喜歡,可修改為你喜歡的路徑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);// 流控規(guī)則ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(flowRulePath,flowRuleListParser);// 將可讀數(shù)據(jù)源注冊(cè)至FlowRuleManager// 這樣當(dāng)規(guī)則文件發(fā)生變化時(shí),就會(huì)更新規(guī)則到內(nèi)存FlowRuleManager.register2Property(flowRuleRDS.getProperty());WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(flowRulePath,this::encodeJson);// 將可寫(xiě)數(shù)據(jù)源注冊(cè)至transport模塊的WritableDataSourceRegistry中// 這樣收到控制臺(tái)推送的規(guī)則時(shí),Sentinel會(huì)先更新到內(nèi)存,然后將規(guī)則寫(xiě)入到文件中WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);// 降級(jí)規(guī)則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);// 系統(tǒng)規(guī)則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);// 授權(quán)規(guī)則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);// 熱點(diǎn)參數(shù)規(guī)則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);} }3.3 配置
在項(xiàng)目的 resources/META-INF/services 目錄下創(chuàng)建文件,名為 com.alibaba.csp.sentinel.init.InitFunc ,內(nèi)容為:
# 改成上面FileDataSourceInit的包名類(lèi)名全路徑即可。 com.itmuch.contentcenter.sentinel.FileDataSourceInit四、優(yōu)缺點(diǎn)分析
優(yōu)點(diǎn):
簡(jiǎn)單易懂
沒(méi)有多余依賴(lài)(比如配置中心、緩存等)
缺點(diǎn):
由于規(guī)則是用 FileRefreshableDataSource 定時(shí)更新的,所以規(guī)則更新會(huì)有延遲。如果FileRefreshableDataSource定時(shí)時(shí)間過(guò)大,可能長(zhǎng)時(shí)間延遲;如果FileRefreshableDataSource過(guò)小,又會(huì)影響性能;
規(guī)則存儲(chǔ)在本地文件,如果有一天需要遷移微服務(wù),那么需要把規(guī)則文件一起遷移,否則規(guī)則會(huì)丟失。
五、你可能會(huì)有的疑問(wèn)
Spring Cloud Alibaba不是提供了如下配置了嗎?為什么要全部自己寫(xiě)呢?
spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json spring.cloud.sentinel.datasource.ds1.file.rule-type=flow#spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json #spring.cloud.sentinel.datasource.ds1.file.data-type=custom #spring.cloud.sentinel.datasource.ds1.file.converter-class=com.alibaba.cloud.examples.JsonFlowRuleListConverter #spring.cloud.sentinel.datasource.ds1.file.rule-type=flow關(guān)于這個(gè)問(wèn)題,可以參考提的Issuehttps://github.com/alibaba/spring-cloud-alibaba/issues/756):
六、參考文檔
https://github.com/alibaba/Sentinel/wiki/在生產(chǎn)環(huán)境中使用-Sentinel#pull模式七、案例測(cè)試
7.1. 添加流控規(guī)則
簇點(diǎn)鏈路-流控
7.2. 服務(wù)停止
停止內(nèi)容中心和Sentinel控制臺(tái)
7.3. 重新啟動(dòng)服務(wù)
重新啟動(dòng)Sentinel控制臺(tái)和內(nèi)容中心
7.4. 調(diào)用接口
由于Sentinel控制臺(tái)采用的是懶加載策略,因此,需要請(qǐng)求一次接口,再刷新
http://localhost:8010/shares/17.5. 查看流控規(guī)則
刷新Sentinel控制臺(tái),查看流控規(guī)則
總結(jié)
以上是生活随笔為你收集整理的Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Git免密登陆远程
- 下一篇: 服务器重启后发现docker-compo