Search in sources :

Example 11 with MasterSlaveRuleConfiguration

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);
    }
}
Also used : YamlOrchestrationMasterSlaveRuleConfiguration(io.shardingjdbc.orchestration.yaml.YamlOrchestrationMasterSlaveRuleConfiguration) MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) YamlMasterSlaveRuleConfiguration(io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration) OrchestrationFacade(io.shardingjdbc.orchestration.internal.OrchestrationFacade) ConfigurationService(io.shardingjdbc.orchestration.internal.config.ConfigurationService)

Example 12 with MasterSlaveRuleConfiguration

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);
    }
}
Also used : YamlOrchestrationMasterSlaveRuleConfiguration(io.shardingjdbc.orchestration.yaml.YamlOrchestrationMasterSlaveRuleConfiguration) MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) YamlMasterSlaveRuleConfiguration(io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration) OrchestrationFacade(io.shardingjdbc.orchestration.internal.OrchestrationFacade) ConfigurationService(io.shardingjdbc.orchestration.internal.config.ConfigurationService)

Example 13 with MasterSlaveRuleConfiguration

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;
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) YamlMasterSlaveRuleConfiguration(io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) YamlMasterSlaveRuleConfiguration(io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration) LinkedList(java.util.LinkedList)

Example 14 with MasterSlaveRuleConfiguration

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));
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) Test(org.junit.Test)

Example 15 with MasterSlaveRuleConfiguration

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;
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration)

Aggregations

MasterSlaveRuleConfiguration (io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration)18 Test (org.junit.Test)12 HashMap (java.util.HashMap)7 DataSource (javax.sql.DataSource)7 TestDataSource (io.shardingjdbc.core.fixture.TestDataSource)5 MasterSlaveDataSource (io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)3 YamlMasterSlaveRuleConfiguration (io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration)3 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)2 MasterSlaveConnection (io.shardingjdbc.core.jdbc.core.connection.MasterSlaveConnection)2 OrchestrationFacade (io.shardingjdbc.orchestration.internal.OrchestrationFacade)2 ConfigurationService (io.shardingjdbc.orchestration.internal.config.ConfigurationService)2 YamlOrchestrationMasterSlaveRuleConfiguration (io.shardingjdbc.orchestration.yaml.YamlOrchestrationMasterSlaveRuleConfiguration)2 Connection (java.sql.Connection)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 RoundRobinMasterSlaveLoadBalanceAlgorithm (io.shardingjdbc.core.api.algorithm.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm)1 LinkedList (java.util.LinkedList)1 BeforeClass (org.junit.BeforeClass)1