OrchestrationSpringBootConfiguration.java 4.6 KB
Newer Older
H
haocao 已提交
1
package io.shardingjdbc.orchestration.spring.boot;
H
haocao 已提交
2 3 4 5 6 7

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingjdbc.core.constant.ShardingPropertiesConstant;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import io.shardingjdbc.core.util.DataSourceUtil;
H
haocao 已提交
8 9
import io.shardingjdbc.orchestration.api.OrchestrationMasterSlaveDataSourceFactory;
import io.shardingjdbc.orchestration.api.OrchestrationShardingDataSourceFactory;
10
import io.shardingjdbc.orchestration.spring.boot.masterslave.OrchestrationSpringBootMasterSlaveRuleConfigurationProperties;
H
haocao 已提交
11
import io.shardingjdbc.orchestration.spring.boot.orchestration.OrchestrationSpringBootConfigurationProperties;
12
import io.shardingjdbc.orchestration.spring.boot.sharding.OrchestrationSpringBootShardingRuleConfigurationProperties;
H
haocao 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
H
haocao 已提交
28
 * Orchestration spring boot sharding and master-slave configuration.
H
haocao 已提交
29 30 31 32
 *
 * @author caohao
 */
@Configuration
H
haocao 已提交
33 34
@EnableConfigurationProperties({OrchestrationSpringBootConfigurationProperties.class, OrchestrationSpringBootShardingRuleConfigurationProperties.class, 
    OrchestrationSpringBootMasterSlaveRuleConfigurationProperties.class})
35
public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
H
haocao 已提交
36
    
H
haocao 已提交
37 38 39
    @Autowired
    private OrchestrationSpringBootConfigurationProperties orchestrationProperties;
    
H
haocao 已提交
40
    @Autowired
41
    private OrchestrationSpringBootShardingRuleConfigurationProperties shardingProperties;
H
haocao 已提交
42 43
    
    @Autowired
44
    private OrchestrationSpringBootMasterSlaveRuleConfigurationProperties masterSlaveProperties;
H
haocao 已提交
45 46 47 48 49 50 51
    
    private final Map<String, DataSource> dataSourceMap = new HashMap<>();
    
    private final Properties props = new Properties();
    
    @Bean
    public DataSource dataSource() throws SQLException {
H
haocao 已提交
52 53 54 55 56
        return null == masterSlaveProperties.getMasterDataSourceName() 
                ? OrchestrationShardingDataSourceFactory.createDataSource(dataSourceMap, 
                        shardingProperties.getShardingRuleConfiguration(), orchestrationProperties.getOrchestrationConfiguration())
                : OrchestrationMasterSlaveDataSourceFactory.createDataSource(dataSourceMap, 
                        masterSlaveProperties.getMasterSlaveRuleConfiguration(), orchestrationProperties.getOrchestrationConfiguration());
H
haocao 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }
    
    @Override
    public void setEnvironment(final Environment environment) {
        setDataSourceMap(environment);
        setShardingProperties(environment);
    }
    
    private void setDataSourceMap(final Environment environment) {
        RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "sharding.jdbc.datasource.");
        String dataSources = propertyResolver.getProperty("names");
        for (String each : dataSources.split(",")) {
            try {
                Map<String, Object> dataSourceProps = propertyResolver.getSubProperties(each + ".");
                Preconditions.checkState(!dataSourceProps.isEmpty(), "Wrong datasource properties!");
                DataSource dataSource = DataSourceUtil.getDataSource(dataSourceProps.get("type").toString(), dataSourceProps);
                dataSourceMap.put(each, dataSource);
            } catch (final ReflectiveOperationException ex) {
                throw new ShardingJdbcException("Can't find datasource type!", ex);
            }
        }
    }
    
    private void setShardingProperties(final Environment environment) {
        RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "sharding.jdbc.config.sharding.props.");
        String showSQL = propertyResolver.getProperty(ShardingPropertiesConstant.SQL_SHOW.getKey());
        if (!Strings.isNullOrEmpty(showSQL)) {
            props.setProperty(ShardingPropertiesConstant.SQL_SHOW.getKey(), showSQL);
        }
        String executorSize = propertyResolver.getProperty(ShardingPropertiesConstant.EXECUTOR_SIZE.getKey());
        if (!Strings.isNullOrEmpty(executorSize)) {
            props.setProperty(ShardingPropertiesConstant.EXECUTOR_SIZE.getKey(), executorSize);
        }
    }
}