alibaba / spring-cloud-alibaba

Spring Cloud Alibaba provides a one-stop solution for application development for the distributed solutions of Alibaba middleware.
https://sca.aliyun.com
Apache License 2.0
27.9k stars 8.32k forks source link

关于动态数据源支持 #756

Open eacdy opened 5 years ago

eacdy commented 5 years ago

我看到文档里有:

spring.cloud.sentinel.datasource.ds1.file.file=classpath: degraderule.json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

#spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
#spring.cloud.sentinel.datasource.ds1.file.data-type=custom
#spring.cloud.sentinel.datasource.ds1.file.converter-class=com.alibaba.cloud.examples.JsonFlowRuleListConverter
#spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

这一段,让Sentinel能定时从本地文件读取规则。但没有WritableDataSource的相关配置,那么如果控制台推送规则到微服务上,微服务要如何修改本地的规则文件呢?


对照Sentinel官方文档里的示例代码:

来自:http://www.itmuch.com/spring-cloud-alibaba/sentinel-rules-persistence-pull-mode/

public class FileDataSourceInit implements InitFunc {

    @Override
    public void init() throws Exception {
        String flowRulePath = "xxx";

        ReadableDataSource<String, List<FlowRule>> ds = new FileRefreshableDataSource<>(
            flowRulePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})
        );
        // 将可读数据源注册至 FlowRuleManager.
        FlowRuleManager.register2Property(ds.getProperty());

        WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(flowRulePath, this::encodeJson);
        // 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
        // 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
        WritableDataSourceRegistry.registerFlowDataSource(wds);
    }

    private <T> String encodeJson(T t) {
        return JSON.toJSONString(t);
    }
}

读和写都要有,才完整。

请问是否能改进下?

否则现有设计不如自己写了,像我的博客一样:Alibaba Sentinel规则持久化-拉模式-手把手教程【基于文件】

MadlifeZhou commented 4 years ago

大目老师真的太厉害了