Sentinel Dashboard(控制台)默认情况下,只能将配置规则保存到内存中,这样就会导致 Sentinel Dashboard 重启后配置规则丢失的情况,因此我们需要将规则保存到某种数据源中,Sentinel 支持的数据源有以下这些:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP
然而,默认情况下,Sentinel 和数据源之间的关系是单向数据通讯的,也就是只能先在数据源中配置规则,然后数据源会被规则推送至 Sentinel Dashboard 和 Sentinel 客户端,但是在 Sentinel Dashboard 中修改规则或新增规则是不能反向同步到数据源中的,这就是单向通讯。

所以,今天我们就该修改一下 Sentinel 的源码,让其可以同步规则至数据源,改造之后的交互流程如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP
Sentinel 同步规则至数据源,例如将 Sentinel 的规则,同步规则至 Nacos 数据源的改造步骤很多,但整体实现难度不大,下面我们一起来看吧。

1.下载Sentinel源码

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

下载源码之后,使用 idea 打开里面的 sentinel-dashboard 项目,如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP

2.修改pom.xml

将 sentinel-datasource-nacos 底下的 scope 注释掉,如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP

3.移动单元测试代码

将 test/com.alibaba.csp.sentinel.dashboard.rule.nacos 下所有文件复制到 src/main/java/com.alibaba.csp.sentinel.dashboard.rule 目录下,如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP

4.新建NacosPropertiesConfiguration文件

在 com.alibaba.csp.sentinel.dashboard.rule 下创建 Nacos 配置文件的读取类,实现代码如下:

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 文件:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP
修改后代码:

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

如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP

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 文件:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP
搜索“dashboard.flowV1”改为“dashboard.flow”,如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP

9.修改identity.js

identity.js 文件有两处修改,它位于 webapp/resources/app/scripts/controllers/identity.js 目录。

9.1 第一处修改

将“FlowServiceV1”修改为“FlowServiceV2”,如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP

9.2 第二处修改

搜索“/dashboard/flow/”修改为“/dashboard/v2/flow/”,如下图所示:
Sentinel源码改造,实现Nacos双向通信!-LMLPHP

小结

Sentinel Dashboard 默认情况下,只能将配置规则保存到内存中,这样就会程序重启后配置规则丢失的情况,因此我们需要给 Sentinel 设置一个数据源,并且要和数据源之间实现双向通讯,所以我们需要修改 Sentinel 的源码。源码的改造步骤虽然很多,但只要逐一核对和修改就可以实现 Sentinel 生成环境的配置了。看完记得收藏哦,防止以后用的时候找不到。

10-18 08:52