提交 5da9e194 编写于 作者: T terrymanu

fix orchestration spring boot starter

上级 5c12fc61
......@@ -25,11 +25,11 @@ import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfig
import org.apache.shardingsphere.orchestration.center.yaml.config.YamlCenterRepositoryConfiguration;
import org.apache.shardingsphere.orchestration.center.yaml.swapper.CenterRepositoryConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.core.strategy.algorithm.sharding.inline.InlineExpressionParser;
import org.apache.shardingsphere.sharding.core.yaml.swapper.root.RuleRootConfigurationsYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.common.SpringBootRootConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.sharding.LocalShardingRuleCondition;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.sharding.OrchestrationSpringBootRuleConfigurationsYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.sharding.OrchestrationSpringBootRulesConfigurationProperties;
import org.apache.shardingsphere.spring.boot.datasource.DataSourcePropertiesSetterHolder;
import org.apache.shardingsphere.spring.boot.util.DataSourceUtil;
......@@ -107,7 +107,7 @@ public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
@Conditional(LocalShardingRuleCondition.class)
public DataSource shardingSphereDataSourceByLocal(final OrchestrationConfiguration orchestrationConfiguration) throws SQLException {
return new OrchestrationShardingSphereDataSource(
new ShardingSphereDataSource(dataSourceMap, new RuleRootConfigurationsYamlSwapper().swap(rules), root.getProps()), orchestrationConfiguration);
new ShardingSphereDataSource(dataSourceMap, new OrchestrationSpringBootRuleConfigurationsYamlSwapper().swap(rules), root.getProps()), orchestrationConfiguration);
}
/**
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.sharding;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.core.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.core.yaml.swapper.ShadowRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.sharding.core.yaml.swapper.ShardingRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.underlying.common.config.RuleConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.swapper.YamlSwapper;
import java.util.Collection;
import java.util.LinkedList;
/**
* Orchestration Spring boot root configurations YAML swapper.
*/
public final class OrchestrationSpringBootRuleConfigurationsYamlSwapper implements YamlSwapper<OrchestrationSpringBootRulesConfigurationProperties, Collection<RuleConfiguration>> {
private final ShardingRuleConfigurationYamlSwapper shardingRuleConfigurationYamlSwapper = new ShardingRuleConfigurationYamlSwapper();
private final MasterSlaveRuleConfigurationYamlSwapper masterSlaveRuleConfigurationYamlSwapper = new MasterSlaveRuleConfigurationYamlSwapper();
private final EncryptRuleConfigurationYamlSwapper encryptRuleConfigurationYamlSwapper = new EncryptRuleConfigurationYamlSwapper();
private final ShadowRuleConfigurationYamlSwapper shadowRuleConfigurationYamlSwapper = new ShadowRuleConfigurationYamlSwapper();
@Override
public OrchestrationSpringBootRulesConfigurationProperties swap(final Collection<RuleConfiguration> data) {
OrchestrationSpringBootRulesConfigurationProperties result = new OrchestrationSpringBootRulesConfigurationProperties();
for (RuleConfiguration each : data) {
if (each instanceof ShardingRuleConfiguration) {
result.setShardingRule(shardingRuleConfigurationYamlSwapper.swap((ShardingRuleConfiguration) each));
} else if (each instanceof MasterSlaveRuleConfiguration) {
result.setMasterSlaveRule(masterSlaveRuleConfigurationYamlSwapper.swap((MasterSlaveRuleConfiguration) each));
} else if (each instanceof EncryptRuleConfiguration) {
result.setEncryptRule(encryptRuleConfigurationYamlSwapper.swap((EncryptRuleConfiguration) each));
} else if (each instanceof ShadowRuleConfiguration) {
result.setShadowRule(shadowRuleConfigurationYamlSwapper.swap((ShadowRuleConfiguration) each));
}
}
return result;
}
@Override
public Collection<RuleConfiguration> swap(final OrchestrationSpringBootRulesConfigurationProperties configurations) {
Collection<RuleConfiguration> result = new LinkedList<>();
if (null != configurations.getShardingRule()) {
result.add(shardingRuleConfigurationYamlSwapper.swap(configurations.getShardingRule()));
}
if (null != configurations.getMasterSlaveRule()) {
result.add(masterSlaveRuleConfigurationYamlSwapper.swap(configurations.getMasterSlaveRule()));
}
if (null != configurations.getEncryptRule()) {
result.add(encryptRuleConfigurationYamlSwapper.swap(configurations.getEncryptRule()));
}
if (null != configurations.getShadowRule()) {
result.add(shadowRuleConfigurationYamlSwapper.swap(configurations.getShadowRule()));
}
return result;
}
}
......@@ -19,14 +19,26 @@ package org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.shardin
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.sharding.core.yaml.config.YamlRootRuleConfigurations;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.sharding.core.yaml.config.masterslave.YamlMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.sharding.core.yaml.config.shadow.YamlShadowRuleConfiguration;
import org.apache.shardingsphere.sharding.core.yaml.config.sharding.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.config.YamlConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Spring boot rules configuration properties for orchestration.
* Orchestration spring boot rules configuration properties.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.rules")
@Getter
@Setter
public final class OrchestrationSpringBootRulesConfigurationProperties extends YamlRootRuleConfigurations {
public class OrchestrationSpringBootRulesConfigurationProperties implements YamlConfiguration {
private YamlShardingRuleConfiguration shardingRule;
private YamlMasterSlaveRuleConfiguration masterSlaveRule;
private YamlEncryptRuleConfiguration encryptRule;
private YamlShadowRuleConfiguration shadowRule;
}
......@@ -52,25 +52,28 @@ public class OrchestrationSpringBootRegistryEncryptTest {
public static void init() {
EmbedTestingServer.start();
TestCenterRepository testCenter = new TestCenterRepository();
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/datasource",
"dataSource: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 100\n"
+ " password: ''\n"
+ " username: sa\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/rule", "encryptRule:\n encryptors:\n"
+ " order_encrypt:\n"
+ " props:\n"
+ " aes.key.value: '123456'\n"
+ " type: aes\n"
+ " tables:\n"
+ " t_order:\n"
+ " columns:\n"
+ " user_id:\n"
+ " cipherColumn: user_id\n"
+ " encryptor: order_encrypt\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/datasource", ""
+ "dataSource: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 100\n"
+ " password: ''\n"
+ " username: sa\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/rule", ""
+ "rules:\n"
+ "- !!org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration\n"
+ " encryptors:\n"
+ " order_encrypt:\n"
+ " props:\n"
+ " aes.key.value: '123456'\n"
+ " type: aes\n"
+ " tables:\n"
+ " t_order:\n"
+ " columns:\n"
+ " user_id:\n"
+ " cipherColumn: user_id\n"
+ " encryptor: order_encrypt\n");
testCenter.persist("/demo_spring_boot_ds_center/config/props", "sql.show: 'true'\n");
testCenter.persist("/demo_spring_boot_ds_center/registry/datasources", "");
}
......
......@@ -52,37 +52,39 @@ public class OrchestrationSpringBootRegistryMasterSlaveTest {
public static void init() {
EmbedTestingServer.start();
TestCenterRepository testCenter = new TestCenterRepository();
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/datasource",
"ds_master: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: root\n"
+ "ds_slave_0: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:demo_ds_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: root\n"
+ "ds_slave_1: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:demo_ds_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: root\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/rule", "masterSlaveRule:\n"
+ " dataSources:\n"
+ " ds_ms:\n"
+ " loadBalanceAlgorithmType: ROUND_ROBIN\n"
+ " masterDataSourceName: ds_master\n"
+ " name: ds_ms\n"
+ " slaveDataSourceNames: \n"
+ " - ds_slave_0\n"
+ " - ds_slave_1\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/datasource", ""
+ "ds_master: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: root\n"
+ "ds_slave_0: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:demo_ds_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: root\n"
+ "ds_slave_1: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:demo_ds_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: root\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/rule", ""
+ "rules:\n"
+ "- !!org.apache.shardingsphere.sharding.core.yaml.config.masterslave.YamlMasterSlaveRuleConfiguration\n"
+ " dataSources:\n"
+ " ds_ms:\n"
+ " loadBalanceAlgorithmType: ROUND_ROBIN\n"
+ " masterDataSourceName: ds_master\n"
+ " name: ds_ms\n"
+ " slaveDataSourceNames: \n"
+ " - ds_slave_0\n"
+ " - ds_slave_1\n");
testCenter.persist("/demo_spring_boot_ds_center/config/props", "{}\n");
testCenter.persist("/demo_spring_boot_ds_center/registry/datasources", "");
}
......
......@@ -60,72 +60,77 @@ public class OrchestrationSpringBootRegistryShardingTest {
public static void init() {
EmbedTestingServer.start();
TestCenterRepository testCenter = new TestCenterRepository();
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/datasource", "ds: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: sa\n"
+ "ds_0: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: sa\n"
+ "ds_1: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: sa\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/rule", "shardingRule:\n"
+ " bindingTables:\n"
+ " - t_order\n"
+ " - t_order_item\n"
+ " broadcastTables:\n"
+ " - t_config\n"
+ " defaultDatabaseStrategy:\n"
+ " standard:\n"
+ " shardingAlgorithm:\n"
+ " type: INLINE\n"
+ " props:\n"
+ " algorithm.expression: ds_${user_id % 2}\n"
+ " shardingColumn: user_id\n"
+ " tables:\n"
+ " t_order:\n"
+ " actualDataNodes: ds_${0..1}.t_order_${0..1}\n"
+ " keyGenerator:\n"
+ " column: order_id\n"
+ " props:\n"
+ " worker.id: '123'\n"
+ " type: SNOWFLAKE\n"
+ " logicTable: t_order\n"
+ " tableStrategy:\n"
+ " standard:\n"
+ " shardingAlgorithm:\n"
+ " type: INLINE\n"
+ " props:\n"
+ " algorithm.expression: t_order_${order_id % 2}\n"
+ " shardingColumn: order_id\n"
+ " t_order_item:\n"
+ " actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n"
+ " keyGenerator:\n"
+ " column: order_item_id\n"
+ " props:\n"
+ " worker.id: '123'\n"
+ " type: SNOWFLAKE\n"
+ " logicTable: t_order_item\n"
+ " tableStrategy:\n"
+ " standard:\n"
+ " shardingAlgorithm:\n"
+ " type: INLINE\n"
+ " props:\n"
+ " algorithm.expression: t_order_item_${order_id % 2}\n"
+ " shardingColumn: order_id\n");
testCenter.persist("/demo_spring_boot_ds_center/config/props", "executor.size: '100'\nsql.show: 'true'\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/datasource", ""
+ "ds: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: sa\n"
+ "ds_0: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: sa\n"
+ "ds_1: !!org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: org.apache.commons.dbcp2.BasicDataSource\n"
+ " properties:\n"
+ " url: jdbc:h2:mem:ds_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL\n"
+ " maxTotal: 16\n"
+ " password: ''\n"
+ " username: sa\n");
testCenter.persist("/demo_spring_boot_ds_center/config/schema/logic_db/rule", ""
+ "rules:\n"
+ "- !!org.apache.shardingsphere.sharding.core.yaml.config.sharding.YamlShardingRuleConfiguration\n"
+ " bindingTables:\n"
+ " - t_order\n"
+ " - t_order_item\n"
+ " broadcastTables:\n"
+ " - t_config\n"
+ " defaultDatabaseStrategy:\n"
+ " standard:\n"
+ " shardingAlgorithm:\n"
+ " type: INLINE\n"
+ " props:\n"
+ " algorithm.expression: ds_${user_id % 2}\n"
+ " shardingColumn: user_id\n"
+ " tables:\n"
+ " t_order:\n"
+ " actualDataNodes: ds_${0..1}.t_order_${0..1}\n"
+ " keyGenerator:\n"
+ " column: order_id\n"
+ " props:\n"
+ " worker.id: '123'\n"
+ " type: SNOWFLAKE\n"
+ " logicTable: t_order\n"
+ " tableStrategy:\n"
+ " standard:\n"
+ " shardingAlgorithm:\n"
+ " type: INLINE\n"
+ " props:\n"
+ " algorithm.expression: t_order_${order_id % 2}\n"
+ " shardingColumn: order_id\n"
+ " t_order_item:\n"
+ " actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n"
+ " keyGenerator:\n"
+ " column: order_item_id\n"
+ " props:\n"
+ " worker.id: '123'\n"
+ " type: SNOWFLAKE\n"
+ " logicTable: t_order_item\n"
+ " tableStrategy:\n"
+ " standard:\n"
+ " shardingAlgorithm:\n"
+ " type: INLINE\n"
+ " props:\n"
+ " algorithm.expression: t_order_item_${order_id % 2}\n"
+ " shardingColumn: order_id\n");
testCenter.persist("/demo_spring_boot_ds_center/config/props", ""
+ "executor.size: '100'\n"
+ "sql.show: 'true'\n");
testCenter.persist("/demo_spring_boot_ds_center/registry/datasources", "");
}
......
......@@ -32,7 +32,7 @@ import java.util.Collection;
import java.util.LinkedList;
/**
* Rule root configurations YAML swapper.
* Spring boot rule configurations YAML swapper.
*/
public final class SpringBootRuleConfigurationsYamlSwapper implements YamlSwapper<SpringBootRulesConfigurationProperties, Collection<RuleConfiguration>> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册