use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class OrchestrationMasterSlaveDataSourceFactory method createDataSource.
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param yamlMasterSlaveRuleConfig yaml master-slave rule configuration
* @param orchestrationConfig orchestration configuration
* @return master-slave data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final YamlMasterSlaveRuleConfiguration yamlMasterSlaveRuleConfig, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig);
if (null == yamlMasterSlaveRuleConfig) {
ConfigurationService configService = orchestrationFacade.getConfigService();
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);
}
}
use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class OrchestrationMasterSlaveDataSourceFactory method createDataSource.
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param masterSlaveRuleConfig master-slave rule configuration
* @param orchestrationConfig orchestration configuration
* @param configMap config map
* @return master-slave data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Map<String, Object> configMap, final OrchestrationConfiguration orchestrationConfig) throws SQLException {
OrchestrationFacade orchestrationFacade = new OrchestrationFacade(orchestrationConfig);
if (null == masterSlaveRuleConfig || null == masterSlaveRuleConfig.getMasterDataSourceName()) {
ConfigurationService configService = orchestrationFacade.getConfigService();
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);
}
}
use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class YamlShardingRuleConfiguration method getShardingRuleConfiguration.
/**
* Get sharding rule configuration.
*
* @return sharding rule configuration
*/
public ShardingRuleConfiguration getShardingRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.setDefaultDataSourceName(defaultDataSourceName);
for (Entry<String, YamlTableRuleConfiguration> entry : tables.entrySet()) {
YamlTableRuleConfiguration tableRuleConfig = entry.getValue();
tableRuleConfig.setLogicTable(entry.getKey());
result.getTableRuleConfigs().add(tableRuleConfig.build());
}
result.getBindingTableGroups().addAll(bindingTables);
if (null != defaultDatabaseStrategy) {
result.setDefaultDatabaseShardingStrategyConfig(defaultDatabaseStrategy.build());
}
if (null != defaultTableStrategy) {
result.setDefaultTableShardingStrategyConfig(defaultTableStrategy.build());
}
if (null != defaultKeyGeneratorClass) {
result.setDefaultKeyGenerator(KeyGeneratorFactory.newInstance(defaultKeyGeneratorClass));
}
Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs = new LinkedList<>();
for (Entry<String, YamlMasterSlaveRuleConfiguration> entry : masterSlaveRules.entrySet()) {
YamlMasterSlaveRuleConfiguration each = entry.getValue();
each.setName(entry.getKey());
masterSlaveRuleConfigs.add(entry.getValue().getMasterSlaveRuleConfiguration());
}
result.setMasterSlaveRuleConfigs(masterSlaveRuleConfigs);
return result;
}
use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class MasterSlaveDataSourceFactoryTest method assertCreateDataSourceForSingleSlave.
@Test
public void assertCreateDataSourceForSingleSlave() throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("master_ds", new TestDataSource("master_ds"));
dataSourceMap.put("slave_ds", new TestDataSource("slave_ds"));
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
assertThat(MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfiguration("logic_ds", "master_ds", Collections.singletonList("slave_ds")), configMap), instanceOf(MasterSlaveDataSource.class));
MatcherAssert.assertThat(ConfigMapContext.getInstance().getMasterSlaveConfig(), is(configMap));
}
use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class DataSourceService method getAvailableShardingRuleConfiguration.
/**
* Get available sharding rule configuration.
*
* @return available sharding rule configuration
*/
public ShardingRuleConfiguration getAvailableShardingRuleConfiguration() {
ShardingRuleConfiguration result = configService.loadShardingRuleConfiguration();
Collection<String> disabledDataSourceNames = getDisabledDataSourceNames();
for (String each : disabledDataSourceNames) {
for (MasterSlaveRuleConfiguration masterSlaveRuleConfig : result.getMasterSlaveRuleConfigs()) {
masterSlaveRuleConfig.getSlaveDataSourceNames().remove(each);
}
}
return result;
}
Aggregations