Sentinel規則持久化

在這里插入圖片描述

首先 Sentinel 控制臺通過 API 將規則推送至客戶端并更新到內存中,接著注冊的寫數據源會將新的規則保存到本地的文件中。

示例代碼:

1.編寫處理類

//規則持久化
public class FilePersistence implements InitFunc {@Value("spring.application:name")private String appcationName;@Overridepublic void init() throws Exception {String ruleDir = System.getProperty("user.home") + "/sentinelrules/"+appcationName;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);// 流控規則ReadableDataSource<String, List<FlowRule>> flowRuleRDS = new FileRefreshableDataSource<>(flowRulePath,flowRuleListParser);FlowRuleManager.register2Property(flowRuleRDS.getProperty());WritableDataSource<List<FlowRule>> flowRuleWDS = new FileWritableDataSource<>(flowRulePath,this::encodeJson);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);}
}

2.添加配置

在resources下創建配置目錄 META-INF/services ,然后添加文件 com.alibaba.csp.sentinel.init.InitFunc 在文件中添加配置類的全路徑

com.zxt.config.FilePersistence

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/38380.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/38380.shtml
英文地址,請注明出處:http://en.pswp.cn/news/38380.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

云原生k8s---資源限制、探針

目錄 一&#xff1a;資源限制 1、資源限制原因 2、Pod 和 容器 的資源請求和限制 3、CPU 資源單位 4、內存 資源單位 5、事例 &#xff08;1&#xff09;事例一 &#xff08;2&#xff09;事例二 二&#xff1a;重啟策略 1、重啟策略模式 2、事例 三&#xff1a;探針…

Win10提醒事項如何打開?電腦上如何添加日程提醒?

有不少上班族表示自己在日常辦公時&#xff0c;經常會忘記一些重要的事情&#xff0c;例如領導安排給自己的任務、會議安排、項目截止日期等。為了避免自己忘記工作事項&#xff0c;很多人都想要在電腦上設置提醒事項或添加日程提醒。那么Win10提醒事項如何打開呢&#xff1f;P…

Golang 中的 archive/zip 包詳解(一):實現 ZIP 壓縮與解壓

Golang 中的 archive/zip 包用于處理 ZIP 格式的壓縮文件&#xff0c;提供了一系列用于創建、讀取和解壓縮 ZIP 格式文件的函數和類型&#xff0c;使用起來非常方便。 實現壓縮功能 1、首先需要創建一個 zip 文件。 zip 文件也是一個文件&#xff0c;首先需要創建一個基礎的…

[Leetcode] [Tutorial] 多維動態規劃(未完待續)

文章目錄 62. 不同路徑Solution 62. 不同路徑 一個機器人位于一個 m ? * ? n 網格的左上角 &#xff08;起始點在下圖中標記為 “Start” &#xff09;。 機器人每次只能向下或者向右移動一步。機器人試圖達到網格的右下角。 問總共有多少條不同的路徑&#xff1f; 示例…

【electron】electron項目創建的方式:

文章目錄 【1】npm init quick-start/electron&#xff08;推薦&#xff09;【2】 克隆倉庫&#xff0c;快速啟動【3】 通過腳手架搭建項目【4】 手動創建項目 【Electron官網】https://www.electronjs.org/zh/docs/latest/api/app 【1】npm init quick-start/electron&#xf…

Excelize Go語言操作 Office Excel文檔基礎庫

Excelize 是 Go 語言編寫的用于操作 Office Excel 文檔基礎庫&#xff0c;基于 ECMA-376&#xff0c;ISO/IEC 29500 國際標準。可以使用它來讀取、寫入由 Microsoft Excel? 2007 及以上版本創建的電子表格文檔。支持 XLAM / XLSM / XLSX / XLTM / XLTX 等多種文檔格式&#xf…

微信小程序實現左滑刪除

一、效果 二、代碼 實現思路使用的是官方提供的 movable-area&#xff1a;注意點&#xff0c;需要設置其高度&#xff0c;否則會出現列表內容重疊的現象。由于movable-view需要向右移動&#xff0c;左滑的時候給刪除控件展示的空間&#xff0c;故 movable-area 需要左移 left:…

Android Gradle Plug-in(AGP ) 的對應關系

AGP 和 Gradle 版本的對應關系 Android Gradle 插件版本說明 | Android 開發者 | Android Developers 插件版本所需的最低 Gradle 版本8.18.08.08.07.47.57.37.47.27.3.37.17.27.07.04.2.06.7.1 舊版 插件版本所需的 Gradle 版本4.1.06.54.0.06.1.13.6.0 - 3.6.45.6.4…

sqlloader學習筆記

