日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】

發(fā)布時間:2024/9/27 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

          • 一、拉模式架構
          • 二、原理簡述
          • 三、編寫
            • 3.1 加依賴
            • 3.2 寫代碼
            • 3.3 配置
          • 四、優(yōu)缺點分析
          • 五、你可能會有的疑問
          • 六、參考文檔
          • 七、案例測試
            • 7.1. 添加流控規(guī)則
            • 7.2. 服務停止
            • 7.3. 重新啟動服務
            • 7.4. 調用接口
            • 7.5. 查看流控規(guī)則

本文實現(xiàn)基于拉模式的Alibaba Sentinel規(guī)則持久化。

一、拉模式架構

圖片來自官方。
引用自 https://github.com/alibaba/Sentinel/wiki/在生產環(huán)境中使用-Sentinel

二、原理簡述

FileRefreshableDataSource 定時從指定文件中讀取規(guī)則JSON文件【圖中的本地文件】,如果發(fā)現(xiàn)文件發(fā)生變化,就更新規(guī)則緩存。
FileWritableDataSource 接收控制臺規(guī)則推送,并根據配置,修改規(guī)則JSON文件【圖中的本地文件】。

三、編寫

修改Spring Cloud Alibaba微服務。

3.1 加依賴
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-extension</artifactId> </dependency>
3.2 寫代碼
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: 如果你對這個路徑不喜歡,可修改為你喜歡的路徑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);// 將可讀數據源注冊至FlowRuleManager// 這樣當規(guī)則文件發(fā)生變化時,就會更新規(guī)則到內存FlowRuleManager.register2Property(flowRuleRDS.getProperty());WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(flowRulePath,this::encodeJson);// 將可寫數據源注冊至transport模塊的WritableDataSourceRegistry中// 這樣收到控制臺推送的規(guī)則時,Sentinel會先更新到內存,然后將規(guī)則寫入到文件中WritableDataSourceRegistry.registerFlowDataSource(flowRuleWDS);// 降級規(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);// 授權規(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);// 熱點參數規(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 配置

在項目的 resources/META-INF/services 目錄下創(chuàng)建文件,名為 com.alibaba.csp.sentinel.init.InitFunc ,內容為:

# 改成上面FileDataSourceInit的包名類名全路徑即可。 com.itmuch.contentcenter.sentinel.FileDataSourceInit

四、優(yōu)缺點分析

優(yōu)點:
簡單易懂
沒有多余依賴(比如配置中心、緩存等)
缺點:
由于規(guī)則是用 FileRefreshableDataSource 定時更新的,所以規(guī)則更新會有延遲。如果FileRefreshableDataSource定時時間過大,可能長時間延遲;如果FileRefreshableDataSource過小,又會影響性能;
規(guī)則存儲在本地文件,如果有一天需要遷移微服務,那么需要把規(guī)則文件一起遷移,否則規(guī)則會丟失。

五、你可能會有的疑問

Spring Cloud Alibaba不是提供了如下配置了嗎?為什么要全部自己寫呢?

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

關于這個問題,可以參考提的Issuehttps://github.com/alibaba/spring-cloud-alibaba/issues/756):

六、參考文檔
https://github.com/alibaba/Sentinel/wiki/在生產環(huán)境中使用-Sentinel#pull模式
七、案例測試
7.1. 添加流控規(guī)則

簇點鏈路-流控

7.2. 服務停止

停止內容中心和Sentinel控制臺

7.3. 重新啟動服務

重新啟動Sentinel控制臺和內容中心

7.4. 調用接口

由于Sentinel控制臺采用的是懶加載策略,因此,需要請求一次接口,再刷新

http://localhost:8010/shares/1

7.5. 查看流控規(guī)則

刷新Sentinel控制臺,查看流控規(guī)則

總結

以上是生活随笔為你收集整理的Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。