use of com.alibaba.csp.sentinel.datasource.ReadableDataSource in project jboot by yangfuhai.
the class JbootSentinelBuilder method init.
public void init() {
SentinelConfig config = SentinelConfig.get();
// todo 晚点实现主动 Sentinel 控制台写入到数据源
if (StrUtil.isNotBlank(config.getDatasource())) {
SentinelDatasourceFactory factory = getDatasourceFactory(config);
ReadableDataSource rds = factory.createDataSource();
FlowRuleManager.register2Property(rds.getProperty());
} else // 当未配置数据源的情况下,使用文件数据源
// 将可写数据源注册至 transport 模块的 WritableDataSourceRegistry 中.
// 这样收到控制台推送的规则时,Sentinel 会先更新到内存,然后将规则写入到文件中.
// 文档:https://github.com/alibaba/Sentinel/wiki/%E5%9C%A8%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%E4%BD%BF%E7%94%A8-Sentinel
{
String rulePath = config.getRuleFile();
File ruleFile = rulePath.startsWith("/") ? new File(rulePath) : new File(PathKit.getWebRootPath(), rulePath);
ReadableDataSource rds = new FileDataSource<>(ruleFile, this::decodeJson);
FlowRuleManager.register2Property(rds.getProperty());
WritableDataSource<List<FlowRule>> wds = new FileWritableDataSource<>(ruleFile, this::encodeJson);
WritableDataSourceRegistry.registerFlowDataSource(wds);
}
}
Aggregations