Search in sources :

Example 1 with YamlMasterSlaveRuleConfiguration

use of io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration 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 2 with YamlMasterSlaveRuleConfiguration

use of io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration 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 3 with YamlMasterSlaveRuleConfiguration

use of io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.

the class YamlShardingRuleConfigurationTest method createYamlMasterSlaveRuleConfig.

private YamlMasterSlaveRuleConfiguration createYamlMasterSlaveRuleConfig() {
    YamlMasterSlaveRuleConfiguration result = new YamlMasterSlaveRuleConfiguration();
    result.setName("master_slave_ds");
    result.setMasterDataSourceName("master_ds");
    result.setSlaveDataSourceNames(Arrays.asList("slave_ds_0", "slave_ds_1"));
    result.setLoadBalanceAlgorithmType(MasterSlaveLoadBalanceAlgorithmType.ROUND_ROBIN);
    result.setLoadBalanceAlgorithmClassName("TestLoadBalanceAlgorithmClassName");
    return result;
}
Also used : YamlMasterSlaveRuleConfiguration(io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration)

Aggregations

YamlMasterSlaveRuleConfiguration (io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration)3 MasterSlaveRuleConfiguration (io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration)2 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)1 OrchestrationFacade (io.shardingjdbc.orchestration.internal.OrchestrationFacade)1 ConfigurationService (io.shardingjdbc.orchestration.internal.config.ConfigurationService)1 YamlOrchestrationMasterSlaveRuleConfiguration (io.shardingjdbc.orchestration.yaml.YamlOrchestrationMasterSlaveRuleConfiguration)1 LinkedList (java.util.LinkedList)1