sentinal源码初识二 规则持久化-持久化到文件

参考源码中的sentinel-demo-dynamic-file-rule项目

源码流程图

在这里插入图片描述

源码分析

dashboard端保存和读取规则

保存规则

以flow为例,前端保存时会调用/rule保存
先保存到本地缓存中,然后通过http,全量数据推送给客户端

@PostMapping("/rule")
@AuthAction(PrivilegeType.WRITE_RULE)
public Result<FlowRuleEntity> apiAddFlowRule(@RequestBody FlowRuleEntity entity) {
    Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
    if (checkResult != null) {
        return checkResult;
    }
    entity.setId(null);
    Date date = new Date();
    entity.setGmtCreate(date);
    entity.setGmtModified(date);
    entity.setLimitApp(entity.getLimitApp().trim());
    entity.setResource(entity.getResource().trim());
    try {
    	 //保存本地缓存中
        entity = repository.save(entity);
        //推给客户端(http://ip:port/setRules)
        publishRules(entity.getApp(), entity.getIp(), entity.getPort()).get(5000, TimeUnit.MILLISECONDS);
        return Result.ofSuccess(entity);
    } catch (Throwable t) {
        ...
        return Result.ofFail(-1, e.getMessage());
    }
}

获取规则

前端查看规则时,会向客户端发起http请求,从客户端拉取所有规则

@GetMapping("/rules")
@AuthAction(PrivilegeType.READ_RULE)
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app,
                                                         @RequestParam String ip,
                                                         @RequestParam Integer port) {
    ...
    try {
    	//通过http调用客户端,从客户端拉取规则
        List<FlowRuleEntity> rules = sentinelApiClient.fetchFlowRuleOfMachine(app, ip, port);
        rules = repository.saveAll(rules);
        return Result.ofSuccess(rules);
    } catch (Throwable throwable) {
        return Result.ofThrowable(-1, throwable);
    }
}

客户端

启动服务,接收请求

Env

在第一次判断熔断限流逻辑时,会走Env的静态代码块

public class Env {
    public static final Sph sph = new CtSph();
    static {
        // If init fails, the process will exit.
        InitExecutor.doInit();
    }
}
InitExecutor.doInit()

通过spi获取所有InitFunc类型的类,调用init方法
此处就是扩展点,可以通过这里,调用我们自己自定义的InitFunc类

public static void doInit() {
	//只初始化一次
    if (!initialized.compareAndSet(false, true)) {
        return;
    }
    try {
    	//通过spi获取所有InitFunc类
        List<InitFunc> initFuncs = SpiLoader.of(InitFunc.class).loadInstanceListSorted();
        List<OrderWrapper> initList = new ArrayList<OrderWrapper>();
        for (InitFunc initFunc : initFuncs) {
            insertSorted(initList, initFunc);
        }
        //调用InitFunc类的init方法
        for (OrderWrapper w : initList) {
            w.func.init();
            RecordLog.info("[InitExecutor] Executing {} with order {}",
                w.func.getClass().getCanonicalName(), w.order);
        }
    } 
}
CommandCenterInitFunc

此类为sentinal提供,会第一个调用此类
这个类用来启动服务,用来和dashboard通信

@InitOrder(-1)
public class CommandCenterInitFunc implements InitFunc {
    @Override
    public void init() throws Exception {
    	//此类构造时,会调用静态代码块,通过spi机制获取CommandCenter
        CommandCenter commandCenter = CommandCenterProvider.getCommandCenter();
        if (commandCenter == null) {
            RecordLog.warn("[CommandCenterInitFunc] Cannot resolve CommandCenter");
            return;
        }
        commandCenter.beforeStart();
        commandCenter.start();
        RecordLog.info("[CommandCenterInit] Starting command center: "
                + commandCenter.getClass().getCanonicalName());
    }
}

通过SPI获取服务,默认获取SimpleHttpCommandCenter

public final class CommandCenterProvider {

    private static CommandCenter commandCenter = null;

    static {
        resolveInstance();
    }

    private static void resolveInstance() {
        CommandCenter resolveCommandCenter = SpiLoader.of(CommandCenter.class).loadHighestPriorityInstance();

        if (resolveCommandCenter == null) {
        } else {
            commandCenter = resolveCommandCenter;
        }
    }

    public static CommandCenter getCommandCenter() {
        return commandCenter;
    }

    private CommandCenterProvider() {}
}

SimpleHttpCommandCenter

此类会启动bio服务,用来和dashboard通信,代码省略…
最终接收到请求时,会调用CommandHandler
用来处理规则修改的类是ModifyRulesCommandHandler

