Search in sources :

Example 1 with AbstractDataSourceProperties

use of com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties in project spring-cloud-alibaba by alibaba.

the class SentinelDataSourceHandler method afterSingletonsInstantiated.

@Override
public void afterSingletonsInstantiated() {
    sentinelProperties.getDatasource().forEach((dataSourceName, dataSourceProperties) -> {
        try {
            List<String> validFields = dataSourceProperties.getValidField();
            if (validFields.size() != 1) {
                log.error("[Sentinel Starter] DataSource " + dataSourceName + " multi datasource active and won't loaded: " + dataSourceProperties.getValidField());
                return;
            }
            AbstractDataSourceProperties abstractDataSourceProperties = dataSourceProperties.getValidDataSourceProperties();
            abstractDataSourceProperties.setEnv(env);
            abstractDataSourceProperties.preCheck(dataSourceName);
            registerBean(abstractDataSourceProperties, dataSourceName + "-sentinel-" + validFields.get(0) + "-datasource");
        } catch (Exception e) {
            log.error("[Sentinel Starter] DataSource " + dataSourceName + " build error: " + e.getMessage(), e);
        }
    });
}
Also used : AbstractDataSourceProperties(com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties)

Example 2 with AbstractDataSourceProperties

use of com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties in project spring-cloud-alibaba by alibaba.

the class SentinelDataSourceHandler method registerBean.

private void registerBean(final AbstractDataSourceProperties dataSourceProperties, String dataSourceName) {
    Map<String, Object> propertyMap = Arrays.stream(dataSourceProperties.getClass().getDeclaredFields()).collect(HashMap::new, (m, v) -> {
        try {
            v.setAccessible(true);
            m.put(v.getName(), v.get(dataSourceProperties));
        } catch (IllegalAccessException e) {
            log.error("[Sentinel Starter] DataSource " + dataSourceName + " field: " + v.getName() + " invoke error");
            throw new RuntimeException("[Sentinel Starter] DataSource " + dataSourceName + " field: " + v.getName() + " invoke error", e);
        }
    }, HashMap::putAll);
    propertyMap.put(CONVERTER_CLASS_FIELD, dataSourceProperties.getConverterClass());
    propertyMap.put(DATA_TYPE_FIELD, dataSourceProperties.getDataType());
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(dataSourceProperties.getFactoryBeanName());
    propertyMap.forEach((propertyName, propertyValue) -> {
        Field field = ReflectionUtils.findField(dataSourceProperties.getClass(), propertyName);
        if (null == field) {
            return;
        }
        if (DATA_TYPE_FIELD.equals(propertyName)) {
            String dataType = StringUtils.trimAllWhitespace(propertyValue.toString());
            if (CUSTOM_DATA_TYPE.equals(dataType)) {
                try {
                    if (StringUtils.isEmpty(dataSourceProperties.getConverterClass())) {
                        throw new RuntimeException("[Sentinel Starter] DataSource " + dataSourceName + "dataType is custom, please set converter-class " + "property");
                    }
                    // construct custom Converter with 'converterClass'
                    // configuration and register
                    String customConvertBeanName = "sentinel-" + dataSourceProperties.getConverterClass();
                    if (!this.beanFactory.containsBean(customConvertBeanName)) {
                        this.beanFactory.registerBeanDefinition(customConvertBeanName, BeanDefinitionBuilder.genericBeanDefinition(Class.forName(dataSourceProperties.getConverterClass())).getBeanDefinition());
                    }
                    builder.addPropertyReference("converter", customConvertBeanName);
                } catch (ClassNotFoundException e) {
                    log.error("[Sentinel Starter] DataSource " + dataSourceName + " handle " + dataSourceProperties.getClass().getSimpleName() + " error, class name: " + dataSourceProperties.getConverterClass());
                    throw new RuntimeException("[Sentinel Starter] DataSource " + dataSourceName + " handle " + dataSourceProperties.getClass().getSimpleName() + " error, class name: " + dataSourceProperties.getConverterClass(), e);
                }
            } else {
                if (!dataTypeList.contains(StringUtils.trimAllWhitespace(propertyValue.toString()))) {
                    throw new RuntimeException("[Sentinel Starter] DataSource " + dataSourceName + " dataType: " + propertyValue + " is not support now. please using these types: " + dataTypeList.toString());
                }
                // converter type now support xml or json.
                // The bean name of these converters wrapped by
                // 'sentinel-{converterType}-{ruleType}-converter'
                builder.addPropertyReference("converter", "sentinel-" + propertyValue.toString() + "-" + dataSourceProperties.getRuleType().getName() + "-converter");
            }
        } else if (CONVERTER_CLASS_FIELD.equals(propertyName)) {
            return;
        } else {
            // wired properties
            Optional.ofNullable(propertyValue).ifPresent(v -> builder.addPropertyValue(propertyName, v));
        }
    });
    this.beanFactory.registerBeanDefinition(dataSourceName, builder.getBeanDefinition());
    // init in Spring
    AbstractDataSource newDataSource = (AbstractDataSource) this.beanFactory.getBean(dataSourceName);
    // register property in RuleManager
    dataSourceProperties.postRegister(newDataSource);
}
Also used : Arrays(java.util.Arrays) JsonConverter(com.alibaba.cloud.sentinel.datasource.converter.JsonConverter) Logger(org.slf4j.Logger) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) LoggerFactory(org.slf4j.LoggerFactory) SentinelProperties(com.alibaba.cloud.sentinel.SentinelProperties) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) XmlConverter(com.alibaba.cloud.sentinel.datasource.converter.XmlConverter) SmartInitializingSingleton(org.springframework.beans.factory.SmartInitializingSingleton) AbstractDataSource(com.alibaba.csp.sentinel.datasource.AbstractDataSource) List(java.util.List) ReadableDataSource(com.alibaba.csp.sentinel.datasource.ReadableDataSource) Environment(org.springframework.core.env.Environment) ReflectionUtils(org.springframework.util.ReflectionUtils) Map(java.util.Map) Optional(java.util.Optional) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) AbstractDataSourceProperties(com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties) StringUtils(org.springframework.util.StringUtils) Field(java.lang.reflect.Field) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) HashMap(java.util.HashMap) AbstractDataSource(com.alibaba.csp.sentinel.datasource.AbstractDataSource)

Aggregations

AbstractDataSourceProperties (com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties)2 SentinelProperties (com.alibaba.cloud.sentinel.SentinelProperties)1 JsonConverter (com.alibaba.cloud.sentinel.datasource.converter.JsonConverter)1 XmlConverter (com.alibaba.cloud.sentinel.datasource.converter.XmlConverter)1 AbstractDataSource (com.alibaba.csp.sentinel.datasource.AbstractDataSource)1 ReadableDataSource (com.alibaba.csp.sentinel.datasource.ReadableDataSource)1 Field (java.lang.reflect.Field)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 SmartInitializingSingleton (org.springframework.beans.factory.SmartInitializingSingleton)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 Environment (org.springframework.core.env.Environment)1 ReflectionUtils (org.springframework.util.ReflectionUtils)1 StringUtils (org.springframework.util.StringUtils)1