1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

update sentinel version to 0.2.0

This commit is contained in:
flystar32 2018-10-11 22:03:26 +08:00
parent b9710ada46
commit c8d911e89b
9 changed files with 307 additions and 305 deletions

View File

@ -16,7 +16,7 @@
<description>Spring Cloud Alibaba Dependencies</description> <description>Spring Cloud Alibaba Dependencies</description>
<properties> <properties>
<sentinel.version>0.1.1</sentinel.version> <sentinel.version>0.2.0</sentinel.version>
<oss.version>3.1.0</oss.version> <oss.version>3.1.0</oss.version>
<nacos.version>0.2.1</nacos.version> <nacos.version>0.2.1</nacos.version>
</properties> </properties>

View File

@ -2,7 +2,7 @@ package org.springframework.cloud.alibaba.cloud.examples;
import java.util.List; import java.util.List;
import com.alibaba.csp.sentinel.datasource.ConfigParser; import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
@ -10,9 +10,10 @@ import com.alibaba.fastjson.TypeReference;
/** /**
* @author fangjian * @author fangjian
*/ */
public class JsonFlowRuleListParser implements ConfigParser<String, List<FlowRule>> { public class JsonFlowRuleListParser implements Converter<String, List<FlowRule>> {
@Override @Override
public List<FlowRule> parse(String source) { public List<FlowRule> convert(String source) {
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}); return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
});
} }
} }

View File

