diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestratorBuilder.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestratorBuilder.java index b14cead3f619918816baa50fd9e4217e2765ab00..b5b9bf20abc89970ea4a1a815009af69ccfbbc2f 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestratorBuilder.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestratorBuilder.java @@ -1,13 +1,9 @@ package io.shardingjdbc.orchestration.api; import io.shardingjdbc.orchestration.api.config.OrchestratorConfiguration; -import io.shardingjdbc.orchestration.internal.OrchestratorImpl; -import io.shardingjdbc.orchestration.reg.base.*; -import io.shardingjdbc.orchestration.reg.zookeeper.ZookeeperConfiguration; -import io.shardingjdbc.orchestration.reg.zookeeper.ZookeeperRegistryCenter; -import io.shardingjdbc.orchestration.reg.zookeeper.config.ZkConfigurationService; -import io.shardingjdbc.orchestration.reg.zookeeper.state.datasource.ZkDataSourceService; -import io.shardingjdbc.orchestration.reg.zookeeper.state.instance.ZkInstanceStateService; +import io.shardingjdbc.orchestration.reg.zookeeper.ZookeeperOrchestratorFactory; + +import java.util.ServiceLoader; /** * Orchestrator builder @@ -20,6 +16,7 @@ public class OrchestratorBuilder { /** * New orchestrator builder + * * @return OrchestratorBuilder */ public static OrchestratorBuilder newBuilder() { @@ -28,6 +25,7 @@ public class OrchestratorBuilder { /** * with orchestorator configuration + * * @param configuration orchestrator configuration * @return self */ @@ -42,20 +40,14 @@ public class OrchestratorBuilder { * @return orchestrator instance */ public Orchestrator build() { - // TODO build different kind of orchestrator - String name = configuration.getName(); - boolean overwrite = configuration.isOverwrite(); - CoordinatorRegistryCenter registryCenter = setupRegistryCenterIfNeeded(configuration); - ConfigurationService configurationService = new ZkConfigurationService(name, overwrite, registryCenter); - InstanceStateService instanceStateService = new ZkInstanceStateService(name, configurationService, registryCenter); - DataSourceService dataSourceService = new ZkDataSourceService(name, configurationService, registryCenter); - return new OrchestratorImpl(configurationService, instanceStateService, dataSourceService); + String type = configuration.getRegistryCenter().get("type"); + ServiceLoader serviceLoader = ServiceLoader.load(OrchestratorFactory.class); + for (OrchestratorFactory orchestratorFactory : serviceLoader) { + if (orchestratorFactory.target(type)) { + return orchestratorFactory.create(configuration); + } + } + return new ZookeeperOrchestratorFactory().create(configuration); } - private CoordinatorRegistryCenter setupRegistryCenterIfNeeded(OrchestratorConfiguration config) { - ZookeeperConfiguration zkConfig = ZookeeperConfiguration.from(config); - CoordinatorRegistryCenter registryCenter = new ZookeeperRegistryCenter(zkConfig); - registryCenter.init(); - return registryCenter; - } } diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestratorFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestratorFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..d996fddcacd4d5aafafb9a5e09406583ac4eb4c1 --- /dev/null +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/api/OrchestratorFactory.java @@ -0,0 +1,14 @@ +package io.shardingjdbc.orchestration.api; + +import io.shardingjdbc.orchestration.api.Orchestrator; +import io.shardingjdbc.orchestration.api.config.OrchestratorConfiguration; + +/** + * Orchestrator factory + * + * @author junxiong + */ +public interface OrchestratorFactory { + boolean target(String type); + Orchestrator create(OrchestratorConfiguration configuration); +} diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdConfigurationService.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdConfigurationService.java index a663854284dc3bf170dc50d1bb46ff7cb8528482..54a856c287b91b909be9d89472f05210982c093e 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdConfigurationService.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdConfigurationService.java @@ -6,6 +6,7 @@ import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource; import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource; import io.shardingjdbc.core.rule.MasterSlaveRule; import io.shardingjdbc.orchestration.reg.base.ConfigurationService; +import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter; import javax.sql.DataSource; import java.util.Map; @@ -15,6 +16,12 @@ import java.util.Properties; * @author junxiong */ public class EtcdConfigurationService implements ConfigurationService { + private CoordinatorRegistryCenter registryCenter; + + public EtcdConfigurationService(CoordinatorRegistryCenter registryCenter) { + this.registryCenter = registryCenter; + } + @Override public void persistShardingConfiguration(Map dataSourceMap, ShardingRuleConfiguration shardingRuleConfig, Properties props, ShardingDataSource shardingDataSource) { diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdDataSourceService.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdDataSourceService.java index d3c6d8bbf10e7f883e3425b8b0c3e4c1922b7d34..b8f91f16de5db30b79690efad4c764cded8ce439 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdDataSourceService.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdDataSourceService.java @@ -1,12 +1,19 @@ package io.shardingjdbc.orchestration.reg.etcd; import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource; +import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter; import io.shardingjdbc.orchestration.reg.base.DataSourceService; /** * @author junxiong */ public class EtcdDataSourceService implements DataSourceService { + private CoordinatorRegistryCenter registryCenter; + + public EtcdDataSourceService(CoordinatorRegistryCenter registryCenter) { + this.registryCenter = registryCenter; + } + @Override public void persistDataSourcesNodeOnline(MasterSlaveDataSource masterSlaveDataSource) { diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdInstanceStateService.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdInstanceStateService.java index f168da96f337ba2d7d548d328a01e9dc9e81d586..8aabc89ea2eb1bccc1d9fbae662ed948261a8a4b 100644 --- a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdInstanceStateService.java +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdInstanceStateService.java @@ -2,12 +2,19 @@ package io.shardingjdbc.orchestration.reg.etcd; import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource; import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource; +import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter; import io.shardingjdbc.orchestration.reg.base.InstanceStateService; /** * @author junxiong */ public class EtcdInstanceStateService implements InstanceStateService { + private CoordinatorRegistryCenter registryCenter; + + public EtcdInstanceStateService(CoordinatorRegistryCenter registryCenter) { + this.registryCenter = registryCenter; + } + @Override public void persistShardingInstanceOnline(ShardingDataSource shardingDataSource) { diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdOrchestratorFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdOrchestratorFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..b740c3dff27a31ad5626bd4c6c793372cc849c69 --- /dev/null +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/etcd/EtcdOrchestratorFactory.java @@ -0,0 +1,39 @@ +package io.shardingjdbc.orchestration.reg.etcd; + +import io.shardingjdbc.orchestration.api.Orchestrator; +import io.shardingjdbc.orchestration.api.OrchestratorFactory; +import io.shardingjdbc.orchestration.api.config.OrchestratorConfiguration; +import io.shardingjdbc.orchestration.internal.OrchestratorImpl; +import io.shardingjdbc.orchestration.reg.base.ConfigurationService; +import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter; +import io.shardingjdbc.orchestration.reg.base.DataSourceService; +import io.shardingjdbc.orchestration.reg.base.InstanceStateService; + +/** + * Etcd Orchestrator factory + * + * @author junxiong + */ +public class EtcdOrchestratorFactory implements OrchestratorFactory { + + @Override + public boolean target(String type) { + return "etcd".equalsIgnoreCase(type); + } + + @Override + public Orchestrator create(OrchestratorConfiguration configuration) { + CoordinatorRegistryCenter registryCenter = setUpCoordinatorRegistryCenter(configuration); + ConfigurationService configurationService = new EtcdConfigurationService(registryCenter); + DataSourceService dataSourceService = new EtcdDataSourceService(registryCenter); + InstanceStateService instanceStateService = new EtcdInstanceStateService(registryCenter); + return new OrchestratorImpl(configurationService, instanceStateService, dataSourceService); + } + + private CoordinatorRegistryCenter setUpCoordinatorRegistryCenter(OrchestratorConfiguration configuration) { + return new EtcdRegistryCenter(EtcdConfiguration.builder() + .namespace(configuration.getRegistryCenter().get("namespace")) + .serverLists(configuration.getRegistryCenter().get("server-lists")) + .build()); + } +} diff --git a/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/zookeeper/ZookeeperOrchestratorFactory.java b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/zookeeper/ZookeeperOrchestratorFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..a403a3e88384c6016126e29e8bd4261d2d0ed699 --- /dev/null +++ b/sharding-jdbc-orchestration/src/main/java/io/shardingjdbc/orchestration/reg/zookeeper/ZookeeperOrchestratorFactory.java @@ -0,0 +1,47 @@ +package io.shardingjdbc.orchestration.reg.zookeeper; + +import io.shardingjdbc.orchestration.api.OrchestratorFactory; +import io.shardingjdbc.orchestration.api.Orchestrator; +import io.shardingjdbc.orchestration.api.config.OrchestratorConfiguration; +import io.shardingjdbc.orchestration.internal.OrchestratorImpl; +import io.shardingjdbc.orchestration.reg.base.ConfigurationService; +import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter; +import io.shardingjdbc.orchestration.reg.base.DataSourceService; +import io.shardingjdbc.orchestration.reg.base.InstanceStateService; +import io.shardingjdbc.orchestration.reg.zookeeper.config.ZkConfigurationService; +import io.shardingjdbc.orchestration.reg.zookeeper.state.datasource.ZkDataSourceService; +import io.shardingjdbc.orchestration.reg.zookeeper.state.instance.ZkInstanceStateService; + +/** + * @author junxiong + */ +public class ZookeeperOrchestratorFactory implements OrchestratorFactory { + + @Override + public boolean target(String type) { + return "zk".equalsIgnoreCase(type); + } + + /** + * build specific orchestrator instance + * + * @return orchestrator instance + */ + @Override + public Orchestrator create(OrchestratorConfiguration configuration) { + String name = configuration.getName(); + boolean overwrite = configuration.isOverwrite(); + CoordinatorRegistryCenter registryCenter = setupRegistryCenterIfNeeded(configuration); + ConfigurationService configurationService = new ZkConfigurationService(name, overwrite, registryCenter); + InstanceStateService instanceStateService = new ZkInstanceStateService(name, configurationService, registryCenter); + DataSourceService dataSourceService = new ZkDataSourceService(name, configurationService, registryCenter); + return new OrchestratorImpl(configurationService, instanceStateService, dataSourceService); + } + + private CoordinatorRegistryCenter setupRegistryCenterIfNeeded(OrchestratorConfiguration config) { + ZookeeperConfiguration zkConfig = ZookeeperConfiguration.from(config); + CoordinatorRegistryCenter registryCenter = new ZookeeperRegistryCenter(zkConfig); + registryCenter.init(); + return registryCenter; + } +} diff --git a/sharding-jdbc-orchestration/src/main/resources/META-INF/services/io.shardingjdbc.orchestration.api.OrchestratorFactory b/sharding-jdbc-orchestration/src/main/resources/META-INF/services/io.shardingjdbc.orchestration.api.OrchestratorFactory new file mode 100644 index 0000000000000000000000000000000000000000..0fd8c6121a19fbefb841b77aeb796ad471ab95da --- /dev/null +++ b/sharding-jdbc-orchestration/src/main/resources/META-INF/services/io.shardingjdbc.orchestration.api.OrchestratorFactory @@ -0,0 +1,2 @@ +io.shardingjdbc.orchestration.reg.etcd.EtcdOrchestratorFactory +io.shardingjdbc.orchestration.reg.zookeeper.ZookeeperOrchestratorFactory \ No newline at end of file diff --git a/sharding-jdbc-orchestration/src/test/java/io/shardingjdbc/orchestration/reg/AllRegTests.java b/sharding-jdbc-orchestration/src/test/java/io/shardingjdbc/orchestration/reg/AllRegTests.java new file mode 100644 index 0000000000000000000000000000000000000000..a4d0a33b62457373b75290f74fe09f5a930390a8 --- /dev/null +++ b/sharding-jdbc-orchestration/src/test/java/io/shardingjdbc/orchestration/reg/AllRegTests.java @@ -0,0 +1,11 @@ +package io.shardingjdbc.orchestration.reg; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + OrchestratorBuilderIntegrateTest.class +}) +public class AllRegTests { +} diff --git a/sharding-jdbc-orchestration/src/test/java/io/shardingjdbc/orchestration/reg/OrchestratorBuilderIntegrateTest.java b/sharding-jdbc-orchestration/src/test/java/io/shardingjdbc/orchestration/reg/OrchestratorBuilderIntegrateTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bdd70471df15a3b641c26db8a22986e258f60f1b --- /dev/null +++ b/sharding-jdbc-orchestration/src/test/java/io/shardingjdbc/orchestration/reg/OrchestratorBuilderIntegrateTest.java @@ -0,0 +1,43 @@ +package io.shardingjdbc.orchestration.reg; + +import com.google.common.collect.Maps; +import io.shardingjdbc.orchestration.api.Orchestrator; +import io.shardingjdbc.orchestration.api.OrchestratorBuilder; +import io.shardingjdbc.orchestration.api.config.OrchestratorConfiguration; +import io.shardingjdbc.orchestration.util.EmbedTestingServer; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertNotNull; + +public class OrchestratorBuilderIntegrateTest { + + @BeforeClass + public static void before() { + EmbedTestingServer.start(); + } + + @Test + public void testBuildZookeeperOrchestor() { + Map registryCenter = Maps.newHashMap(); + registryCenter.put("type", "zk"); + registryCenter.put("namespace", "test"); + registryCenter.put("server-lists", "localhost:3181"); + OrchestratorConfiguration configuration = new OrchestratorConfiguration("test", false, registryCenter); + Orchestrator orchestrator = OrchestratorBuilder.newBuilder().with(configuration).build(); + assertNotNull(orchestrator); + } + + @Test + public void testBuildEtcdOrchestor() { + Map registryCenter = Maps.newHashMap(); + registryCenter.put("type", "etcd"); + registryCenter.put("namespace", "test"); + registryCenter.put("server-lists", "http://localhost:2379"); + OrchestratorConfiguration configuration = new OrchestratorConfiguration("test", false, registryCenter); + Orchestrator orchestrator = OrchestratorBuilder.newBuilder().with(configuration).build(); + assertNotNull(orchestrator); + } +}