Sentinel NacosDataSource源码解析
官网代码
public static final String NAMESPACE = "user-space";
public static final String FLOW_POSTFIX = "-flow-rules";
// Register cluster flow rule property supplier which creates data source by namespace.
ClusterFlowRuleManager.setPropertySupplier(namespace -> {
ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(remoteAddress, groupId,
namespace + DemoConstants.FLOW_POSTFIX,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
return ds.getProperty();
});
如果在默认命名空间 public 下可以获取到配置,如果使用自定义的命名空间就报404。原因如下:(直接上源码,有兴趣的同学自己Dubug)
com.alibaba.nacos.client.config.NacosConfigService
Nacos 配置请求具体实现代码
com.alibaba.nacos.client.config.impl.ClientWorker
结论
ClusterFlowRuleManager.setPropertySupplier(namespace -> {
Properties properties = new Properties();
properties.setProperty("serverAddr", remoteAddress);
properties.setProperty("isUseCloudNamespaceParsing", String.valueOf(false));
properties.setProperty("namespace", namespaceSetDataId);
ReadableDataSource<String, List<FlowRule>> ds = new NacosDataSource<>(properties, groupId,
namespace + Constants.FLOW_POSTFIX,
source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
return ds.getProperty();
});