@ -6,7 +6,7 @@ import org.springframework.cloud.alibaba.sentinel.annotation.SentinelProtect;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.alibaba.csp.sentinel.datasource.ConfigParser; import com.alibaba.csp.sentinel.datasource.Converter;
/** /**
* @author xiaojing * @author xiaojing
@ -26,7 +26,7 @@ public class ServiceApplication {
} }
@Bean @Bean
public ConfigParser myParser() { public Converter myParser() {
return new JsonFlowRuleListParser(); return new JsonFlowRuleListParser();
} }

View File

@ -34,118 +34,119 @@ import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import com.alibaba.csp.sentinel.datasource.DataSource; import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
/** /**
* {@link DataSource} Loader * {@link ReadableDataSource} Loader
* *
* @author fangjian * @author fangjian
*/ */
public class DataSourceLoader { public class DataSourceLoader {
private static final Logger logger = LoggerFactory.getLogger(DataSourceLoader.class); private static final Logger logger = LoggerFactory.getLogger(DataSourceLoader.class);
private final static String PROPERTIES_RESOURCE_LOCATION = "META-INF/sentinel-datasource.properties"; private final static String PROPERTIES_RESOURCE_LOCATION = "META-INF/sentinel-datasource.properties";
private final static String ALL_PROPERTIES_RESOURCES_LOCATION = CLASSPATH_ALL_URL_PREFIX private final static String ALL_PROPERTIES_RESOURCES_LOCATION = CLASSPATH_ALL_URL_PREFIX
+ PROPERTIES_RESOURCE_LOCATION; + PROPERTIES_RESOURCE_LOCATION;
private final static ConcurrentMap<String, Class<? extends DataSource>> dataSourceClassesCache = new ConcurrentHashMap<String, Class<? extends DataSource>>( private final static ConcurrentMap<String, Class<? extends ReadableDataSource>> dataSourceClassesCache = new ConcurrentHashMap<String, Class<? extends ReadableDataSource>>(
4); 4);
static void loadAllDataSourceClassesCache() { static void loadAllDataSourceClassesCache() {
Map<String, Class<? extends DataSource>> dataSourceClassesMap = loadAllDataSourceClassesCache( Map<String, Class<? extends ReadableDataSource>> dataSourceClassesMap = loadAllDataSourceClassesCache(
ALL_PROPERTIES_RESOURCES_LOCATION); ALL_PROPERTIES_RESOURCES_LOCATION);
dataSourceClassesCache.putAll(dataSourceClassesMap); dataSourceClassesCache.putAll(dataSourceClassesMap);
} }
static Map<String, Class<? extends DataSource>> loadAllDataSourceClassesCache( static Map<String, Class<? extends ReadableDataSource>> loadAllDataSourceClassesCache(
String resourcesLocation) { String resourcesLocation) {
Map<String, Class<? extends DataSource>> dataSourcesMap = new HashMap<String, Class<? extends DataSource>>( Map<String, Class<? extends ReadableDataSource>> dataSourcesMap = new HashMap<String, Class<? extends ReadableDataSource>>(
4); 4);
ClassLoader classLoader = DataSourceLoader.class.getClassLoader(); ClassLoader classLoader = DataSourceLoader.class.getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try { try {
Resource[] resources = resolver.getResources(resourcesLocation); Resource[] resources = resolver.getResources(resourcesLocation);
for (Resource resource : resources) { for (Resource resource : resources) {
if (resource.exists()) { if (resource.exists()) {
Properties properties = PropertiesLoaderUtils Properties properties = PropertiesLoaderUtils
.loadProperties(resource); .loadProperties(resource);
for (Map.Entry<Object, Object> entry : properties.entrySet()) { for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String type = (String) entry.getKey(); String type = (String) entry.getKey();
String className = (String) entry.getValue(); String className = (String) entry.getValue();
if (!ClassUtils.isPresent(className, classLoader)) { if (!ClassUtils.isPresent(className, classLoader)) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug( logger.debug(
"Sentinel DataSource implementation [ type : " "Sentinel DataSource implementation [ type : "
+ type + ": , class : " + className + type + ": , class : " + className
+ " , url : " + resource.getURL() + " , url : " + resource.getURL()
+ "] was not present in current classpath , " + "] was not present in current classpath , "
+ "thus loading will be ignored , please add dependency if required !"); + "thus loading will be ignored , please add dependency if required !");
} }
continue; continue;
} }
Assert.isTrue(!dataSourcesMap.containsKey(type), Assert.isTrue(!dataSourcesMap.containsKey(type),
"The duplicated type[" + type "The duplicated type[" + type
+ "] of SentinelDataSource were found in " + "] of SentinelDataSource were found in "
+ "resource [" + resource.getURL() + "]"); + "resource [" + resource.getURL() + "]");
Class<?> dataSourceClass = ClassUtils.resolveClassName(className, Class<?> dataSourceClass = ClassUtils.resolveClassName(className,
classLoader); classLoader);
Assert.isAssignable(DataSource.class, dataSourceClass); Assert.isAssignable(ReadableDataSource.class, dataSourceClass);
dataSourcesMap.put(type, dataSourcesMap.put(type,
(Class<? extends DataSource>) dataSourceClass); (Class<? extends ReadableDataSource>) dataSourceClass);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Sentinel DataSource implementation [ type : " logger.debug("Sentinel DataSource implementation [ type : "
+ type + ": , class : " + className + type + ": , class : " + className
+ "] was loaded."); + "] was loaded.");
} }
} }
} }
} }
} }
catch (IOException e) { catch (IOException e) {
if (logger.isErrorEnabled()) { if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
} }
return dataSourcesMap; return dataSourcesMap;
} }
public static Class<? extends DataSource> loadClass(String type) public static Class<? extends ReadableDataSource> loadClass(String type)
throws IllegalArgumentException { throws IllegalArgumentException {
Class<? extends DataSource> dataSourceClass = dataSourceClassesCache.get(type); Class<? extends ReadableDataSource> dataSourceClass = dataSourceClassesCache
.get(type);
if (dataSourceClass == null) { if (dataSourceClass == null) {
if (dataSourceClassesCache.isEmpty()) { if (dataSourceClassesCache.isEmpty()) {
loadAllDataSourceClassesCache(); loadAllDataSourceClassesCache();
dataSourceClass = dataSourceClassesCache.get(type); dataSourceClass = dataSourceClassesCache.get(type);
} }
} }
if (dataSourceClass == null) { if (dataSourceClass == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Sentinel DataSource implementation [ type : " + type "Sentinel DataSource implementation [ type : " + type
+ " ] can't be found!"); + " ] can't be found!");
} }
return dataSourceClass; return dataSourceClass;
} }
} }

