日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

改造Sentinel源碼,實現Nacos雙向通信!

來源: 責編: 時間:2023-10-17 18:04:57 326觀看
導讀Sentinel Dashboard(控制臺)默認情況下,只能將配置規則保存到內存中,這樣就會導致 Sentinel Dashboard 重啟后配置規則丟失的情況,因此我們需要將規則保存到某種數據源中,Sentinel 支持的數據源有以下這些:圖片然而,默認情況

Sentinel Dashboard(控制臺)默認情況下,只能將配置規則保存到內存中,這樣就會導致 Sentinel Dashboard 重啟后配置規則丟失的情況,因此我們需要將規則保存到某種數據源中,Sentinel 支持的數據源有以下這些:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

然而,默認情況下,Sentinel 和數據源之間的關系是單向數據通訊的,也就是只能先在數據源中配置規則,然后數據源會被規則推送至 Sentinel Dashboard 和 Sentinel 客戶端,但是在 Sentinel Dashboard 中修改規則或新增規則是不能反向同步到數據源中的,這就是單向通訊。vzW28資訊網——每日最新資訊28at.com

所以,今天我們就該修改一下 Sentinel 的源碼,讓其可以同步規則至數據源,改造之后的交互流程如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

Sentinel 同步規則至數據源,例如將 Sentinel 的規則,同步規則至 Nacos 數據源的改造步驟很多,但整體實現難度不大,下面我們一起來看吧。vzW28資訊網——每日最新資訊28at.com

1.下載Sentinel源碼

下載地址:https://github.com/alibaba/SentinelvzW28資訊網——每日最新資訊28at.com

PS:本文 Sentinel 使用的版本是 1.8.6。vzW28資訊網——每日最新資訊28at.com

下載源碼之后,使用 idea 打開里面的 sentinel-dashboard 項目,如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

2.修改pom.xml

將 sentinel-datasource-nacos 底下的 scope 注釋掉,如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

PS:因為官方提供的 Nacos 持久化實例,是在 test 目錄下進行單元測試的,而我們是用于生產環境,所以需要將 scope 中的 test 去掉。vzW28資訊網——每日最新資訊28at.com

3.移動單元測試代碼

將 test/com.alibaba.csp.sentinel.dashboard.rule.nacos 下所有文件復制到 src/main/java/com.alibaba.csp.sentinel.dashboard.rule 目錄下,如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

4.新建NacosPropertiesConfiguration文件

在 com.alibaba.csp.sentinel.dashboard.rule 下創建 Nacos 配置文件的讀取類,實現代碼如下:vzW28資訊網——每日最新資訊28at.com

package com.alibaba.csp.sentinel.dashboard.rule;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;@ConfigurationProperties(prefix = "sentinel.nacos")@Configurationpublic class NacosPropertiesConfiguration {    private String serverAddr;    private String dataId;    private String groupId;    private String namespace;    private String username;    private String password;    // 省略 Getter/Setter 代碼}

5.修改NacosConfig文件

只修改 NacosConfig 中的 nacosConfigService 方法,修改后的代碼如下:vzW28資訊網——每日最新資訊28at.com

@Beanpublic ConfigService nacosConfigService(NacosPropertiesConfiguration nacosPropertiesConfiguration) throws Exception {    Properties properties = new Properties();    properties.put(PropertyKeyConst.SERVER_ADDR, nacosPropertiesConfiguration.getServerAddr());    properties.put(PropertyKeyConst.NAMESPACE, nacosPropertiesConfiguration.getNamespace());    properties.put(PropertyKeyConst.USERNAME,nacosPropertiesConfiguration.getUsername());    properties.put(PropertyKeyConst.PASSWORD,nacosPropertiesConfiguration.getPassword());    return ConfigFactory.createConfigService(properties);//        return ConfigFactory.createConfigService("localhost"); // 原代碼}

6.修改FlowControllerV2文件

修改 com.alibaba.csp.sentinel.dashboard.controller.v2 目錄下的 FlowControllerV2 文件:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

修改后代碼:vzW28資訊網——每日最新資訊28at.com

@Autowired@Qualifier("flowRuleNacosProvider")private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;@Autowired@Qualifier("flowRuleNacosPublisher")private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

vzW28資訊網——每日最新資訊28at.com

PS:此操作的目的是開啟 Controller 層操作 Nacos 的開關。vzW28資訊網——每日最新資訊28at.com

如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

7.修改配置信息

在 application.properties 中設置 Nacos 連接信息,配置如下:vzW28資訊網——每日最新資訊28at.com

sentinel.nacos.serverAddr=localhost:8848sentinel.nacos.username=nacossentinel.nacos.password=nacossentinel.nacos.namespace=sentinel.nacos.groupId=DEFAULT_GROUPsentinel.nacos.dataId=sentinel-dashboard-demo-sentinel

8.修改sidebar.html

修改 webapp/resources/app/scripts/directives/sidebar/sidebar.html 文件:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

搜索“dashboard.flowV1”改為“dashboard.flow”,如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

9.修改identity.js

identity.js 文件有兩處修改,它位于 webapp/resources/app/scripts/controllers/identity.js 目錄。vzW28資訊網——每日最新資訊28at.com

9.1 第一處修改

將“FlowServiceV1”修改為“FlowServiceV2”,如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

9.2 第二處修改

搜索“/dashboard/flow/”修改為“/dashboard/v2/flow/”,如下圖所示:vzW28資訊網——每日最新資訊28at.com

圖片圖片vzW28資訊網——每日最新資訊28at.com

PS:修改 identity.js 文件主要是用于在 Sentinel 點擊資源的“流控”按鈕添加規則后將信息同步給 Nacos。vzW28資訊網——每日最新資訊28at.com

小結

Sentinel Dashboard 默認情況下,只能將配置規則保存到內存中,這樣就會程序重啟后配置規則丟失的情況,因此我們需要給 Sentinel 設置一個數據源,并且要和數據源之間實現雙向通訊,所以我們需要修改 Sentinel 的源碼。源碼的改造步驟雖然很多,但只要逐一核對和修改就可以實現 Sentinel 生成環境的配置了。看完記得收藏哦,防止以后用的時候找不到。vzW28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-13673-0.html改造Sentinel源碼,實現Nacos雙向通信!

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 7個開發者不可不知的VS Code小技巧

下一篇: 使用Python處理大型CSV文件

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 乌审旗| 伊吾县| 马尔康县| 天柱县| 孝感市| 商河县| 收藏| 翼城县| 岚皋县| 黑河市| 尚志市| 营山县| 康乐县| 宜都市| 山阴县| 东平县| 湖州市| 奈曼旗| 静海县| 邵阳市| 灌南县| 陕西省| 宣化县| 孟连| 裕民县| 北海市| 同心县| 汝南县| 亳州市| 望奎县| 壶关县| 岳阳市| 安西县| 武功县| 泰来县| 长丰县| 宜兴市| 酒泉市| 介休市| 莫力| 六枝特区|