OrchestrationSpringBootConfiguration.java 4.6 KB
Newer Older
H
haocao 已提交
1 2 3 4 5 6
/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
T
terrymanu 已提交
7 8 9
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
H
haocao 已提交
10 11 12 13 14 15 16 17
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

H
haocao 已提交
18
package io.shardingjdbc.orchestration.spring.boot;
H
haocao 已提交
19 20 21 22

import com.google.common.base.Preconditions;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import io.shardingjdbc.core.util.DataSourceUtil;
H
haocao 已提交
23 24
import io.shardingjdbc.orchestration.api.OrchestrationMasterSlaveDataSourceFactory;
import io.shardingjdbc.orchestration.api.OrchestrationShardingDataSourceFactory;
25
import io.shardingjdbc.orchestration.spring.boot.masterslave.SpringBootMasterSlaveRuleConfigurationProperties;
26
import io.shardingjdbc.orchestration.spring.boot.orchestration.SpringBootOrchestrationConfigurationProperties;
27
import io.shardingjdbc.orchestration.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties;
H
haocao 已提交
28 29 30 31 32 33 34
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;
35
import org.springframework.util.StringUtils;
H
haocao 已提交
36 37 38

import javax.sql.DataSource;
import java.sql.SQLException;
39
import java.util.LinkedHashMap;
H
haocao 已提交
40 41 42
import java.util.Map;

/**
H
haocao 已提交
43
 * Orchestration spring boot sharding and master-slave configuration.
H
haocao 已提交
44 45 46 47
 *
 * @author caohao
 */
@Configuration
48
@EnableConfigurationProperties({SpringBootShardingRuleConfigurationProperties.class, SpringBootMasterSlaveRuleConfigurationProperties.class, SpringBootOrchestrationConfigurationProperties.class})
49
public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
H
haocao 已提交
50
    
51
    private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();
T
terrymanu 已提交
52
    
H
haocao 已提交
53
    @Autowired
54
    private SpringBootShardingRuleConfigurationProperties shardingProperties;
T
terrymanu 已提交
55
    
H
haocao 已提交
56
    @Autowired
57
    private SpringBootMasterSlaveRuleConfigurationProperties masterSlaveProperties;
T
terrymanu 已提交
58
    
59 60
    @Autowired
    private SpringBootOrchestrationConfigurationProperties orchestrationProperties;
T
terrymanu 已提交
61
    
62 63 64 65 66 67
    /**
     * Get data source bean.
     * 
     * @return data source bean
     * @throws SQLException SQL Exception
     */
H
haocao 已提交
68 69
    @Bean
    public DataSource dataSource() throws SQLException {
H
haocao 已提交
70 71
        return null == masterSlaveProperties.getMasterDataSourceName() 
                ? OrchestrationShardingDataSourceFactory.createDataSource(dataSourceMap, 
72
                        shardingProperties.getShardingRuleConfiguration(), shardingProperties.getConfigMap(), shardingProperties.getProps(), orchestrationProperties.getOrchestrationConfiguration())
H
haocao 已提交
73
                : OrchestrationMasterSlaveDataSourceFactory.createDataSource(dataSourceMap, 
74
                        masterSlaveProperties.getMasterSlaveRuleConfiguration(), masterSlaveProperties.getConfigMap(), orchestrationProperties.getOrchestrationConfiguration());
H
haocao 已提交
75 76 77 78 79 80 81 82 83 84
    }
    
    @Override
    public void setEnvironment(final Environment environment) {
        setDataSourceMap(environment);
    }
    
    private void setDataSourceMap(final Environment environment) {
        RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment, "sharding.jdbc.datasource.");
        String dataSources = propertyResolver.getProperty("names");
85 86
        Preconditions.checkState(!StringUtils.isEmpty(dataSources), "Wrong datasource properties, empty datasource !");
        dataSources = dataSources.trim();
H
haocao 已提交
87
        for (String each : dataSources.split(",")) {
T
terrymanu 已提交
88 89 90 91 92 93 94 95
            try {
                Map<String, Object> dataSourceProps = propertyResolver.getSubProperties(each + ".");
                Preconditions.checkState(!dataSourceProps.isEmpty(), String.format("Wrong datasource [%s] properties!", each));
                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);
            }
H
haocao 已提交
96 97 98
        }
    }
}