View File

@ -44,174 +44,174 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.datasource.ConfigParser; import com.alibaba.csp.sentinel.datasource.Converter;
/** /**
* {@link SentinelDataSource @SentinelDataSource} Post Processor * {@link SentinelDataSource @SentinelDataSource} Post Processor
* *
* @author fangjian * @author fangjian
* @see com.alibaba.csp.sentinel.datasource.DataSource * @see com.alibaba.csp.sentinel.datasource.ReadableDataSource
* @see SentinelDataSource * @see SentinelDataSource
*/ */
public class SentinelDataSourcePostProcessor public class SentinelDataSourcePostProcessor
extends InstantiationAwareBeanPostProcessorAdapter extends InstantiationAwareBeanPostProcessorAdapter
implements MergedBeanDefinitionPostProcessor { implements MergedBeanDefinitionPostProcessor {
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(SentinelDataSourcePostProcessor.class); .getLogger(SentinelDataSourcePostProcessor.class);
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Autowired @Autowired
private ConfigurableEnvironment environment; private ConfigurableEnvironment environment;
private final Map<String, List<SentinelDataSourceField>> dataSourceFieldCache = new ConcurrentHashMap<>( private final Map<String, List<SentinelDataSourceField>> dataSourceFieldCache = new ConcurrentHashMap<>(
64); 64);
@Override @Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
Class<?> beanType, final String beanName) { Class<?> beanType, final String beanName) {
// find all fields using by @SentinelDataSource annotation // find all fields using by @SentinelDataSource annotation
ReflectionUtils.doWithFields(beanType, new ReflectionUtils.FieldCallback() { ReflectionUtils.doWithFields(beanType, new ReflectionUtils.FieldCallback() {
@Override @Override
public void doWith(Field field) public void doWith(Field field)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
SentinelDataSource annotation = getAnnotation(field, SentinelDataSource annotation = getAnnotation(field,
SentinelDataSource.class); SentinelDataSource.class);
if (annotation != null) { if (annotation != null) {
if (Modifier.isStatic(field.getModifiers())) { if (Modifier.isStatic(field.getModifiers())) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn( logger.warn(
"@SentinelDataSource annotation is not supported on static fields: " "@SentinelDataSource annotation is not supported on static fields: "
+ field); + field);
} }
return; return;
} }
if (dataSourceFieldCache.containsKey(beanName)) { if (dataSourceFieldCache.containsKey(beanName)) {
dataSourceFieldCache.get(beanName) dataSourceFieldCache.get(beanName)
.add(new SentinelDataSourceField(annotation, field)); .add(new SentinelDataSourceField(annotation, field));
} }
else { else {
List<SentinelDataSourceField> list = new ArrayList<>(); List<SentinelDataSourceField> list = new ArrayList<>();
list.add(new SentinelDataSourceField(annotation, field)); list.add(new SentinelDataSourceField(annotation, field));
dataSourceFieldCache.put(beanName, list); dataSourceFieldCache.put(beanName, list);
} }
} }
} }
}); });
} }
@Override @Override
public PropertyValues postProcessPropertyValues(PropertyValues pvs, public PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, Object bean, String beanName) PropertyDescriptor[] pds, Object bean, String beanName)
throws BeanCreationException { throws BeanCreationException {
if (dataSourceFieldCache.containsKey(beanName)) { if (dataSourceFieldCache.containsKey(beanName)) {
List<SentinelDataSourceField> sentinelDataSourceFields = dataSourceFieldCache List<SentinelDataSourceField> sentinelDataSourceFields = dataSourceFieldCache
.get(beanName); .get(beanName);
for(SentinelDataSourceField sentinelDataSourceField : sentinelDataSourceFields) { for (SentinelDataSourceField sentinelDataSourceField : sentinelDataSourceFields) {
try { try {
// construct DataSource field annotated by @SentinelDataSource // construct DataSource field annotated by @SentinelDataSource
Field field = sentinelDataSourceField.getField(); Field field = sentinelDataSourceField.getField();
ReflectionUtils.makeAccessible(field); ReflectionUtils.makeAccessible(field);
String dataSourceBeanName = constructDataSource( String dataSourceBeanName = constructDataSource(
sentinelDataSourceField.getSentinelDataSource()); sentinelDataSourceField.getSentinelDataSource());
field.set(bean, applicationContext.getBean(dataSourceBeanName)); field.set(bean, applicationContext.getBean(dataSourceBeanName));
} }
catch (IllegalAccessException e) { catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
return pvs; return pvs;
} }
private String constructDataSource(SentinelDataSource annotation) { private String constructDataSource(SentinelDataSource annotation) {
String prefix = annotation.value(); String prefix = annotation.value();
if (StringUtils.isEmpty(prefix)) { if (StringUtils.isEmpty(prefix)) {
prefix = SentinelConstants.PROPERTY_DATASOURCE_PREFIX; prefix = SentinelConstants.PROPERTY_DATASOURCE_PREFIX;
} }
Map<String, Object> propertyMap = PropertySourcesUtils Map<String, Object> propertyMap = PropertySourcesUtils
.getSubProperties(environment.getPropertySources(), prefix); .getSubProperties(environment.getPropertySources(), prefix);
String alias = propertyMap.get("type").toString(); String alias = propertyMap.get("type").toString();
Class dataSourceClass = DataSourceLoader.loadClass(alias); Class dataSourceClass = DataSourceLoader.loadClass(alias);
String beanName = StringUtils.isEmpty(annotation.name()) String beanName = StringUtils.isEmpty(annotation.name())
? StringUtils.uncapitalize(dataSourceClass.getSimpleName()) + "_" + prefix ? StringUtils.uncapitalize(dataSourceClass.getSimpleName()) + "_" + prefix
: annotation.name(); : annotation.name();
if (applicationContext.containsBean(beanName)) { if (applicationContext.containsBean(beanName)) {
return beanName; return beanName;
} }
Class targetClass = null; Class targetClass = null;
// if alias exists in SentinelDataSourceRegistry, wired properties into // if alias exists in SentinelDataSourceRegistry, wired properties into
// FactoryBean // FactoryBean
if (SentinelDataSourceRegistry.checkFactoryBean(alias)) { if (SentinelDataSourceRegistry.checkFactoryBean(alias)) {
targetClass = SentinelDataSourceRegistry.getFactoryBean(alias); targetClass = SentinelDataSourceRegistry.getFactoryBean(alias);
} }
else { else {
// if alias not exists in SentinelDataSourceRegistry, wired properties into // if alias not exists in SentinelDataSourceRegistry, wired properties into
// raw class // raw class
targetClass = dataSourceClass; targetClass = dataSourceClass;
} }
registerDataSource(beanName, targetClass, propertyMap); registerDataSource(beanName, targetClass, propertyMap);
return beanName; return beanName;
} }
private void registerDataSource(String beanName, Class targetClass, private void registerDataSource(String beanName, Class targetClass,
Map<String, Object> propertyMap) { Map<String, Object> propertyMap) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition(targetClass); .genericBeanDefinition(targetClass);
for (String propertyName : propertyMap.keySet()) { for (String propertyName : propertyMap.keySet()) {
Field field = ReflectionUtils.findField(targetClass, propertyName); Field field = ReflectionUtils.findField(targetClass, propertyName);
if (field != null) { if (field != null) {
if (field.getType().isAssignableFrom(ConfigParser.class)) { if (field.getType().isAssignableFrom(Converter.class)) {
// ConfigParser get from ApplicationContext // Converter get from ApplicationContext
builder.addPropertyReference(propertyName, builder.addPropertyReference(propertyName,
propertyMap.get(propertyName).toString()); propertyMap.get(propertyName).toString());
} }
else { else {
// wired properties // wired properties
builder.addPropertyValue(propertyName, propertyMap.get(propertyName)); builder.addPropertyValue(propertyName, propertyMap.get(propertyName));
} }
} }
} }
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
.getAutowireCapableBeanFactory(); .getAutowireCapableBeanFactory();
beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition()); beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition());
} }
class SentinelDataSourceField { class SentinelDataSourceField {
private SentinelDataSource sentinelDataSource; private SentinelDataSource sentinelDataSource;
private Field field; private Field field;
public SentinelDataSourceField(SentinelDataSource sentinelDataSource, public SentinelDataSourceField(SentinelDataSource sentinelDataSource,
Field field) { Field field) {
this.sentinelDataSource = sentinelDataSource; this.sentinelDataSource = sentinelDataSource;
this.field = field; this.field = field;
} }
public SentinelDataSource getSentinelDataSource() { public SentinelDataSource getSentinelDataSource() {
return sentinelDataSource; return sentinelDataSource;
} }
public void setSentinelDataSource(SentinelDataSource sentinelDataSource) { public void setSentinelDataSource(SentinelDataSource sentinelDataSource) {
this.sentinelDataSource = sentinelDataSource; this.sentinelDataSource = sentinelDataSource;
} }
public Field getField() { public Field getField() {
return field; return field;
} }
public void setField(Field field) { public void setField(Field field) {
this.field = field; this.field = field;
} }
} }
} }

