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

refactor sentinel DataSourceHandler

This commit is contained in:
fangjian0423 2019-01-14 21:00:17 +08:00
parent 4cbd4428fd
commit 992afe54e7
2 changed files with 22 additions and 31 deletions

View File

@ -22,6 +22,7 @@ import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -165,8 +166,9 @@ public class SentinelAutoConfiguration {
} }
@Bean @Bean
public SentinelDataSourceHandler sentinelDataSourceHandler() { public SentinelDataSourceHandler sentinelDataSourceHandler(
return new SentinelDataSourceHandler(); DefaultListableBeanFactory beanFactory) {
return new SentinelDataSourceHandler(beanFactory);
} }
protected static class SentinelConverterConfiguration { protected static class SentinelConverterConfiguration {

View File

@ -1,9 +1,7 @@
package org.springframework.cloud.alibaba.sentinel.custom; package org.springframework.cloud.alibaba.sentinel.custom;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -12,10 +10,10 @@ import java.util.TreeMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.cloud.alibaba.sentinel.SentinelConstants; import org.springframework.cloud.alibaba.sentinel.SentinelConstants;
import org.springframework.cloud.alibaba.sentinel.SentinelProperties; import org.springframework.cloud.alibaba.sentinel.SentinelProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants; import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceConstants;
@ -24,7 +22,6 @@ import org.springframework.cloud.alibaba.sentinel.datasource.config.DataSourcePr
import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties; import org.springframework.cloud.alibaba.sentinel.datasource.config.NacosDataSourceProperties;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter; import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter; import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
import org.springframework.context.event.EventListener;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -44,29 +41,28 @@ import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
* @see JsonConverter * @see JsonConverter
* @see XmlConverter * @see XmlConverter
*/ */
public class SentinelDataSourceHandler { public class SentinelDataSourceHandler implements SmartInitializingSingleton {
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(SentinelDataSourceHandler.class); .getLogger(SentinelDataSourceHandler.class);
private List<String> dataTypeList = Arrays.asList("json", "xml"); private List<String> dataTypeList = Arrays.asList("json", "xml");
private List<String> dataSourceBeanNameList = Collections
.synchronizedList(new ArrayList<>());
private final String DATA_TYPE_FIELD = "dataType"; private final String DATA_TYPE_FIELD = "dataType";
private final String CUSTOM_DATA_TYPE = "custom"; private final String CUSTOM_DATA_TYPE = "custom";
private final String CONVERTER_CLASS_FIELD = "converterClass"; private final String CONVERTER_CLASS_FIELD = "converterClass";
private DefaultListableBeanFactory beanFactory;
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Autowired @Autowired
private SentinelProperties sentinelProperties; private SentinelProperties sentinelProperties;
@EventListener(classes = ApplicationStartedEvent.class) @Override
public void buildDataSource(ApplicationStartedEvent event) throws Exception { public void afterSingletonsInstantiated() {
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) event
.getApplicationContext().getAutowireCapableBeanFactory();
// commercialization // commercialization
if (!StringUtils.isEmpty(System.getProperties() if (!StringUtils.isEmpty(System.getProperties()
.getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) { .getProperty(SentinelDataSourceConstants.NACOS_DATASOURCE_ENDPOINT))) {
@ -101,9 +97,8 @@ public class SentinelDataSourceHandler {
AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties
.getValidDataSourceProperties(); .getValidDataSourceProperties();
abstractDataSourceProperties.preCheck(dataSourceName); abstractDataSourceProperties.preCheck(dataSourceName);
registerBean(beanFactory, abstractDataSourceProperties, registerBean(abstractDataSourceProperties, dataSourceName
dataSourceName + "-sentinel-" + validFields.get(0) + "-sentinel-" + validFields.get(0) + "-datasource");
+ "-datasource");
} }
catch (Exception e) { catch (Exception e) {
logger.error("[Sentinel Starter] DataSource " + dataSourceName logger.error("[Sentinel Starter] DataSource " + dataSourceName
@ -112,8 +107,7 @@ public class SentinelDataSourceHandler {
}); });
} }
private void registerBean(DefaultListableBeanFactory beanFactory, private void registerBean(final AbstractDataSourceProperties dataSourceProperties,
final AbstractDataSourceProperties dataSourceProperties,
String dataSourceName) { String dataSourceName) {
Map<String, Object> propertyMap = Arrays Map<String, Object> propertyMap = Arrays
@ -159,8 +153,8 @@ public class SentinelDataSourceHandler {
// configuration and register // configuration and register
String customConvertBeanName = "sentinel-" String customConvertBeanName = "sentinel-"
+ dataSourceProperties.getConverterClass(); + dataSourceProperties.getConverterClass();
if (!beanFactory.containsBean(customConvertBeanName)) { if (!this.beanFactory.containsBean(customConvertBeanName)) {
beanFactory.registerBeanDefinition(customConvertBeanName, this.beanFactory.registerBeanDefinition(customConvertBeanName,
BeanDefinitionBuilder BeanDefinitionBuilder
.genericBeanDefinition( .genericBeanDefinition(
Class.forName(dataSourceProperties Class.forName(dataSourceProperties
@ -209,9 +203,10 @@ public class SentinelDataSourceHandler {
} }
}); });
beanFactory.registerBeanDefinition(dataSourceName, builder.getBeanDefinition()); this.beanFactory.registerBeanDefinition(dataSourceName,
builder.getBeanDefinition());
// init in Spring // init in Spring
AbstractDataSource newDataSource = (AbstractDataSource) beanFactory AbstractDataSource newDataSource = (AbstractDataSource) this.beanFactory
.getBean(dataSourceName); .getBean(dataSourceName);
logAndCheckRuleType(newDataSource, dataSourceName, logAndCheckRuleType(newDataSource, dataSourceName,
@ -230,7 +225,6 @@ public class SentinelDataSourceHandler {
DegradeRuleManager.register2Property(newDataSource.getProperty()); DegradeRuleManager.register2Property(newDataSource.getProperty());
} }
} }
dataSourceBeanNameList.add(dataSourceName);
} }
private void logAndCheckRuleType(AbstractDataSource dataSource, String dataSourceName, private void logAndCheckRuleType(AbstractDataSource dataSource, String dataSourceName,
@ -278,9 +272,4 @@ public class SentinelDataSourceHandler {
+ ruleClass.getSimpleName() + ">. Class: " + ruleConfig.getClass()); + ruleClass.getSimpleName() + ">. Class: " + ruleConfig.getClass());
} }
} }
public List<String> getDataSourceBeanNameList() {
return dataSourceBeanNameList;
}
} }