Sentinel——pull模式规则持久化

pull模式规则持久化

pull 模式的数据源(如本地文件、RDBMS 等)一般是可写入的。使用时需要在客户端注册数据源:将对应的读数据源注册至对应的 RuleManager,将写数据源注册至 transport 的 WritableDataSourceRegistry 中。[1]

本地文件数据源会定时轮询文件的变更,读取规则。这样我们既可以在应用本地直接修改文件来更新规则,也可以通过 Sentinel 控制台推送规则。以本地文件数据源为例,推送过程如下图所示:

首先 Sentinel 控制台通过 API 将规则推送至客户端并更新到内存中,接着注册的写数据源会将新的规则保存到本地的文件中。使用 pull 模式的数据源时一般不需要对 Sentinel 控制台进行改造。

这种实现方法好处是简单,不引入新的依赖,坏处是无法保证监控数据的一致性。

定义数据源

这里将数据源定义在了classes目录下了。

package com.zjw.sentinel;

import com.alibaba.csp.sentinel.command.handler.ModifyParamFlowRulesCommandHandler;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.datasource.FileWritableDataSource;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.WritableDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.util.WritableDataSourceRegistry;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @since 2023/12/07 22:10
 */
public class FileDataSourceInit implements InitFunc {

    @Override
    public void init() throws Exception {
        // 获取当前类的ClassLoader
        ClassLoader classLoader = FileDataSourceInit.class.getClassLoader();
//        File ruleDir = new File(System.getProperty("user.dir") + "/rules");
        File ruleDir = new File(classLoader.getResource("").getPath() + File.separator+"rules");
        if (!ruleDir.exists()) {
            ruleDir.mkdirs();
        }
        // 规则文件路径
        String ruleDirPath = ruleDir.getPath();
        readWriteRuleFileFlow(ruleDirPath,"flow-rule.json");
        readWriteRuleFileDegrade(ruleDirPath,"degrade-rule.json");
        readWriteRuleFileParamFlow(ruleDirPath,"param-rule.json");
        readWriteRuleFileAuthority(ruleDirPath,"authority-rule.json");
        readWriteRuleFileSystem(ruleDirPath,"system-rule.json");
    }


    /**
     * 流控规则
     */
    private void readWriteRuleFileFlow(String rulePath, String ruleFile) throws IOException {
        //创建规则文件
        String ruleFilePath = rulePath + File.separator + ruleFile;
        createRuleFile(ruleFilePath);

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

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

    /**
     * 熔断规则
     */
    private void readWriteRuleFileDegrade(String rulePath, String ruleFile) throws IOException {
        //创建规则文件
        String ruleFilePath = rulePath + File.separator + ruleFile;
        createRuleFile(ruleFilePath);

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

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

    /**
     * 热点规则
     */
    private void readWriteRuleFileParamFlow(String rulePath, String ruleFile) throws IOException {
        //创建规则文件
        String ruleFilePath = rulePath + File.separator + ruleFile;
        createRuleFile(ruleFilePath);

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

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

    /**
     * 系统规则
     */
    private void readWriteRuleFileSystem(String rulePath, String ruleFile) throws IOException {
        //创建规则文件
        String ruleFilePath = rulePath + File.separator + ruleFile;
        createRuleFile(ruleFilePath);

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

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

    /**
     * 授权规则
     */
    private void readWriteRuleFileAuthority(String rulePath, String ruleFile) throws IOException {
        //创建规则文件
        String ruleFilePath = rulePath + File.separator + ruleFile;
        createRuleFile(ruleFilePath);

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

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

    private void createRuleFile(String ruleFilePath) throws IOException {
        File file = new File(ruleFilePath);
        if (!file.exists()) {
            file.createNewFile();
        }
    }

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

定义SPI接口文件

pull模式的 Datasource 扩展采用的是 SPt 服务发现机制。按照 SPI 规范,需要在/resources目录下新建 META-INF/services 目录,然后在该目录下新建一个文本文件。文件名为扩展类实现接口的全限定性接口名,文件内容则为该接口的实现类的全限定性类名。

com.alibaba.csp.sentinel.init.InitFunc

com.zjw.sentinel.FileDataSourceInit

测试

启动服务后,发现创建了规则文件。

在sentinel控制台配置了规则后会自动保存在规则文件中。文件中修改了也会在sentinel控制台中修改。服务重启后配置仍然存在。


  1. https://github.com/alibaba/Sentinel/wiki/在生产环境中使用-Sentinel#pull模式 ↩︎

posted @ 2023-12-07 23:20  雨中遐想  阅读(118)  评论(0编辑  收藏  举报