org.zodiac.sentinel.base.support.SentinelDataSourcesContext Maven / Gradle / Ivy
The newest version!
package org.zodiac.sentinel.base.support;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import org.zodiac.sentinel.base.datasource.DataSourceType;
import org.zodiac.sentinel.base.datasource.model.AbstractDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelApolloDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelFileDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelNacosDataSourceInfo;
import org.zodiac.sentinel.base.datasource.model.SentinelRedisDataSourceInfo;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.datasource.redis.RedisDataSource;
public abstract class SentinelDataSourcesContext {
private static Logger log = SmartSlf4jLoggerFactory.getLogger(SentinelDataSourcesContext.class);
private volatile Object apolloDataSourceLock = new Object();
private volatile Object fileDataSourceLock = new Object();
private volatile Object nacosDataSourceLock = new Object();
private volatile Object redisDataSourceLock = new Object();
/*格式:Map。*/
private volatile Map apolloDataSourceMap = new ConcurrentHashMap<>();
/*格式:Map。*/
private volatile Map fileDataSourceMap = new ConcurrentHashMap<>();
/*格式:Map。*/
private volatile Map nacosDataSourceMap = new ConcurrentHashMap<>();
/*格式:Map。*/
private volatile Map redisDataSourceMap = new ConcurrentHashMap<>();
/*格式:Map<{sentinel数据源配置的mapKey-{dataSourceType}}, dataSourceInfo>。*/
private volatile Map apolloDataSourceInfoMap = new ConcurrentHashMap<>();
/*格式:Map<{sentinel数据源配置的mapKey-{dataSourceType}}, dataSourceInfo>。*/
private volatile Map fileDataSourceInfoMap = new ConcurrentHashMap<>();
/*格式:Map<{sentinel数据源配置的mapKey-{dataSourceType}}, dataSourceInfo>。*/
private volatile Map nacosDataSourceInfoMap = new ConcurrentHashMap<>();
/*格式:Map<{sentinel数据源配置的mapKey-{dataSourceType}}, dataSourceInfo>。*/
private volatile Map redisDataSourceInfoMap = new ConcurrentHashMap<>();
private SentinelDataSourcesContext() {
super();
}
public SentinelDataSourcesContext addApolloDataSource(String name, ApolloDataSource apolloDataSource) {
apolloDataSourceMap.putIfAbsent(name, apolloDataSource);
return this;
}
public SentinelDataSourcesContext addFileDataSource(String name, FileRefreshableDataSource fileRefreshableDataSource) {
fileDataSourceMap.putIfAbsent(name, fileRefreshableDataSource);
return this;
}
public SentinelDataSourcesContext addNacosDataSource(String name, NacosDataSource nacosDataSource) {
nacosDataSourceMap.putIfAbsent(name, nacosDataSource);
return this;
}
public SentinelDataSourcesContext addRedisDataSource(String name, RedisDataSource redisDataSource) {
redisDataSourceMap.putIfAbsent(name, redisDataSource);
return this;
}
public ApolloDataSource getApolloDataSource(String name) {
return apolloDataSourceMap.get(name);
}
public FileRefreshableDataSource getFileDataSource(String name) {
return fileDataSourceMap.get(name);
}
public NacosDataSource getNacosDataSource(String name) {
return nacosDataSourceMap.get(name);
}
public RedisDataSource getRedisDataSource(String name) {
return redisDataSourceMap.get(name);
}
public SentinelDataSourcesContext addApolloDataSourceInfo(String dataSourceConfigName,
SentinelApolloDataSourceInfo dataSourceInfo) {
apolloDataSourceInfoMap.putIfAbsent(obtainDataSourceInfoKey(dataSourceConfigName,
dataSourceInfo.getDataSourceType()), dataSourceInfo);
return this;
}
public SentinelDataSourcesContext addFileDataSourceInfo(String dataSourceConfigName,
SentinelFileDataSourceInfo dataSourceInfo) {
fileDataSourceInfoMap.putIfAbsent(obtainDataSourceInfoKey(dataSourceConfigName,
dataSourceInfo.getDataSourceType()), dataSourceInfo);
return this;
}
public SentinelDataSourcesContext addNacosDataSourceInfo(String dataSourceConfigName,
SentinelNacosDataSourceInfo dataSourceInfo) {
nacosDataSourceInfoMap.putIfAbsent(obtainDataSourceInfoKey(dataSourceConfigName,
dataSourceInfo.getDataSourceType()), dataSourceInfo);
return this;
}
public SentinelDataSourcesContext addRedisDataSourceInfo(String dataSourceConfigName,
SentinelRedisDataSourceInfo dataSourceInfo) {
redisDataSourceInfoMap.putIfAbsent(obtainDataSourceInfoKey(dataSourceConfigName,
dataSourceInfo.getDataSourceType()), dataSourceInfo);
return this;
}
public SentinelApolloDataSourceInfo getApolloDataSourceInfo(String dataSourceConfigName) {
return apolloDataSourceInfoMap.get(obtainDataSourceInfoKey(dataSourceConfigName, DataSourceType.apollo));
}
public SentinelFileDataSourceInfo getFileDataSourceInfo(String dataSourceConfigName) {
return fileDataSourceInfoMap.get(obtainDataSourceInfoKey(dataSourceConfigName, DataSourceType.file));
}
public SentinelNacosDataSourceInfo getNacosDataSourceInfo(String dataSourceConfigName) {
return nacosDataSourceInfoMap.get(obtainDataSourceInfoKey(dataSourceConfigName, DataSourceType.nacos));
}
public SentinelRedisDataSourceInfo getRedisDataSourceInfo(String dataSourceConfigName) {
return redisDataSourceInfoMap.get(obtainDataSourceInfoKey(dataSourceConfigName, DataSourceType.redis));
}
private String obtainDataSourceInfoKey(String dataSourceConfigName, DataSourceType type) {
return String.format("%s-%s", dataSourceConfigName, type.name());
}
public static void addDataSource(String dataSourceConfigName, AbstractDataSourceInfo dataSourceInfo,
String name, ReadableDataSource dataSource) {
if (StrUtil.isBlank(name) || null == dataSource) {
return;
}
SentinelDataSourcesContext context = getInstance();
if (dataSource instanceof ApolloDataSource) {
context.addApolloDataSource(name, (ApolloDataSource)dataSource);
} else if (dataSource instanceof FileRefreshableDataSource) {
context.addFileDataSource(name, (FileRefreshableDataSource)dataSource);
} else if (dataSource instanceof NacosDataSource) {
context.addNacosDataSource(name, (NacosDataSource)dataSource);
} else if (dataSource instanceof RedisDataSource) {
context.addRedisDataSource(name, (RedisDataSource)dataSource);
} else {
log.warn("Unsupported sentinel data source implementation '{}' .", dataSource.getClass().getCanonicalName());
}
if (StrUtil.isBlank(dataSourceConfigName) || null != dataSourceInfo) {
return;
}
if (dataSourceInfo instanceof SentinelApolloDataSourceInfo) {
context.addApolloDataSourceInfo(dataSourceConfigName, (SentinelApolloDataSourceInfo)dataSourceInfo);
} else if (dataSourceInfo instanceof SentinelFileDataSourceInfo) {
context.addFileDataSourceInfo(dataSourceConfigName, (SentinelFileDataSourceInfo)dataSourceInfo);
} else if (dataSourceInfo instanceof SentinelNacosDataSourceInfo) {
context.addNacosDataSourceInfo(dataSourceConfigName, (SentinelNacosDataSourceInfo)dataSourceInfo);
} else if (dataSourceInfo instanceof SentinelRedisDataSourceInfo) {
context.addRedisDataSourceInfo(dataSourceConfigName, (SentinelRedisDataSourceInfo)dataSourceInfo);
} else {
log.warn("Unsupported sentinel data source config implementation '{}' .", dataSourceInfo.getClass().getCanonicalName());
}
}
public static ApolloDataSource acquireApolloDataSource(String name) {
return getInstance().getApolloDataSource(name);
}
public static FileRefreshableDataSource acquireFileDataSource(String name) {
return getInstance().getFileDataSource(name);
}
public static NacosDataSource acquireNacosDataSource(String name) {
return getInstance().getNacosDataSource(name);
}
public static RedisDataSource acquireRedisDataSource(String name) {
return getInstance().getRedisDataSource(name);
}
public static SentinelApolloDataSourceInfo acquireApolloDataSourceInfo(String dataSourceConfigName) {
return getInstance().getApolloDataSourceInfo(dataSourceConfigName);
}
public static SentinelFileDataSourceInfo acquireFileDataSourceInfo(String dataSourceConfigName) {
return getInstance().getFileDataSourceInfo(dataSourceConfigName);
}
public static SentinelNacosDataSourceInfo acquireNacosDataSourceInfo(String dataSourceConfigName) {
return getInstance().getNacosDataSourceInfo(dataSourceConfigName);
}
public static SentinelRedisDataSourceInfo acquireRedisDataSourceInfo(String dataSourceConfigName) {
return getInstance().getRedisDataSourceInfo(dataSourceConfigName);
}
private static final SentinelDataSourcesContext getInstance() {
return SentinelDataSourcesHolderHolder.INSTANCE;
}
private static class SentinelDataSourcesHolderHolder {
private static final SentinelDataSourcesContext INSTANCE = new SentinelDataSourcesContext() {};
}
}