From 1ce382c1bb0afe3cb9ddd9ef910c28a9f1b6f034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=99=93=E5=85=89?= Date: Mon, 2 Apr 2018 20:28:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=BA=E5=A4=B1=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrchestrationSpringBootConfiguration.java | 4 +++- ...estrationMasterSlaveDataSourceFactory.java | 21 ++++++++++--------- ...rchestrationShardingDataSourceFactory.java | 15 ++++++++----- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/main/java/io/shardingjdbc/orchestration/spring/boot/OrchestrationSpringBootConfiguration.java b/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/main/java/io/shardingjdbc/orchestration/spring/boot/OrchestrationSpringBootConfiguration.java index b938ed87d7..150a49024c 100644 --- a/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/main/java/io/shardingjdbc/orchestration/spring/boot/OrchestrationSpringBootConfiguration.java +++ b/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/main/java/io/shardingjdbc/orchestration/spring/boot/OrchestrationSpringBootConfiguration.java @@ -68,7 +68,9 @@ public class OrchestrationSpringBootConfiguration implements EnvironmentAware { */ @Bean public DataSource dataSource() throws SQLException { - return OrchestrationConfiguration.SHARDING.equals(orchestrationProperties.getType()) + String type = orchestrationProperties.getType(); + Preconditions.checkState(null != type, "Missing the type of datasource configuration in orchestration configuration"); + return OrchestrationConfiguration.SHARDING.equals(type) ? OrchestrationShardingDataSourceFactory.createDataSource(dataSourceMap, shardingProperties.getShardingRuleConfiguration(), shardingProperties.getConfigMap(), shardingProperties.getProps(), orchestrationProperties.getOrchestrationConfiguration()) : OrchestrationMasterSlaveDataSourceFactory.createDataSource(dataSourceMap, diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationMasterSlaveDataSourceFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationMasterSlaveDataSourceFactory.java index d6b57949cd..b30c26a768 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationMasterSlaveDataSourceFactory.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationMasterSlaveDataSourceFactory.java @@ -17,6 +17,7 @@ package io.shardingjdbc.orchestration.api; +import com.google.common.base.Preconditions; import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration; import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration; import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration; @@ -30,11 +31,7 @@ import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import javax.sql.DataSource; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.sql.SQLException; import java.util.Map; @@ -52,7 +49,7 @@ public final class OrchestrationMasterSlaveDataSourceFactory { * * @param dataSourceMap data source map * @param masterSlaveRuleConfig master-slave rule configuration - * @param orchestrationConfig orchestration master-slave configuration + * @param orchestrationConfig orchestration configuration * @param configMap config map * @return master-slave data source * @throws SQLException SQL exception @@ -61,9 +58,11 @@ public final class OrchestrationMasterSlaveDataSourceFactory { final Map dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Map configMap, final OrchestrationConfiguration orchestrationConfig) throws SQLException { OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig); - if (null == masterSlaveRuleConfig) { + if (null == masterSlaveRuleConfig || null == masterSlaveRuleConfig.getMasterDataSourceName()) { ConfigurationService configService = orchestrationFacade.getConfigService(); - return createDataSource(configService.loadDataSourceMap(), configService.loadMasterSlaveRuleConfiguration(), configService.loadMasterSlaveConfigMap(), orchestrationFacade); + final MasterSlaveRuleConfiguration cloudMasterSlaveRuleConfig = configService.loadMasterSlaveRuleConfiguration(); + Preconditions.checkState(null != cloudMasterSlaveRuleConfig, "Missing the master-slave rule configuration on register center"); + return createDataSource(configService.loadDataSourceMap(), cloudMasterSlaveRuleConfig, configService.loadMasterSlaveConfigMap(), orchestrationFacade); } else { return createDataSource(dataSourceMap, masterSlaveRuleConfig, configMap, orchestrationFacade); } @@ -74,7 +73,7 @@ public final class OrchestrationMasterSlaveDataSourceFactory { * * @param dataSourceMap data source map * @param yamlMasterSlaveRuleConfig yaml master-slave rule configuration - * @param orchestrationConfig orchestration master-slave configuration + * @param orchestrationConfig orchestration configuration * @return master-slave data source * @throws SQLException SQL exception */ @@ -83,7 +82,9 @@ public final class OrchestrationMasterSlaveDataSourceFactory { OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig); if (null == yamlMasterSlaveRuleConfig) { ConfigurationService configService = orchestrationFacade.getConfigService(); - return createDataSource(configService.loadDataSourceMap(), configService.loadMasterSlaveRuleConfiguration(), configService.loadMasterSlaveConfigMap(), orchestrationFacade); + final MasterSlaveRuleConfiguration cloudMasterSlaveRuleConfig = configService.loadMasterSlaveRuleConfiguration(); + Preconditions.checkState(null != cloudMasterSlaveRuleConfig, "Missing the master-slave rule configuration on register center"); + return createDataSource(configService.loadDataSourceMap(), cloudMasterSlaveRuleConfig, configService.loadMasterSlaveConfigMap(), orchestrationFacade); } else { return createDataSource(dataSourceMap, yamlMasterSlaveRuleConfig.getMasterSlaveRuleConfiguration(), yamlMasterSlaveRuleConfig.getConfigMap(), orchestrationFacade); } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationShardingDataSourceFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationShardingDataSourceFactory.java index fb115f2e0d..47907ea3fe 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationShardingDataSourceFactory.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestrationShardingDataSourceFactory.java @@ -17,6 +17,7 @@ package io.shardingjdbc.orchestration.api; +import com.google.common.base.Preconditions; import io.shardingjdbc.core.api.MasterSlaveDataSourceFactory; import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration; import io.shardingjdbc.core.api.config.ShardingRuleConfiguration; @@ -57,7 +58,7 @@ public final class OrchestrationShardingDataSourceFactory { * * @param dataSourceMap data source map * @param shardingRuleConfig sharding rule configuration - * @param orchestrationConfig orchestration master-slave configuration + * @param orchestrationConfig orchestration configuration * @param configMap config map * @param props properties for data source * @return sharding data source @@ -67,9 +68,11 @@ public final class OrchestrationShardingDataSourceFactory { final Map dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, final Map configMap, final Properties props, final OrchestrationConfiguration orchestrationConfig) throws SQLException { OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig); - if (null == shardingRuleConfig) { + if (null == shardingRuleConfig || shardingRuleConfig.getTableRuleConfigs().isEmpty()) { ConfigurationService configService = orchestrationFacade.getConfigService(); - return createDataSource(configService.loadDataSourceMap(), configService.loadShardingRuleConfiguration(), configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade); + final ShardingRuleConfiguration cloudShardingRuleConfig = configService.loadShardingRuleConfiguration(); + Preconditions.checkState(null != cloudShardingRuleConfig, "Missing the sharding rule configuration on register center"); + return createDataSource(configService.loadDataSourceMap(), cloudShardingRuleConfig, configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade); } else { return createDataSource(dataSourceMap, shardingRuleConfig, configMap, props, orchestrationFacade); } @@ -80,7 +83,7 @@ public final class OrchestrationShardingDataSourceFactory { * * @param dataSourceMap data source map * @param yamlShardingRuleConfig yaml sharding rule configuration - * @param orchestrationConfig orchestration master-slave configuration + * @param orchestrationConfig orchestration configuration * @return sharding data source * @throws SQLException SQL exception */ @@ -89,7 +92,9 @@ public final class OrchestrationShardingDataSourceFactory { OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig); if (null == yamlShardingRuleConfig) { ConfigurationService configService = orchestrationFacade.getConfigService(); - return createDataSource(configService.loadDataSourceMap(), configService.loadShardingRuleConfiguration(), configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade); + final ShardingRuleConfiguration cloudShardingRuleConfig = configService.loadShardingRuleConfiguration(); + Preconditions.checkState(null != cloudShardingRuleConfig, "Missing the sharding rule configuration on register center"); + return createDataSource(configService.loadDataSourceMap(), cloudShardingRuleConfig, configService.loadShardingConfigMap(), configService.loadShardingProperties(), orchestrationFacade); } else { return createDataSource(dataSourceMap, yamlShardingRuleConfig.getShardingRuleConfiguration(), yamlShardingRuleConfig.getConfigMap(), yamlShardingRuleConfig.getProps(), orchestrationFacade); } -- GitLab