偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Sentinel如何持久化數(shù)據(jù)到Nacos?

開發(fā) 架構
Sentinel Dashboard 默認情況下,只能將配置規(guī)則保存到內存中,這樣就會程序重啟后配置規(guī)則丟失的情況,因此我們需要給 Sentinel 設置一個數(shù)據(jù)源,并且要和數(shù)據(jù)源之間實現(xiàn)雙向通訊,所以我們需要修改 Sentinel 的源碼。

默認情況下 Sentinel 只能接收到 Nacos 推送的消息,但不能將自己控制臺修改的信息同步給 Nacos,如下圖所示:

但是在生成環(huán)境下,我們?yōu)榱烁奖愕牟僮鳎切枰獙?Sentinel 控制臺修改的規(guī)則也同步到 Nacos 的,所以在這種情況下我們就需要修改 Sentinel 的源碼,讓其可以實現(xiàn)和 Nacos 的雙向通訊,如下圖所示:

改造之后的交互流程如下圖所示:

圖片圖片

Sentinel 同步規(guī)則至數(shù)據(jù)源,例如將 Sentinel 的規(guī)則,同步規(guī)則至 Nacos 數(shù)據(jù)源的改造步驟很多,但整體實現(xiàn)難度不大,下面我們一起來看吧。

1.下載Sentinel源碼

下載地址:https://github.com/alibaba/Sentinel

PS:本文 Sentinel 使用的版本是 1.8.6。

下載源碼之后,使用 idea 打開里面的 sentinel-dashboard 項目,如下圖所示:

2.修改pom.xml

將 sentinel-datasource-nacos 底下的 scope 注釋掉,如下圖所示:

PS:因為官方提供的 Nacos 持久化實例,是在 test 目錄下進行單元測試的,而我們是用于生產環(huán)境,所以需要將 scope 中的 test 去掉。

3.移動單元測試代碼

將 test/com.alibaba.csp.sentinel.dashboard.rule.nacos 下所有文件復制到 src/main/java/com.alibaba.csp.sentinel.dashboard.rule 目錄下,如下圖所示:

4.新建NacosPropertiesConfiguration文件

在 com.alibaba.csp.sentinel.dashboard.rule 下創(chuàng)建 Nacos 配置文件的讀取類,實現(xiàn)代碼如下:

package com.alibaba.csp.sentinel.dashboard.rule;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties(prefix = "sentinel.nacos")
@Configuration
public 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 方法,修改后的代碼如下:

@Bean
public 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 文件:

修改后代碼:

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

PS:此操作的目的是開啟 Controller 層操作 Nacos 的開關。

如下圖所示:

7.修改配置信息

在 application.properties 中設置 Nacos 連接信息,配置如下:

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

8.修改sidebar.html

修改 webapp/resources/app/scripts/directives/sidebar/sidebar.html 文件:

搜索“dashboard.flowV1”改為“dashboard.flow”,如下圖所示:

9.修改identity.js

identity.js 文件有兩處修改,它位于 webapp/resources/app/scripts/controllers/identity.js 目錄。

(1)第一處修改

將“FlowServiceV1”修改為“FlowServiceV2”,如下圖所示:

(2)第二處修改

搜索“/dashboard/flow/”修改為“/dashboard/v2/flow/”,如下圖所示:

PS:修改 identity.js 文件主要是用于在 Sentinel 點擊資源的“流控”按鈕添加規(guī)則后將信息同步給 Nacos。

小結

Sentinel Dashboard 默認情況下,只能將配置規(guī)則保存到內存中,這樣就會程序重啟后配置規(guī)則丟失的情況,因此我們需要給 Sentinel 設置一個數(shù)據(jù)源,并且要和數(shù)據(jù)源之間實現(xiàn)雙向通訊,所以我們需要修改 Sentinel 的源碼。源碼的改造步驟雖然很多,但只要逐一核對和修改就可以實現(xiàn) Sentinel 生成環(huán)境的配置了。

責任編輯:姜華 來源: Java中文社群
相關推薦

2011-07-07 15:45:45

iPhone SQLite 數(shù)據(jù)

2025-05-08 01:20:00

2021-05-20 08:01:15

Nacos 存儲Sentinel

2021-03-18 08:18:15

ZooKeeper數(shù)據(jù)持久化

2009-08-26 18:05:25

ViewState持久

2021-11-18 13:14:08

DDD聚合代碼

2023-08-17 16:17:00

Docker前端

2023-10-17 17:13:14

內存程序源碼

2013-09-12 14:56:02

iOS持久化

2021-01-21 08:49:52

數(shù)據(jù)單體架構

2018-12-14 09:48:23

Redis數(shù)據(jù)故障

2017-09-21 08:16:33

數(shù)據(jù)存儲環(huán)境

2011-08-17 15:19:38

iPhone應用數(shù)據(jù)

2011-06-07 17:16:47

iPhone 數(shù)據(jù)

2022-08-30 10:15:27

Kubernetes數(shù)據(jù)持久化管理

2024-01-02 07:55:26

MySQLRedolog緩存

2024-03-26 00:03:08

Redis數(shù)據(jù)RDB

2023-09-06 17:02:53

2024-12-27 09:32:25

MyBatis代碼

2023-11-26 09:06:46

點贊
收藏

51CTO技術棧公眾號