提交 91ce77d9 编写于 作者: H haocao

Add orchestration spring boot support 3th.

上级 744565c6
......@@ -69,7 +69,7 @@ public final class ShardingDataSourceFactory {
* @param yamlFile yaml file for rule configuration of databases and tables sharding with data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws SQLException IO exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final File yamlFile) throws SQLException, IOException {
return new YamlShardingDataSource(yamlFile);
......@@ -82,7 +82,7 @@ public final class ShardingDataSourceFactory {
* @param yamlFile yaml file for rule configuration of databases and tables sharding without data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws SQLException IO exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws SQLException, IOException {
return new YamlShardingDataSource(dataSourceMap, yamlFile);
......@@ -94,7 +94,7 @@ public final class ShardingDataSourceFactory {
* @param yamlByteArray yaml byte array for rule configuration of databases and tables sharding with data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws SQLException IO exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final byte[] yamlByteArray) throws SQLException, IOException {
return new YamlShardingDataSource(yamlByteArray);
......@@ -107,7 +107,7 @@ public final class ShardingDataSourceFactory {
* @param yamlByteArray yaml byte array for rule configuration of databases and tables sharding without data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws SQLException IO exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws SQLException, IOException {
return new YamlShardingDataSource(dataSourceMap, yamlByteArray);
......
......@@ -54,16 +54,16 @@ public class YamlMasterSlaveDataSource extends MasterSlaveDataSource {
super(unmarshal(yamlByteArray).getMasterSlaveRule(dataSourceMap));
}
private static YamMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException {
private static YamlMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException {
try (
FileInputStream fileInputStream = new FileInputStream(yamlFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
) {
return new Yaml(new Constructor(YamMasterSlaveRuleConfiguration.class)).loadAs(inputStreamReader, YamMasterSlaveRuleConfiguration.class);
return new Yaml(new Constructor(YamlMasterSlaveRuleConfiguration.class)).loadAs(inputStreamReader, YamlMasterSlaveRuleConfiguration.class);
}
}
private static YamMasterSlaveRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException {
return new Yaml(new Constructor(YamMasterSlaveRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamMasterSlaveRuleConfiguration.class);
private static YamlMasterSlaveRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException {
return new Yaml(new Constructor(YamlMasterSlaveRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlMasterSlaveRuleConfiguration.class);
}
}
......@@ -37,7 +37,7 @@ import java.util.Map;
*/
@Getter
@Setter
public class YamMasterSlaveRuleConfiguration {
public class YamlMasterSlaveRuleConfiguration {
private Map<String, DataSource> dataSources = new HashMap<>();
......
......@@ -20,7 +20,7 @@ package io.shardingjdbc.core.yaml.sharding;
import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.yaml.masterslave.YamMasterSlaveRuleConfiguration;
import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
import lombok.Getter;
import lombok.Setter;
......@@ -57,7 +57,7 @@ public class YamlShardingRuleConfiguration {
private String defaultKeyGeneratorClass;
private Map<String, YamMasterSlaveRuleConfiguration> masterSlaveRules = new HashMap<>();
private Map<String, YamlMasterSlaveRuleConfiguration> masterSlaveRules = new HashMap<>();
private Properties props = new Properties();
......@@ -94,7 +94,7 @@ public class YamlShardingRuleConfiguration {
}
result.setDefaultKeyGeneratorClass(defaultKeyGeneratorClass);
Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs = new LinkedList<>();
for (Map.Entry<String, YamMasterSlaveRuleConfiguration> each : masterSlaveRules.entrySet()) {
for (Map.Entry<String, YamlMasterSlaveRuleConfiguration> each : masterSlaveRules.entrySet()) {
MasterSlaveRuleConfiguration config = new MasterSlaveRuleConfiguration();
config.setName(each.getKey());
config.setMasterDataSourceName(each.getValue().getMasterDataSourceName());
......
package io.shardingjdbc.orchestration.spring.boot.masterslave;
import io.shardingjdbc.core.yaml.masterslave.YamMasterSlaveRuleConfiguration;
import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
......@@ -9,5 +9,5 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* @author caohao
*/
@ConfigurationProperties(prefix = "sharding.jdbc.config.masterslave")
public class OrchestrationSpringBootMasterSlaveRuleConfigurationProperties extends YamMasterSlaveRuleConfiguration {
public class OrchestrationSpringBootMasterSlaveRuleConfigurationProperties extends YamlMasterSlaveRuleConfiguration {
}
......@@ -17,7 +17,7 @@
package io.shardingjdbc.orchestration.yaml.masterslave;
import io.shardingjdbc.core.yaml.masterslave.YamMasterSlaveRuleConfiguration;
import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
import io.shardingjdbc.orchestration.reg.base.RegistryCenter;
import lombok.Getter;
import lombok.Setter;
......@@ -29,7 +29,7 @@ import lombok.Setter;
*/
@Getter
@Setter
public class YamOrchesrationMasterSlaveRuleConfiguration extends YamMasterSlaveRuleConfiguration {
public class YamlOrchesrationMasterSlaveRuleConfiguration extends YamlMasterSlaveRuleConfiguration {
private boolean overwrite;
......
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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>
*/
package io.shardingjdbc.orchestration.yaml.masterslave;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
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.sql.SQLException;
import java.util.Collections;
import java.util.Map;
/**
* Orchestration master-slave datasource for yaml.
*
* @author caohao
*/
public class YamlOrchestrationMasterSlaveDataSource extends MasterSlaveDataSource {
public YamlOrchestrationMasterSlaveDataSource(final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).getMasterSlaveRule(Collections.<String, DataSource>emptyMap()));
}
public YamlOrchestrationMasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).getMasterSlaveRule(dataSourceMap));
}
public YamlOrchestrationMasterSlaveDataSource(final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).getMasterSlaveRule(Collections.<String, DataSource>emptyMap()));
}
public YamlOrchestrationMasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).getMasterSlaveRule(dataSourceMap));
}
private static YamlMasterSlaveRuleConfiguration unmarshal(final File yamlFile) throws IOException {
try (
FileInputStream fileInputStream = new FileInputStream(yamlFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
) {
return new Yaml(new Constructor(YamlOrchesrationMasterSlaveRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchesrationMasterSlaveRuleConfiguration.class);
}
}
private static YamlMasterSlaveRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException {
return new Yaml(new Constructor(YamlMasterSlaveRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlMasterSlaveRuleConfiguration.class);
}
}
/*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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>
*/
package io.shardingjdbc.orchestration.yaml.sharding;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.core.yaml.sharding.YamlShardingRuleConfiguration;
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.sql.SQLException;
import java.util.Collections;
import java.util.Map;
/**
* Orchestration sharding datasource for yaml.
*
* @author caohao
*/
public class YamlOrchestrationShardingDataSource extends ShardingDataSource {
public YamlOrchestrationShardingDataSource(final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).getShardingRule(Collections.<String, DataSource>emptyMap()), unmarshal(yamlFile).getProps());
}
public YamlOrchestrationShardingDataSource(final Map<String, DataSource> dataSourceMap, final File yamlFile) throws IOException, SQLException {
super(unmarshal(yamlFile).getShardingRule(dataSourceMap), unmarshal(yamlFile).getProps());
}
public YamlOrchestrationShardingDataSource(final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).getShardingRule(Collections.<String, DataSource>emptyMap()), unmarshal(yamlByteArray).getProps());
}
public YamlOrchestrationShardingDataSource(final Map<String, DataSource> dataSourceMap, final byte[] yamlByteArray) throws IOException, SQLException {
super(unmarshal(yamlByteArray).getShardingRule(dataSourceMap), unmarshal(yamlByteArray).getProps());
}
private static YamlShardingRuleConfiguration unmarshal(final File yamlFile) throws IOException {
try (
FileInputStream fileInputStream = new FileInputStream(yamlFile);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8")
) {
return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(inputStreamReader, YamlOrchestrationShardingRuleConfiguration.class);
}
}
private static YamlShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) throws IOException {
return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlOrchestrationShardingRuleConfiguration.class);
}
}
......@@ -17,17 +17,17 @@
package io.shardingjdbc.orchestration.yaml;
import io.shardingjdbc.orchestration.yaml.masterslave.OrchestrationYamlMasterSlaveIntegrateTest;
import io.shardingjdbc.orchestration.yaml.sharding.OrchestrationYamlShardingIntegrateTest;
import io.shardingjdbc.orchestration.yaml.sharding.OrchestrationYamlShardingWithMasterSlaveIntegrateTest;
import io.shardingjdbc.orchestration.yaml.masterslave.YamlOrchestrationMasterSlaveIntegrateTest;
import io.shardingjdbc.orchestration.yaml.sharding.YamlOrchestrationShardingIntegrateTest;
import io.shardingjdbc.orchestration.yaml.sharding.YamlOrchestrationShardingWithMasterSlaveIntegrateTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
OrchestrationYamlShardingIntegrateTest.class,
OrchestrationYamlMasterSlaveIntegrateTest.class,
OrchestrationYamlShardingWithMasterSlaveIntegrateTest.class
YamlOrchestrationShardingIntegrateTest.class,
YamlOrchestrationMasterSlaveIntegrateTest.class,
YamlOrchestrationShardingWithMasterSlaveIntegrateTest.class
})
public class AllYamlTests {
}
......@@ -20,7 +20,6 @@ package io.shardingjdbc.orchestration.yaml.masterslave;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveDataSource;
import io.shardingjdbc.orchestration.yaml.AbstractYamlDataSourceTest;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
......@@ -39,7 +38,7 @@ import java.util.Collection;
@RunWith(Parameterized.class)
@RequiredArgsConstructor
public class OrchestrationYamlMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest {
public class YamlOrchestrationMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest {
private final String filePath;
......@@ -55,12 +54,12 @@ public class OrchestrationYamlMasterSlaveIntegrateTest extends AbstractYamlDataS
@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(OrchestrationYamlMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
File yamlFile = new File(YamlOrchestrationMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlMasterSlaveDataSource(yamlFile);
dataSource = new YamlOrchestrationMasterSlaveDataSource(yamlFile);
} else {
dataSource = new YamlMasterSlaveDataSource(Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), new Function<String, DataSource>() {
dataSource = new YamlOrchestrationMasterSlaveDataSource(Maps.asMap(Sets.newHashSet("db_master", "db_slave_0", "db_slave_1"), new Function<String, DataSource>() {
@Override
public DataSource apply(final String key) {
return createDataSource(key);
......
......@@ -20,7 +20,6 @@ package io.shardingjdbc.orchestration.yaml.sharding;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import io.shardingjdbc.core.yaml.sharding.YamlShardingDataSource;
import io.shardingjdbc.orchestration.yaml.AbstractYamlDataSourceTest;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
......@@ -39,7 +38,7 @@ import java.util.Collection;
@RunWith(Parameterized.class)
@RequiredArgsConstructor
public class OrchestrationYamlShardingIntegrateTest extends AbstractYamlDataSourceTest {
public class YamlOrchestrationShardingIntegrateTest extends AbstractYamlDataSourceTest {
private final String filePath;
......@@ -57,12 +56,12 @@ public class OrchestrationYamlShardingIntegrateTest extends AbstractYamlDataSour
@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(OrchestrationYamlShardingIntegrateTest.class.getResource(filePath).toURI());
File yamlFile = new File(YamlOrchestrationShardingIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlShardingDataSource(yamlFile);
dataSource = new YamlOrchestrationShardingDataSource(yamlFile);
} else {
dataSource = new YamlShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {
dataSource = new YamlOrchestrationShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {
@Override
public DataSource apply(final String key) {
return createDataSource(key);
......
......@@ -20,7 +20,6 @@ package io.shardingjdbc.orchestration.yaml.sharding;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import io.shardingjdbc.core.yaml.sharding.YamlShardingDataSource;
import io.shardingjdbc.orchestration.yaml.AbstractYamlDataSourceTest;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
......@@ -41,7 +40,7 @@ import java.util.Map;
@RunWith(Parameterized.class)
@RequiredArgsConstructor
public class OrchestrationYamlShardingWithMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest {
public class YamlOrchestrationShardingWithMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest {
private final String filePath;
......@@ -59,10 +58,10 @@ public class OrchestrationYamlShardingWithMasterSlaveIntegrateTest extends Abstr
@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(OrchestrationYamlShardingWithMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
File yamlFile = new File(YamlOrchestrationShardingWithMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlShardingDataSource(yamlFile);
dataSource = new YamlOrchestrationShardingDataSource(yamlFile);
} else {
Map<String, DataSource> dataSourceMap = Maps.asMap(Sets.newHashSet("db0_master", "db0_slave", "db1_master", "db1_slave"), new Function<String, DataSource>() {
@Override
......@@ -74,7 +73,7 @@ public class OrchestrationYamlShardingWithMasterSlaveIntegrateTest extends Abstr
for (Map.Entry<String, DataSource> each : dataSourceMap.entrySet()) {
result.put(each.getKey(), each.getValue());
}
dataSource = new YamlShardingDataSource(result, yamlFile);
dataSource = new YamlOrchestrationShardingDataSource(result, yamlFile);
}
try (Connection conn = dataSource.getConnection();
Statement stm = conn.createStatement()) {
......
package io.shardingjdbc.spring.boot.masterslave;
import io.shardingjdbc.core.yaml.masterslave.YamMasterSlaveRuleConfiguration;
import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
......@@ -9,5 +9,5 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* @author caohao
*/
@ConfigurationProperties(prefix = "sharding.jdbc.config.masterslave")
public class SpringBootMasterSlaveRuleConfigurationProperties extends YamMasterSlaveRuleConfiguration {
public class SpringBootMasterSlaveRuleConfigurationProperties extends YamlMasterSlaveRuleConfiguration {
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册