ModifyRulesCommandHandler

handle方法中,处理flow逻辑如下
会先将规则保存到缓存中
然后将规则持久化到数据源(默认不持久话)
我们自己注入DataSource,来持久化数据

 if (FLOW_RULE_TYPE.equalsIgnoreCase(type)) {
    List<FlowRule> flowRules = JSONArray.parseArray(data, FlowRule.class);
    FlowRuleManager.loadRules(flowRules);
    if (!writeToDataSource(getFlowDataSource(), flowRules)) {
        result = WRITE_DS_FAILURE_MSG;
    }
    return CommandResponse.ofSuccess(result);
}

持久化到文件

新写一个项目,封装持久化逻辑,参考sentinel-demo-dynamic-file-rule项目

FileDataSourceInit

通过下面配置,客户端启动的时候,会通过spi机制自动调用init方法

1、创建类FileDataSourceInit
2、添加文件/META-INF\services/com.alibaba.csp.sentinel.init.InitFunc
3、文件内容为FileDataSourceInit 类的路径

public class FileDataSourceInit implements InitFunc {

    @Override
    public void init() throws Exception {
    	//设置持久化路径
        String flowRuleDir = System.getProperty("user.home") + File.separator + "sentinel" + File.separator + "rules";
        String flowRuleFile = "flowRule.json";
        String flowRulePath = flowRuleDir + File.separator + flowRuleFile;
		//监听文件变化
        ReadableDataSource<String, List<FlowRule>> ds = new FileRefreshableDataSource<>(
            flowRulePath, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {})
        );
        FlowRuleManager.register2Property(ds.getProperty());
        //构建持久化数据源
        WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(flowRulePath, this::encodeJson);
        //注入数据源到flow
        WritableDataSourceRegistry.registerFlowDataSource(wds);
    }
    private <T> String encodeJson(T t) {
        return JSON.toJSONString(t);
    }
}
FileWritableDataSource

持久化数据源,客户端规则发生变化时,会调用write方法

public class FileWritableDataSource<T> implements WritableDataSource<T> {

    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    private final Converter<T, String> configEncoder;
    private final File file;
    private final Charset charset;

    private final Lock lock = new ReentrantLock(true);

    public FileWritableDataSource(String filePath, Converter<T, String> configEncoder) {
        this(new File(filePath), configEncoder);
    }

    public FileWritableDataSource(File file, Converter<T, String> configEncoder) {
        this(file, configEncoder, DEFAULT_CHARSET);
    }

    public FileWritableDataSource(File file, Converter<T, String> configEncoder, Charset charset) {
        if (file == null || file.isDirectory()) {
            throw new IllegalArgumentException("Bad file");
        }
        if (configEncoder == null) {
            throw new IllegalArgumentException("Config encoder cannot be null");
        }
        if (charset == null) {
            throw new IllegalArgumentException("Charset cannot be null");
        }
        this.configEncoder = configEncoder;
        this.file = file;
        this.charset = charset;
    }

    @Override
    public void write(T value) throws Exception {
        lock.lock();
        try {
            String convertResult = configEncoder.convert(value);
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                byte[] bytesArray = convertResult.getBytes(charset);

                RecordLog.info("[FileWritableDataSource] Writing to file {}: {}", file, convertResult);
                outputStream.write(bytesArray);
                outputStream.flush();
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (Exception ignore) {
                        // nothing
                    }
                }
            }
        } finally {
            lock.unlock();
        }
    }

    @Override
    public void close() throws Exception {
        // Nothing
    }
}

FileRefreshableDataSource

init方法时,构建FileRefreshableDataSource对象,此类父类构造函数会启动定时任务,监听文件是否有变化
只列出部分代码,具体代码可参考源码中的FileRefreshableDataSource

public AutoRefreshDataSource(Converter<S, T> configParser, final long recommendRefreshMs) {
    super(configParser);
    ...
    this.recommendRefreshMs = recommendRefreshMs;
    startTimerService();
}

启动定时任务,监控文件是否有变化

private void startTimerService() {
    service = Executors.newScheduledThreadPool(1,
        new NamedThreadFactory("sentinel-datasource-auto-refresh-task", true));
    service.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            try {
                if (!isModified()) {
                    return;
                }
                T newValue = loadConfig();
                getProperty().updateValue(newValue);
            } catch (Throwable e) {
                RecordLog.info("loadConfig exception", e);
            }
        }
    }, recommendRefreshMs, recommendRefreshMs, TimeUnit.MILLISECONDS);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值