import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosConfigUtil;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.List;
import java.util.Properties;
public class ApolloConverter<T> implements Converter<List<T>> {
private String appName;
private String nacosServerAddr;
private String groupId;
private String dataIdPostfix;
private Class<T> clazz;
public ApolloConverter(String appName, String nacosServerAddr, String groupId,
String dataIdPostfix, Class<T> clazz) {
this.appName = appName;
this.nacosServerAddr = nacosServerAddr;
this.groupId = groupId;
this.dataIdPostfix = dataIdPostfix;
this.clazz = clazz;
}
@Override
public void configure(String namespace, Properties properties) {
// 配置中心的配置变更可以通过此方法获取并应用
}
@Override
public List<T> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<T>>(){});
}
@Override
public String convert(List<T> target) {
return JSON.toJSONString(target);
}
public static void main(String[] args) {
// 示例:同步规则到Apollo
Properties properties = new Properties();
properties.put("appName", "sentinel-dashboard");
properties.put("nacosServerAddr", "127.0.0.1:8848");
properties.put("groupId", "DEFAULT_GROUP");
properties.put("dataIdPostfix", "sentinelRules");
properties.put("clazz", "com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity");
// 初始化转换器
ApolloConverter converter = new ApolloConverter(
properties.getProperty("appName"),
properties.getProperty("nacosServerAddr"),
properties.getProperty("groupId"),
properties.getProperty("dataIdPostfix"),
(Class<Object>) Class.forName(properties.getProperty("clazz"))
);
// 获取Nacos配置服务
评论已关闭