INFILE的用法 1&#xff09;模糊導入多個數據的文件。 可以在文件名中使用通配符。 星號 &#xff08;*&#xff09; 表示復數字符&#xff0c;問號 &#xff08;&#xff1f;&#xff09; 表示單個字符。 INFILE emp*.dat INFILE m?emp.dat 2&#xff09;如果不需要導入數據…

jQuery EasyUI datagrid 無記錄時,增加“暫無數據“提示

我們只需要在onLoadSuccess中添加如下代碼&#xff1a; if (data.total 0) {var body $(this).data().datagrid.dc.body2;body.find(table tbody).append(<tr><td width" body.width() " style"height: 35px; text-align: center;"><h…

C語言學習之封裝自定義函數,實現atoi函數功能

實例要求&#xff1a;atoi函數的功能是把字符串轉成整型數值并輸出&#xff1b;把字符串"123456"轉換成數值123456 &#xff0c;并返回數值&#xff1b;函數名&#xff1a; int myatoi(char *str);實例分析&#xff1a; 1.自定義的封裝函數類型是整型&#xff0c;…

在阿里云Linux服務器上部署MySQL數據庫流程

阿里云百科分享在阿里云Linux服務器上部署MySQL數據庫流程&#xff0c;MySQL是一個關系型數據庫管理系統&#xff0c;常用于LAMP和LNMP等網站場景中。本教程介紹如何在Linux系統ECS實例上安裝、配置以及遠程訪問MySQL數據庫。 目錄 背景信息 Alibaba Cloud Linux 2/3、CentO…

上傳excel文件

文件上傳&#xff0c;其實就是用el-upload組件來實現上傳&#xff0c;只是換了樣式&#xff0c;和圖片上傳一樣 <el-form-item label"選擇文件"><el-input placeholder"請選擇文件" v-model"form.file" disabled style"width: 45…

java 使用log4j顯示到界面和文件 并格式化

1.下載log4j jar包https://dlcdn.apache.org/logging/log4j/2.20.0/apache-log4j-2.20.0-bin.zip 2. 我只要到核心包 &#xff0c;看需要 sources是源碼包&#xff0c;可以看到說明。在IDEA里先加入class jar后&#xff0c;再雙擊這個class jar包或或右鍵選Navigate ,Add ,…

android.system.ErrnoException: open failed: EPERM (Operation not permitted)

android 10(Q)開始增加了沙盒機制&#xff0c;不能直接把文件保存到/sdcard目錄下&#xff0c;只能保存到APP專屬目錄下&#xff1b;AndroidManifest.xml在標簽下增加屬性【android:requestLegacyExternalStorage“true”】可以暫時保存到/sdcard路徑下&#xff0c;但是Android…

Revit SDK 介紹:PanelSchedule 配電盤明細表

前言 這個例子介紹 Revit 的配電盤明細表&#xff0c;PanelSchedule。Revit 的電器專業在國內用的并不是十分廣泛&#xff0c;但從功能上來說還是比較完整的。 內容 這個例子里有三個命令&#xff1a; PanelScheduleExport - 導出配電盤明細表InstanceViewCreation - 創建配…

【0基礎學爬蟲】爬蟲基礎之網絡請求庫的使用

大數據時代&#xff0c;各行各業對數據采集的需求日益增多&#xff0c;網絡爬蟲的運用也更為廣泛&#xff0c;越來越多的人開始學習網絡爬蟲這項技術&#xff0c;K哥爬蟲此前已經推出不少爬蟲進階、逆向相關文章&#xff0c;為實現從易到難全方位覆蓋&#xff0c;特設【0基礎學…

【Visual Studio Code】--- Win11 C盤爆滿 修改 Code 插件數據和緩存的保存路徑

Win11 C盤爆滿 修改 Code 插件數據和緩存的保存路徑 一、概述二、修改 Code 插件數據和緩存的保存路徑 一、概述 一個好的文章能夠幫助開發者完成更便捷、更快速的開發。書山有路勤為徑&#xff0c;學海無涯苦作舟。我是秋知葉i、期望每一個閱讀了我的文章的開發者都能夠有所成…

領航優配:EFT交易是什么意思?

EFT買賣是一種電子資金搬運買賣方法&#xff0c;EFT代表電子資金搬運&#xff0c;將現金從一個銀行賬戶搬運到另一個銀行賬戶。盡管這種買賣方法已經存在了幾十年&#xff0c;但隨著技能的開展&#xff0c;越來越多的人開始使用它。 從技能視點&#xff0c;EFT買賣是經過計算機…

【C語言】memset()函數

一.memset()函數簡介 我們先來看一下cplusplus.com - The C Resources Network網站上memset()函數的基本信息&#xff1a; 1.函數功能 memset()函數的功能是:將一塊內存空間的每個字節都設置為指定的值。 這個函數通常用于初始化一個內存空間&#xff0c;或者清空一個內存空間…