Search in sources :

Example 1 with FileWritableDataSource

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);
    }
}
Also used : ReadableDataSource(com.alibaba.csp.sentinel.datasource.ReadableDataSource) List(java.util.List) FileWritableDataSource(com.alibaba.csp.sentinel.datasource.FileWritableDataSource) File(java.io.File)

Example 2 with FileWritableDataSource

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);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(com.alibaba.fastjson.TypeReference) FlowRule(com.alibaba.csp.sentinel.slots.block.flow.FlowRule) FileWritableDataSource(com.alibaba.csp.sentinel.datasource.FileWritableDataSource) File(java.io.File) URL(java.net.URL) FileRefreshableDataSource(com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource)

Aggregations

FileWritableDataSource (com.alibaba.csp.sentinel.datasource.FileWritableDataSource)2 File (java.io.File)2 List (java.util.List)2 FileRefreshableDataSource (com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource)1 ReadableDataSource (com.alibaba.csp.sentinel.datasource.ReadableDataSource)1 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)1 TypeReference (com.alibaba.fastjson.TypeReference)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1