View File

@ -2,7 +2,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import com.alibaba.csp.sentinel.datasource.ConfigParser; import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource; import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource;
/** /**
@ -14,12 +14,12 @@ public class ApolloDataSourceFactoryBean implements FactoryBean<ApolloDataSource
private String namespaceName; private String namespaceName;
private String flowRulesKey; private String flowRulesKey;
private String defaultFlowRuleValue; private String defaultFlowRuleValue;
private ConfigParser configParser; private Converter converter;
@Override @Override
public ApolloDataSource getObject() throws Exception { public ApolloDataSource getObject() throws Exception {
return new ApolloDataSource(namespaceName, flowRulesKey, defaultFlowRuleValue, return new ApolloDataSource(namespaceName, flowRulesKey, defaultFlowRuleValue,
configParser); converter);
} }
@Override @Override
@ -56,11 +56,11 @@ public class ApolloDataSourceFactoryBean implements FactoryBean<ApolloDataSource
this.defaultFlowRuleValue = defaultFlowRuleValue; this.defaultFlowRuleValue = defaultFlowRuleValue;
} }
public ConfigParser getConfigParser() { public Converter getConverter() {
return configParser; return converter;
} }
public void setConfigParser(ConfigParser configParser) { public void setConverter(Converter converter) {
this.configParser = configParser; this.converter = converter;
} }
} }

View File

@ -5,7 +5,7 @@ import java.nio.charset.Charset;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import com.alibaba.csp.sentinel.datasource.ConfigParser; import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource; import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
/** /**
@ -18,11 +18,11 @@ public class FileRefreshableDataSourceFactoryBean implements FactoryBean<FileRef
private String charset; private String charset;
private long recommendRefreshMs; private long recommendRefreshMs;
private int bufSize; private int bufSize;
private ConfigParser configParser; private Converter converter;
@Override @Override
public FileRefreshableDataSource getObject() throws Exception { public FileRefreshableDataSource getObject() throws Exception {
return new FileRefreshableDataSource(new File(file), configParser, return new FileRefreshableDataSource(new File(file), converter,
recommendRefreshMs, bufSize, Charset.forName(charset)); recommendRefreshMs, bufSize, Charset.forName(charset));
} }
@ -68,11 +68,11 @@ public class FileRefreshableDataSourceFactoryBean implements FactoryBean<FileRef
this.bufSize = bufSize; this.bufSize = bufSize;
} }
public ConfigParser getConfigParser() { public Converter getConverter() {
return configParser; return converter;
} }
public void setConfigParser(ConfigParser configParser) { public void setConverter(Converter converter) {
this.configParser = configParser; this.converter = converter;
} }
} }

View File

@ -2,7 +2,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import com.alibaba.csp.sentinel.datasource.ConfigParser; import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource; import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
/** /**
@ -14,11 +14,11 @@ public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource>
private String serverAddr; private String serverAddr;
private String groupId; private String groupId;
private String dataId; private String dataId;
private ConfigParser configParser; private Converter converter;
@Override @Override
public NacosDataSource getObject() throws Exception { public NacosDataSource getObject() throws Exception {
return new NacosDataSource(serverAddr, groupId, dataId, configParser); return new NacosDataSource(serverAddr, groupId, dataId, converter);
} }
@Override @Override
@ -55,11 +55,11 @@ public class NacosDataSourceFactoryBean implements FactoryBean<NacosDataSource>
this.dataId = dataId; this.dataId = dataId;
} }
public ConfigParser getConfigParser() { public Converter getConverter() {
return configParser; return converter;
} }
public void setConfigParser(ConfigParser configParser) { public void setConverter(Converter converter) {
this.configParser = configParser; this.converter = converter;
} }
} }

View File

@ -3,7 +3,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource.factorybean;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import com.alibaba.csp.sentinel.datasource.ConfigParser; import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource; import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
/** /**
@ -12,74 +12,74 @@ import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource;
*/ */
public class ZookeeperDataSourceFactoryBean implements FactoryBean<ZookeeperDataSource> { public class ZookeeperDataSourceFactoryBean implements FactoryBean<ZookeeperDataSource> {
private String serverAddr; private String serverAddr;
private String path; private String path;
private String groupId; private String groupId;
private String dataId; private String dataId;
private ConfigParser configParser; private Converter converter;
@Override @Override
public ZookeeperDataSource getObject() throws Exception { public ZookeeperDataSource getObject() throws Exception {
if (StringUtils.isNotEmpty(groupId) && StringUtils.isNotEmpty(dataId)) { if (StringUtils.isNotEmpty(groupId) && StringUtils.isNotEmpty(dataId)) {
// the path will be /{groupId}/{dataId} // the path will be /{groupId}/{dataId}
return new ZookeeperDataSource(serverAddr, groupId, dataId, configParser); return new ZookeeperDataSource(serverAddr, groupId, dataId, converter);
} }
else { else {
// using path directly // using path directly
return new ZookeeperDataSource(serverAddr, path, configParser); return new ZookeeperDataSource(serverAddr, path, converter);
} }
} }
@Override @Override
public Class<?> getObjectType() { public Class<?> getObjectType() {
return ZookeeperDataSource.class; return ZookeeperDataSource.class;
} }
@Override @Override
public boolean isSingleton() { public boolean isSingleton() {
return true; return true;
} }
public String getServerAddr() { public String getServerAddr() {
return serverAddr; return serverAddr;
} }
public void setServerAddr(String serverAddr) { public void setServerAddr(String serverAddr) {
this.serverAddr = serverAddr; this.serverAddr = serverAddr;
} }
public String getPath() { public String getPath() {
return path; return path;
} }
public void setPath(String path) { public void setPath(String path) {
this.path = path; this.path = path;
} }
public String getGroupId() { public String getGroupId() {
return groupId; return groupId;
} }
public void setGroupId(String groupId) { public void setGroupId(String groupId) {
this.groupId = groupId; this.groupId = groupId;
} }
public String getDataId() { public String getDataId() {
return dataId; return dataId;
} }
public void setDataId(String dataId) { public void setDataId(String dataId) {
this.dataId = dataId; this.dataId = dataId;
} }
public ConfigParser getConfigParser() { public Converter getConverter() {
return configParser; return converter;
} }
public void setConfigParser(ConfigParser configParser) { public void setConverter(Converter converter) {
this.configParser = configParser; this.converter = converter;
} }
} }