1. 程式人生 > >Sentinel Dashboard中修改規則同步到Nacos

Sentinel Dashboard中修改規則同步到Nacos

上一篇我們介紹瞭如何通過改造Sentinel Dashboard來實現修改規則之後自動同步到Apollo。下面通過這篇,詳細介紹當使用Nacos作為配置中心之後,如何實現Sentinel Dashboard中修改規則同步到Nacos。關於下面改造的原理和分析可以見上一篇《Sentinel Dashboard中修改規則同步到Apollo》的頭兩節內容,這裡不重複介紹了。

程式碼實現

下面直接來看看如何實現的具體改造步驟,這裡參考了Sentinel Dashboard原始碼中關於Nacos實現的測試用例。但是由於考慮到與Spring Cloud Alibaba的結合使用,略作修改。

第一步:修改pom.xml

中的sentinel-datasource-nacos的依賴,將<scope>test</scope>註釋掉,這樣才能在主程式中使用。

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
    <!--<scope>test</scope>-->
</dependency>

第二步:找到resources/app/scripts/directives/sidebar/sidebar.html

中的這段程式碼:

<li ui-sref-active="active">
    <a ui-sref="dashboard.flowV1({app: entry.app})">
        <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規則
    </a>
</li>

修改為:

<li ui-sref-active="active">
    <a ui-sref="dashboard.flow({app: entry.app})">
        <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控規則
    </a>
</li>

第三步:在com.alibaba.csp.sentinel.dashboard.rule包下新建一個nacos包,用來編寫針對Nacos的擴充套件實現。

第四步:建立Nacos的配置類,具體程式碼如下:

@Configuration
public class NacosConfig {

    @Bean
    public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    @Bean
    public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
        return s -> JSON.parseArray(s, FlowRuleEntity.class);
    }

    @Bean
    public ConfigService nacosConfigService() throws Exception {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, "localhost");
        return ConfigFactory.createConfigService(properties);
    }
}

如果用到了namespace隔離環境,可以在nacosConfigService方法中再加入配置,比如:properties.put(PropertyKeyConst.NAMESPACE, "130e71fa-97fe-467d-ad77-967456f2c16d");

第五步:實現Nacos的配置拉取。

@Component("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<String, List<FlowRuleEntity>> converter;

    public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
    public static final String GROUP_ID = "DEFAULT_GROUP";

    @Override
    public List<FlowRuleEntity> getRules(String appName) throws Exception {
        String rules = configService.getConfig(appName + FLOW_DATA_ID_POSTFIX, GROUP_ID, 3000);
        if (StringUtil.isEmpty(rules)) {
            return new ArrayList<>();
        }
        return converter.convert(rules);
    }
}
  • getRules方法中的appName引數是Sentinel中的服務名稱。
  • configService.getConfig方法是從Nacos中獲取配置資訊的具體操作。其中,DataId和GroupId分別對應客戶端使用時候的對應配置。比如這裡的例子對應了之前我們在《Sentinel使用Nacos儲存規則》一文中的配置,具體如下:
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel

注意:兩邊的DataId和GroupId必須對應上。

第六步:實現Nacos的配置推送。

@Component("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {

    @Autowired
    private ConfigService configService;
    @Autowired
    private Converter<List<FlowRuleEntity>, String> converter;

    public static final String FLOW_DATA_ID_POSTFIX = "-sentinel";
    public static final String GROUP_ID = "DEFAULT_GROUP";

    @Override
    public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }
        configService.publishConfig(app + FLOW_DATA_ID_POSTFIX, GROUP_ID, converter.convert(rules));
    }
}

  • 這裡的大部分內容與上一步中的實現一致。主要就是Nacos中儲存配置的DataId和GroupId不要弄錯。

第七步:修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2DynamicRuleProviderDynamicRulePublisher注入的Bean,改為上面我們編寫的針對Apollo的實現:

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

最後,讀者可以使用本文改造後的sentinel-dashboard聯合之前《Sentinel使用Nacos儲存規則》一文的例子來驗證本文內容。

程式碼示例

本文介紹內容的客戶端程式碼,示例讀者可以通過檢視下面倉庫中的alibaba-sentinel-dashboard-nacos專案:

如果您對這些感興趣,歡迎star、follow、收藏、轉發給予支援!

專題推薦