日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
改造Sentinel源碼,實(shí)現(xiàn)Nacos雙向通信!

Sentinel Dashboard(控制臺(tái))默認(rèn)情況下,只能將配置規(guī)則保存到內(nèi)存中,這樣就會(huì)導(dǎo)致 Sentinel Dashboard 重啟后配置規(guī)則丟失的情況,因此我們需要將規(guī)則保存到某種數(shù)據(jù)源中,Sentinel 支持的數(shù)據(jù)源有以下這些:

我們提供的服務(wù)有:網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、犍為ssl等。為上1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的犍為網(wǎng)站制作公司

圖片

然而,默認(rèn)情況下,Sentinel 和數(shù)據(jù)源之間的關(guān)系是單向數(shù)據(jù)通訊的,也就是只能先在數(shù)據(jù)源中配置規(guī)則,然后數(shù)據(jù)源會(huì)被規(guī)則推送至 Sentinel Dashboard 和 Sentinel 客戶端,但是在 Sentinel Dashboard 中修改規(guī)則或新增規(guī)則是不能反向同步到數(shù)據(jù)源中的,這就是單向通訊。

所以,今天我們就該修改一下 Sentinel 的源碼,讓其可以同步規(guī)則至數(shù)據(jù)源,改造之后的交互流程如下圖所示:

圖片

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

1.下載Sentinel源碼

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

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

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

圖片

2.修改pom.xml

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

圖片

PS:因?yàn)楣俜教峁┑?Nacos 持久化實(shí)例,是在 test 目錄下進(jìn)行單元測(cè)試的,而我們是用于生產(chǎn)環(huán)境,所以需要將 scope 中的 test 去掉。

3.移動(dòng)單元測(cè)試代碼

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

圖片

4.新建NacosPropertiesConfiguration文件

在 com.alibaba.csp.sentinel.dashboard.rule 下創(chuàng)建 Nacos 配置文件的讀取類,實(shí)現(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> ruleProvider;
@Autowired
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher> rulePublisher;

PS:此操作的目的是開啟 Controller 層操作 Nacos 的開關(guān)。

如下圖所示:

圖片

7.修改配置信息

在 application.properties 中設(shè)置 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 目錄。

9.1 第一處修改

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

圖片

9.2 第二處修改

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

圖片

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

小結(jié)

Sentinel Dashboard 默認(rèn)情況下,只能將配置規(guī)則保存到內(nèi)存中,這樣就會(huì)程序重啟后配置規(guī)則丟失的情況,因此我們需要給 Sentinel 設(shè)置一個(gè)數(shù)據(jù)源,并且要和數(shù)據(jù)源之間實(shí)現(xiàn)雙向通訊,所以我們需要修改 Sentinel 的源碼。源碼的改造步驟雖然很多,但只要逐一核對(duì)和修改就可以實(shí)現(xiàn) Sentinel 生成環(huán)境的配置了??赐暧浀檬詹嘏叮乐挂院笥玫臅r(shí)候找不到。


網(wǎng)頁(yè)題目:改造Sentinel源碼,實(shí)現(xiàn)Nacos雙向通信!
鏈接分享:http://www.5511xx.com/article/dhscioh.html