use of com.alibaba.csp.sentinel.datasource.FileWritableDataSource 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);
}
}
use of com.alibaba.csp.sentinel.datasource.FileWritableDataSource in project learn-simple by muggle0.
the class MyflieInitFunc method init.
@Override
public void init() throws Exception {
URL resource = MyflieInitFunc.class.getClassLoader().getResource("");
File file = new File(resource.getPath() + "/config/flow.json");
File fileParent = file.getParentFile();
if (!fileParent.exists()) {
fileParent.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
ReadableDataSource<String, List<FlowRule>> flowReadDataSource = new FileRefreshableDataSource<>(resource.getPath() + "/config/flow.json", source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowReadDataSource.getProperty());
WritableDataSource<List<FlowRule>> flowWriteDataSource = new FileWritableDataSource<>(resource.getPath() + "/config/flow.json", t -> JSON.toJSONString(t));
WritableDataSourceRegistry.registerFlowDataSource(flowWriteDataSource);
}
Aggregations