You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/06/19 14:59:47 UTC
[shardingsphere] branch master updated: shardingSphere algorithm
registry to spring bean support by spring boot. (#6138)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 618e96e shardingSphere algorithm registry to spring bean support by spring boot. (#6138)
618e96e is described below
commit 618e96e8ca56177ab9541809a4d248535ee96307
Author: xiaoyu <54...@qq.com>
AuthorDate: Fri Jun 19 22:59:34 2020 +0800
shardingSphere algorithm registry to spring bean support by spring boot. (#6138)
* shardingSphere algorithm registry to spring bean support by spring boot.
* shardingSphere algorithm registry to spring bean support by spring boot.
* fix test error.
* fix check style.
---
...ocal-nacos-sharding-databases-tables.properties | 6 +-
...-zookeeper-sharding-databases-tables.properties | 11 +-
...pplication-sharding-databases-tables.properties | 6 +-
.../application-sharding-databases.properties | 2 +-
.../application-sharding-tables.properties | 4 +-
...pplication-sharding-databases-tables.properties | 6 +-
.../application-sharding-databases.properties | 8 +-
.../application-sharding-tables.properties | 4 +-
.../src/main/resources/application.properties | 4 +-
.../core/configcenter/ConfigCenter.java | 50 ++++++---
.../AlgorithmProvidedEncryptRuleConfiguration.java | 24 ++--
.../encrypt/constant/EncryptOrder.java | 5 +
...eAlgorithmProviderConfigurationYamlSwapper.java | 79 +++++++++++++
...infra.yaml.swapper.YamlRuleConfigurationSwapper | 1 +
.../boot/EncryptAlgorithmProvidedBeanRegistry.java | 46 ++++++++
.../boot/EncryptRuleSpringBootConfiguration.java | 43 ++++++-
...EncryptSpringBootAlgorithmProviderBeanTest.java | 50 +++++++++
.../spring/boot/fixture/TestEncryptAlgorithm.java | 38 +++++--
...he.shardingsphere.encrypt.spi.EncryptAlgorithm} | 2 +-
.../test/resources/application-encrypt.properties} | 4 +-
.../src/test/resources/logback-test.xml | 36 ++++++
...orithmProvidedMasterSlaveRuleConfiguration.java | 19 +++-
.../masterslave/constant/MasterSlaveOrder.java | 5 +
...eAlgorithmProviderConfigurationYamlSwapper.java | 88 +++++++++++++++
...infra.yaml.swapper.YamlRuleConfigurationSwapper | 1 +
.../MasterSlaveAlgorithmProvidedBeanRegistry.java | 46 ++++++++
.../MasterSlaveRuleSpringbootConfiguration.java | 45 +++++++-
...erSlaveSpringBootAlgorithmProviderBeanTest.java | 50 +++++++++
...ndomMasterSlaveLoadBalanceAlgorithmFixture.java | 31 +++--
...asterslave.spi.MasterSlaveLoadBalanceAlgorithm} | 2 +-
.../resources/application-masterslave.properties} | 4 +-
.../src/test/resources/logback-test.xml | 36 ++++++
.../boot/ShadowRuleSpringBootConfiguration.java | 22 +++-
...AlgorithmProvidedShardingRuleConfiguration.java | 52 ++++++---
.../sharding/constant/ShardingOrder.java | 5 +
...eAlgorithmProviderConfigurationYamlSwapper.java | 125 +++++++++++++++++++++
...infra.yaml.swapper.YamlRuleConfigurationSwapper | 2 +
.../ShardingAlgorithmProvidedBeanRegistry.java | 50 +++++++++
.../boot/ShardingRuleSpringBootConfiguration.java | 50 ++++++++-
...hardingSpringBootAlgorithmProviderBeanTest.java | 55 +++++++++
.../ExtendInlineShardingAlgorithmFixture.java | 89 +++++++++++++++
.../IncrementKeyGenerateAlgorithmFixture.java | 39 +++++--
...ardingsphere.sharding.spi.KeyGenerateAlgorithm} | 2 +-
....shardingsphere.sharding.spi.ShardingAlgorithm} | 3 +-
.../test/resources/application-sharding.properties | 27 +++++
.../src/test/resources/logback-test.xml | 36 ++++++
.../YamlShardingSphereAlgorithmConfiguration.java | 14 +++
.../spring/boot/SpringBootConfiguration.java | 10 +-
.../test/resources/application-sharding.properties | 6 +-
.../OrchestrationSpringBootConfiguration.java | 11 +-
.../type/OrchestrationSpringBootEncryptTest.java | 14 +--
.../test/resources/application-sharding.properties | 6 +-
.../AbstractAlgorithmProvidedBeanRegistry.java | 92 +++++++++++++++
53 files changed, 1321 insertions(+), 145 deletions(-)
diff --git a/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-nacos-sharding-databases-tables.properties b/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-nacos-sharding-databases-tables.properties
index fb785be..df34085 100644
--- a/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-nacos-sharding-databases-tables.properties
+++ b/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-nacos-sharding-databases-tables.properties
@@ -48,11 +48,11 @@ spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.c
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-zookeeper-sharding-databases-tables.properties b/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-zookeeper-sharding-databases-tables.properties
index 91855ac..06830ef 100644
--- a/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-zookeeper-sharding-databases-tables.properties
+++ b/examples/shardingsphere-jdbc-example/orchestration-example/orchestration-spring-boot-example/src/main/resources/application-local-zookeeper-sharding-databases-tables.properties
@@ -48,11 +48,11 @@ spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.c
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
@@ -62,3 +62,8 @@ spring.shardingsphere.orchestration.demo_spring_boot_ds_sharding.instance-type=z
spring.shardingsphere.orchestration.demo_spring_boot_ds_sharding.server-lists=localhost:2181
spring.shardingsphere.orchestration.demo_spring_boot_ds_sharding.namespace=orchestration-spring-boot-demo
spring.shardingsphere.orchestration.demo_spring_boot_ds_sharding.props.overwrite=true
+
+spring.shardingsphere.rules.encrypt.encryptors.status_encryptor.type=AES
+spring.shardingsphere.rules.encrypt.encryptors.status_encryptor.props.aes.key.value=123456
+spring.shardingsphere.rules.encrypt.tables.t_order.columns.status.cipher-column=status
+spring.shardingsphere.rules.encrypt.tables.t_order.columns.status.encryptor-name=status_encryptor
\ No newline at end of file
diff --git a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases-tables.properties b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases-tables.properties
index 57249ba..c688c1e 100644
--- a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases-tables.properties
+++ b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases-tables.properties
@@ -46,11 +46,11 @@ spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.c
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases.properties b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases.properties
index cfcc7ef..ed87161 100644
--- a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases.properties
+++ b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-databases.properties
@@ -41,7 +41,7 @@ spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.c
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-tables.properties b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-tables.properties
index f9680b3..6d3107e 100644
--- a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-tables.properties
+++ b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-jpa-example/src/main/resources/application-sharding-tables.properties
@@ -42,9 +42,9 @@ spring.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.rules.sharding.broadcast-tables=t_address
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases-tables.properties b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases-tables.properties
index fe778d0..6e7d874 100644
--- a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases-tables.properties
+++ b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases-tables.properties
@@ -46,11 +46,11 @@ spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.c
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties
index 581b7c4..ed87161 100644
--- a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties
+++ b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases.properties
@@ -37,11 +37,11 @@ spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-ge
spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds_$->{0..1}.t_order_item
-spring.shardingsphere.rules.sharding.tables.t_order_item.key-generator.column=order_item_id
+spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
-spring.shardingsphere.rules.sharding.tables.snowflake.type=SNOWFLAKE
-spring.shardingsphere.rules.sharding.tables.snowflake.props.worker.id=123
+spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
+spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-tables.properties b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-tables.properties
index 070a28e..0cc1808 100644
--- a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-tables.properties
+++ b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-tables.properties
@@ -41,9 +41,9 @@ spring.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.rules.sharding.broadcast-tables=t_address
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application.properties b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application.properties
index 13c9c5b..b9e32bf 100644
--- a/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application.properties
+++ b/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application.properties
@@ -17,8 +17,8 @@
mybatis.config-location=classpath:META-INF/mybatis-config.xml
-spring.profiles.active=sharding-databases
+#spring.profiles.active=sharding-databases
#spring.profiles.active=sharding-tables
#spring.profiles.active=sharding-databases-tables
#spring.profiles.active=master-slave
-#spring.profiles.active=sharding-master-slave
+spring.profiles.active=sharding-master-slave
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
index a897fb9..e16c9c1 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
@@ -21,10 +21,28 @@ import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+import java.util.stream.Collectors;
import org.apache.shardingsphere.cluster.configuration.config.ClusterConfiguration;
import org.apache.shardingsphere.cluster.configuration.swapper.ClusterConfigurationYamlSwapper;
import org.apache.shardingsphere.cluster.configuration.yaml.YamlClusterConfiguration;
+import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.auth.yaml.config.YamlAuthenticationConfiguration;
+import org.apache.shardingsphere.infra.auth.yaml.swapper.AuthenticationYamlSwapper;
+import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+import org.apache.shardingsphere.masterslave.algorithm.config.AlgorithmProvidedMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
import org.apache.shardingsphere.metrics.configuration.swapper.MetricsConfigurationYamlSwapper;
@@ -33,24 +51,8 @@ import org.apache.shardingsphere.orchestration.center.ConfigCenterRepository;
import org.apache.shardingsphere.orchestration.core.configuration.DataSourceConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
-import org.apache.shardingsphere.infra.auth.Authentication;
-import org.apache.shardingsphere.infra.auth.yaml.config.YamlAuthenticationConfiguration;
-import org.apache.shardingsphere.infra.auth.yaml.swapper.AuthenticationYamlSwapper;
-import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-import java.util.stream.Collectors;
/**
* Configuration service.
@@ -129,6 +131,20 @@ public final class ConfigCenter {
Preconditions.checkState(!config.getTables().isEmpty() || null != config.getDefaultTableShardingStrategy(),
"No available sharding rule configuration in `%s` for orchestration.", shardingSchemaName);
configurations.add(each);
+ } else if (each instanceof AlgorithmProvidedShardingRuleConfiguration) {
+ AlgorithmProvidedShardingRuleConfiguration config = (AlgorithmProvidedShardingRuleConfiguration) each;
+ Preconditions.checkState(!config.getTables().isEmpty() || null != config.getDefaultTableShardingStrategy(),
+ "No available sharding rule configuration in `%s` for orchestration.", shardingSchemaName);
+ configurations.add(each);
+ } else if (each instanceof AlgorithmProvidedMasterSlaveRuleConfiguration) {
+ AlgorithmProvidedMasterSlaveRuleConfiguration config = (AlgorithmProvidedMasterSlaveRuleConfiguration) each;
+ config.getDataSources().forEach(group -> Preconditions.checkState(
+ !group.getMasterDataSourceName().isEmpty(), "No available master-slave rule configuration in `%s` for orchestration.", shardingSchemaName));
+ configurations.add(each);
+ } else if (each instanceof AlgorithmProvidedEncryptRuleConfiguration) {
+ AlgorithmProvidedEncryptRuleConfiguration config = (AlgorithmProvidedEncryptRuleConfiguration) each;
+ Preconditions.checkState(!config.getEncryptors().isEmpty(), "No available encrypt rule configuration in `%s` for orchestration.", shardingSchemaName);
+ configurations.add(each);
} else if (each instanceof MasterSlaveRuleConfiguration) {
MasterSlaveRuleConfiguration config = (MasterSlaveRuleConfiguration) each;
config.getDataSources().forEach(group -> Preconditions.checkState(
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/algorithm/config/AlgorithmProvidedEncryptRuleConfiguration.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/algorithm/config/AlgorithmProvidedEncryptRuleConfiguration.java
index b8fea6c..938245c 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/algorithm/config/AlgorithmProvidedEncryptRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/algorithm/config/AlgorithmProvidedEncryptRuleConfiguration.java
@@ -17,23 +17,33 @@
package org.apache.shardingsphere.encrypt.algorithm.config;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import lombok.Setter;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import java.util.Collection;
-import java.util.Map;
-
/**
* Algorithm provided encrypt rule configuration.
*/
-@RequiredArgsConstructor
@Getter
+@Setter
public final class AlgorithmProvidedEncryptRuleConfiguration implements RuleConfiguration {
- private final Collection<EncryptTableRuleConfiguration> tables;
+ private Collection<EncryptTableRuleConfiguration> tables = new LinkedList<>();
+
+ private Map<String, EncryptAlgorithm> encryptors = new LinkedHashMap<>();
+
+ public AlgorithmProvidedEncryptRuleConfiguration() {
+ }
- private final Map<String, EncryptAlgorithm> encryptors;
+ public AlgorithmProvidedEncryptRuleConfiguration(final Collection<EncryptTableRuleConfiguration> tables,
+ final Map<String, EncryptAlgorithm> encryptors) {
+ this.tables = tables;
+ this.encryptors = encryptors;
+ }
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/constant/EncryptOrder.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/constant/EncryptOrder.java
index 3e1c6af..5fc891d 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/constant/EncryptOrder.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/constant/EncryptOrder.java
@@ -30,4 +30,9 @@ public final class EncryptOrder {
* Encrypt order.
*/
public static final int ORDER = 50;
+
+ /**
+ * Algorithm provider encrypt order.
+ */
+ public static final int ALGORITHM_PROVIDER_ENCRYPT_ORDER = 51;
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/EncryptRuleAlgorithmProviderConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/EncryptRuleAlgorithmProviderConfigurationYamlSwapper.java
new file mode 100644
index 0000000..bf93c8a
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/EncryptRuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -0,0 +1,79 @@
+/*
+ * 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.encrypt.yaml.swapper;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map.Entry;
+import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
+import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.swapper.rule.EncryptTableRuleConfigurationYamlSwapper;
+import org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
+
+/**
+ * Encrypt rule algorithm provider configuration YAML swapper.
+ */
+public final class EncryptRuleAlgorithmProviderConfigurationYamlSwapper implements YamlRuleConfigurationSwapper<YamlEncryptRuleConfiguration, AlgorithmProvidedEncryptRuleConfiguration> {
+
+ private final EncryptTableRuleConfigurationYamlSwapper tableYamlSwapper = new EncryptTableRuleConfigurationYamlSwapper();
+
+ @Override
+ public YamlEncryptRuleConfiguration swapToYamlConfiguration(final AlgorithmProvidedEncryptRuleConfiguration data) {
+ YamlEncryptRuleConfiguration result = new YamlEncryptRuleConfiguration();
+ data.getTables().forEach(each -> result.getTables().put(each.getName(), tableYamlSwapper.swapToYamlConfiguration(each)));
+ data.getEncryptors().forEach((key, value) -> result.getEncryptors().put(key, YamlShardingSphereAlgorithmConfiguration.buildByTypedSPI(value)));
+ return result;
+ }
+
+ @Override
+ public AlgorithmProvidedEncryptRuleConfiguration swapToObject(final YamlEncryptRuleConfiguration yamlConfiguration) {
+ AlgorithmProvidedEncryptRuleConfiguration ruleConfiguration = new AlgorithmProvidedEncryptRuleConfiguration();
+ ruleConfiguration.setTables(swapTables(yamlConfiguration));
+ return ruleConfiguration;
+ }
+
+ private Collection<EncryptTableRuleConfiguration> swapTables(final YamlEncryptRuleConfiguration yamlConfiguration) {
+ Collection<EncryptTableRuleConfiguration> result = new LinkedList<>();
+ for (Entry<String, YamlEncryptTableRuleConfiguration> entry : yamlConfiguration.getTables().entrySet()) {
+ YamlEncryptTableRuleConfiguration yamlEncryptTableRuleConfiguration = entry.getValue();
+ yamlEncryptTableRuleConfiguration.setName(entry.getKey());
+ result.add(tableYamlSwapper.swapToObject(yamlEncryptTableRuleConfiguration));
+ }
+ return result;
+ }
+
+ @Override
+ public Class<AlgorithmProvidedEncryptRuleConfiguration> getTypeClass() {
+ return AlgorithmProvidedEncryptRuleConfiguration.class;
+ }
+
+ @Override
+ public String getRuleTagName() {
+ return "ENCRYPT";
+ }
+
+ @Override
+ public int getOrder() {
+ return EncryptOrder.ALGORITHM_PROVIDER_ENCRYPT_ORDER;
+ }
+
+}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
index 5c061de..0b1aa80 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
@@ -16,3 +16,4 @@
#
org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper
+org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleAlgorithmProviderConfigurationYamlSwapper
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptAlgorithmProvidedBeanRegistry.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptAlgorithmProvidedBeanRegistry.java
new file mode 100644
index 0000000..359f14f
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptAlgorithmProvidedBeanRegistry.java
@@ -0,0 +1,46 @@
+/*
+ * 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.encrypt.spring.boot;
+
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.core.env.Environment;
+
+/**
+ * Encrypt algorithm provided bean registry.
+ */
+public class EncryptAlgorithmProvidedBeanRegistry extends AbstractAlgorithmProvidedBeanRegistry {
+
+ private static final String ENCRYPTORS_ALGORITHMS = "spring.shardingsphere.rules.encrypt.encryptors.";
+
+ /**
+ * Instantiates a new encrypt algorithm provided bean registry.
+ *
+ * @param environment environment
+ */
+ public EncryptAlgorithmProvidedBeanRegistry(final Environment environment) {
+ super(environment);
+ }
+
+ @Override
+ public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry registry) throws BeansException {
+ registerBean(ENCRYPTORS_ALGORITHMS, EncryptAlgorithm.class, registry);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptRuleSpringBootConfiguration.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptRuleSpringBootConfiguration.java
index cb2c7e1..b128b0e 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptRuleSpringBootConfiguration.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptRuleSpringBootConfiguration.java
@@ -17,21 +17,33 @@
package org.apache.shardingsphere.encrypt.spring.boot;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spring.boot.condition.EncryptSpringBootCondition;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleAlgorithmProviderConfigurationYamlSwapper;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
/**
* Encrypt rule configuration for spring boot.
*/
@Configuration
+@ConditionalOnClass(YamlEncryptRuleConfiguration.class)
+@Conditional(EncryptSpringBootCondition.class)
public class EncryptRuleSpringBootConfiguration {
+ private final EncryptRuleAlgorithmProviderConfigurationYamlSwapper swapper = new EncryptRuleAlgorithmProviderConfigurationYamlSwapper();
+
/**
* Encrypt YAML rule spring boot configuration.
*
@@ -39,10 +51,33 @@ public class EncryptRuleSpringBootConfiguration {
*/
@Bean
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.encrypt")
- @ConditionalOnClass(YamlEncryptRuleConfiguration.class)
- @Conditional(EncryptSpringBootCondition.class)
- public YamlRuleConfiguration encrypt() {
+ public YamlEncryptRuleConfiguration encryptConfig() {
return new YamlEncryptRuleConfiguration();
}
+
+ /**
+ * Encrypt rule configuration for spring boot.
+ *
+ * @param yamlEncryptRuleConfiguration YAML encrypt rule configuration
+ * @param encryptors encryptors algorithm to map
+ * @return encrypt rule configuration
+ */
+ @Bean
+ public RuleConfiguration encryptRuleConfiguration(final YamlEncryptRuleConfiguration yamlEncryptRuleConfiguration, final ObjectProvider<Map<String, EncryptAlgorithm>> encryptors) {
+ AlgorithmProvidedEncryptRuleConfiguration ruleConfiguration = swapper.swapToObject(yamlEncryptRuleConfiguration);
+ ruleConfiguration.setEncryptors(Optional.ofNullable(encryptors.getIfAvailable()).orElse(Collections.emptyMap()));
+ return ruleConfiguration;
+ }
+
+ /**
+ * Encrypt algorithm provided bean registry.
+ *
+ * @param environment environment
+ * @return encrypt algorithm provided bean registry
+ */
+ @Bean
+ public static EncryptAlgorithmProvidedBeanRegistry encryptAlgorithmProvidedBeanRegistry(final Environment environment) {
+ return new EncryptAlgorithmProvidedBeanRegistry(environment);
+ }
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptSpringBootAlgorithmProviderBeanTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptSpringBootAlgorithmProviderBeanTest.java
new file mode 100644
index 0000000..c29cda8
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/java/org/apache/shardingsphere/encrypt/spring/boot/EncryptSpringBootAlgorithmProviderBeanTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.encrypt.spring.boot;
+
+import javax.annotation.Resource;
+import org.apache.shardingsphere.encrypt.spring.boot.fixture.TestEncryptAlgorithm;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(classes = EncryptSpringBootAlgorithmProviderBeanTest.class)
+@SpringBootApplication
+@ActiveProfiles("encrypt")
+public class EncryptSpringBootAlgorithmProviderBeanTest {
+
+ @Resource
+ private ApplicationContext applicationContext;
+
+ @Test
+ public void assertAlgorithmProviderBean() {
+ Object algorithmBean = applicationContext.getBean("aes_encryptor");
+ assertThat(algorithmBean.getClass().getName(), is(TestEncryptAlgorithm.class.getName()));
+ TestEncryptAlgorithm algorithmFixture = (TestEncryptAlgorithm) algorithmBean;
+ assertNotNull(algorithmFixture.getEnvironment());
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/java/org/apache/shardingsphere/encrypt/spring/boot/fixture/TestEncryptAlgorithm.java
similarity index 58%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
copy to shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/java/org/apache/shardingsphere/encrypt/spring/boot/fixture/TestEncryptAlgorithm.java
index d9f9747..66f70bf 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/java/org/apache/shardingsphere/encrypt/spring/boot/fixture/TestEncryptAlgorithm.java
@@ -15,22 +15,40 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.yaml.config.algorithm;
+package org.apache.shardingsphere.encrypt.spring.boot.fixture;
+import java.util.Properties;
+import javax.annotation.Resource;
import lombok.Getter;
import lombok.Setter;
-import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
-
-import java.util.Properties;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.springframework.core.env.Environment;
-/**
- * ShardingSphere algorithm configuration for YAML.
- */
@Getter
@Setter
-public final class YamlShardingSphereAlgorithmConfiguration implements YamlConfiguration {
-
- private String type;
+public final class TestEncryptAlgorithm implements EncryptAlgorithm {
private Properties props = new Properties();
+
+ @Resource
+ private Environment environment;
+
+ @Override
+ public void init() {
+ }
+
+ @Override
+ public String encrypt(final Object plaintext) {
+ return "encryptValue";
+ }
+
+ @Override
+ public Object decrypt(final String ciphertext) {
+ return "decryptValue";
+ }
+
+ @Override
+ public String getType() {
+ return "test";
+ }
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm
similarity index 90%
copy from shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
copy to shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm
index 5c061de..0f2ce36 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper
+org.apache.shardingsphere.encrypt.spring.boot.fixture.TestEncryptAlgorithm
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/application-encrypt.properties
similarity index 83%
copy from shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
copy to shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/application-encrypt.properties
index ffe3544..58220d2 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/application-encrypt.properties
@@ -15,4 +15,6 @@
# limitations under the License.
#
-org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper
+spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type=test
+spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.props.aes.key.value=123456
+
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/logback-test.xml b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..297e842
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/test/resources/logback-test.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0"?>
+<!--
+ ~ 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.
+ -->
+
+<configuration>
+ <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+ <logger name="org.apache.shardingsphere" level="info" additivity="false">
+ <appender-ref ref="console"/>
+ </logger>
+ <logger name="org.apache.shardingsphere.core.execute.threadlocal.ExecutorExceptionHandler" level="off" />
+ <logger name="org.apache.shardingsphere.infra.log.ConfigurationLogger" level="off" />
+ <logger name="ShardingSphere-metadata" level="off" />
+
+ <root>
+ <level value="error" />
+ <appender-ref ref="console" />
+ </root>
+</configuration>
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/algorithm/config/AlgorithmProvidedMasterSlaveRuleConfiguration.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/algorithm/config/AlgorithmProvidedMasterSlaveRuleConfiguration.java
index 1f96090..482a8c7 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/algorithm/config/AlgorithmProvidedMasterSlaveRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/algorithm/config/AlgorithmProvidedMasterSlaveRuleConfiguration.java
@@ -17,8 +17,10 @@
package org.apache.shardingsphere.masterslave.algorithm.config;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import lombok.Setter;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.rule.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
@@ -29,11 +31,20 @@ import java.util.Map;
/**
* Algorithm provided master-slave rule configuration.
*/
-@RequiredArgsConstructor
@Getter
+@Setter
public final class AlgorithmProvidedMasterSlaveRuleConfiguration implements RuleConfiguration {
- private final Collection<MasterSlaveDataSourceRuleConfiguration> dataSources;
+ private Collection<MasterSlaveDataSourceRuleConfiguration> dataSources = new LinkedList<>();
- private final Map<String, MasterSlaveLoadBalanceAlgorithm> loadBalanceAlgorithms;
+ private Map<String, MasterSlaveLoadBalanceAlgorithm> loadBalanceAlgorithms = new LinkedHashMap<>();
+
+ public AlgorithmProvidedMasterSlaveRuleConfiguration() {
+ }
+
+ public AlgorithmProvidedMasterSlaveRuleConfiguration(final Collection<MasterSlaveDataSourceRuleConfiguration> dataSources,
+ final Map<String, MasterSlaveLoadBalanceAlgorithm> loadBalanceAlgorithms) {
+ this.dataSources = dataSources;
+ this.loadBalanceAlgorithms = loadBalanceAlgorithms;
+ }
}
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/constant/MasterSlaveOrder.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/constant/MasterSlaveOrder.java
index 879f6c5..1f56303 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/constant/MasterSlaveOrder.java
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/constant/MasterSlaveOrder.java
@@ -30,4 +30,9 @@ public final class MasterSlaveOrder {
* Master-slave order.
*/
public static final int ORDER = 10;
+
+ /**
+ * Algorithm provider master-slave order.
+ */
+ public static final int ALGORITHM_PROVIDER_MASTER_SLAVE_ORDER = 11;
}
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/yaml/swapper/MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/yaml/swapper/MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper.java
new file mode 100644
index 0000000..94e25ff
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/java/org/apache/shardingsphere/masterslave/yaml/swapper/MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -0,0 +1,88 @@
+/*
+ * 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.masterslave.yaml.swapper;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+import org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
+import org.apache.shardingsphere.masterslave.algorithm.config.AlgorithmProvidedMasterSlaveRuleConfiguration;
+import org.apache.shardingsphere.masterslave.api.config.rule.MasterSlaveDataSourceRuleConfiguration;
+import org.apache.shardingsphere.masterslave.constant.MasterSlaveOrder;
+import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
+import org.apache.shardingsphere.masterslave.yaml.config.rule.YamlMasterSlaveDataSourceRuleConfiguration;
+
+/**
+ * Master-slave rule configuration YAML swapper.
+ */
+public final class MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper implements YamlRuleConfigurationSwapper<YamlMasterSlaveRuleConfiguration, AlgorithmProvidedMasterSlaveRuleConfiguration> {
+
+ @Override
+ public YamlMasterSlaveRuleConfiguration swapToYamlConfiguration(final AlgorithmProvidedMasterSlaveRuleConfiguration data) {
+ YamlMasterSlaveRuleConfiguration result = new YamlMasterSlaveRuleConfiguration();
+ result.setDataSources(data.getDataSources().stream().collect(
+ Collectors.toMap(MasterSlaveDataSourceRuleConfiguration::getName, this::swapToYamlConfiguration, (a, b) -> b, LinkedHashMap::new)));
+ if (null != data.getLoadBalanceAlgorithms()) {
+ data.getLoadBalanceAlgorithms().forEach((key, value) -> result.getLoadBalancers().put(key, YamlShardingSphereAlgorithmConfiguration.buildByTypedSPI(value)));
+ }
+ return result;
+ }
+
+ private YamlMasterSlaveDataSourceRuleConfiguration swapToYamlConfiguration(final MasterSlaveDataSourceRuleConfiguration dataSourceRuleConfiguration) {
+ YamlMasterSlaveDataSourceRuleConfiguration result = new YamlMasterSlaveDataSourceRuleConfiguration();
+ result.setName(dataSourceRuleConfiguration.getName());
+ result.setMasterDataSourceName(dataSourceRuleConfiguration.getMasterDataSourceName());
+ result.setSlaveDataSourceNames(dataSourceRuleConfiguration.getSlaveDataSourceNames());
+ result.setLoadBalancerName(dataSourceRuleConfiguration.getLoadBalancerName());
+ return result;
+ }
+
+ @Override
+ public AlgorithmProvidedMasterSlaveRuleConfiguration swapToObject(final YamlMasterSlaveRuleConfiguration yamlConfiguration) {
+ Collection<MasterSlaveDataSourceRuleConfiguration> dataSources = new LinkedList<>();
+ for (Entry<String, YamlMasterSlaveDataSourceRuleConfiguration> entry : yamlConfiguration.getDataSources().entrySet()) {
+ dataSources.add(swapToObject(entry.getKey(), entry.getValue()));
+ }
+ AlgorithmProvidedMasterSlaveRuleConfiguration ruleConfiguration = new AlgorithmProvidedMasterSlaveRuleConfiguration();
+ ruleConfiguration.setDataSources(dataSources);
+ return ruleConfiguration;
+ }
+
+ private MasterSlaveDataSourceRuleConfiguration swapToObject(final String name, final YamlMasterSlaveDataSourceRuleConfiguration yamlDataSourceRuleConfiguration) {
+ return new MasterSlaveDataSourceRuleConfiguration(name,
+ yamlDataSourceRuleConfiguration.getMasterDataSourceName(), yamlDataSourceRuleConfiguration.getSlaveDataSourceNames(), yamlDataSourceRuleConfiguration.getLoadBalancerName());
+ }
+
+ @Override
+ public Class<AlgorithmProvidedMasterSlaveRuleConfiguration> getTypeClass() {
+ return AlgorithmProvidedMasterSlaveRuleConfiguration.class;
+ }
+
+ @Override
+ public String getRuleTagName() {
+ return "MASTER_SLAVE";
+ }
+
+ @Override
+ public int getOrder() {
+ return MasterSlaveOrder.ALGORITHM_PROVIDER_MASTER_SLAVE_ORDER;
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
index ffe3544..df3e942 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
@@ -15,4 +15,5 @@
# limitations under the License.
#
+org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper
org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveAlgorithmProvidedBeanRegistry.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveAlgorithmProvidedBeanRe [...]
new file mode 100644
index 0000000..888eb96
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveAlgorithmProvidedBeanRegistry.java
@@ -0,0 +1,46 @@
+/*
+ * 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.masterslave.spring.boot;
+
+import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
+import org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.core.env.Environment;
+
+/**
+ * Master slave algorithm provided bean registry.
+ */
+public class MasterSlaveAlgorithmProvidedBeanRegistry extends AbstractAlgorithmProvidedBeanRegistry {
+
+ private static final String MASTER_SLAVE_ALGORITHMS = "spring.shardingsphere.rules.master-slave.load-balancers.";
+
+ /**
+ * Instantiates a new master-slave algorithm provided bean registry.
+ *
+ * @param environment environment
+ */
+ public MasterSlaveAlgorithmProvidedBeanRegistry(final Environment environment) {
+ super(environment);
+ }
+
+ @Override
+ public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry registry) throws BeansException {
+ registerBean(MASTER_SLAVE_ALGORITHMS, MasterSlaveLoadBalanceAlgorithm.class, registry);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveRuleSpringbootConfiguration.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveRuleSpringbootConfiguration.java
index 577b897..48d80a5 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveRuleSpringbootConfiguration.java
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveRuleSpringbootConfiguration.java
@@ -17,21 +17,33 @@
package org.apache.shardingsphere.masterslave.spring.boot;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.masterslave.algorithm.config.AlgorithmProvidedMasterSlaveRuleConfiguration;
+import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.masterslave.spring.boot.condition.MasterSlaveSpringBootCondition;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
+import org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper;
+import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
/**
* Rule spring boot configuration for master-slave.
*/
@Configuration
+@ConditionalOnClass(YamlMasterSlaveRuleConfiguration.class)
+@Conditional(MasterSlaveSpringBootCondition.class)
public class MasterSlaveRuleSpringbootConfiguration {
+ private final MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper swapper = new MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper();
+
/**
* YAML rule spring boot configuration for master-slave.
*
@@ -39,10 +51,35 @@ public class MasterSlaveRuleSpringbootConfiguration {
*/
@Bean
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.master-slave")
- @ConditionalOnClass(YamlMasterSlaveRuleConfiguration.class)
- @Conditional(MasterSlaveSpringBootCondition.class)
- public YamlRuleConfiguration masterSlave() {
+ public YamlMasterSlaveRuleConfiguration masterSlaveConfig() {
return new YamlMasterSlaveRuleConfiguration();
}
+
+ /**
+ * Master slave rule configuration for spring boot.
+ *
+ * @param yamlRuleConfiguration YAML master slave rule configuration
+ * @param loadBalanceAlgorithms load balance algorithms
+ * @return master slave rule configuration
+ */
+ @Bean
+ public RuleConfiguration masterSlaveRuleConfiguration(final YamlMasterSlaveRuleConfiguration yamlRuleConfiguration,
+ final ObjectProvider<Map<String, MasterSlaveLoadBalanceAlgorithm>> loadBalanceAlgorithms) {
+ AlgorithmProvidedMasterSlaveRuleConfiguration ruleConfiguration = swapper.swapToObject(yamlRuleConfiguration);
+ Map<String, MasterSlaveLoadBalanceAlgorithm> balanceAlgorithmMap = Optional.ofNullable(loadBalanceAlgorithms.getIfAvailable()).orElse(Collections.emptyMap());
+ ruleConfiguration.setLoadBalanceAlgorithms(balanceAlgorithmMap);
+ return ruleConfiguration;
+ }
+
+ /**
+ * Master slave algorithm provided bean registry.
+ *
+ * @param environment environment
+ * @return Master slave algorithm provided bean registry
+ */
+ @Bean
+ public static MasterSlaveAlgorithmProvidedBeanRegistry masterSlaveAlgorithmProvidedBeanRegistry(final Environment environment) {
+ return new MasterSlaveAlgorithmProvidedBeanRegistry(environment);
+ }
}
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveSpringBootAlgorithmProviderBeanTest.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveSpringBootAlgorit [...]
new file mode 100644
index 0000000..feb63dd
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/java/org/apache/shardingsphere/masterslave/spring/boot/MasterSlaveSpringBootAlgorithmProviderBeanTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.masterslave.spring.boot;
+
+import javax.annotation.Resource;
+import org.apache.shardingsphere.masterslave.spring.boot.fixture.RandomMasterSlaveLoadBalanceAlgorithmFixture;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(classes = MasterSlaveSpringBootAlgorithmProviderBeanTest.class)
+@SpringBootApplication
+@ActiveProfiles("masterslave")
+public class MasterSlaveSpringBootAlgorithmProviderBeanTest {
+
+ @Resource
+ private ApplicationContext applicationContext;
+
+ @Test
+ public void assertAlgorithmProviderBean() {
+ Object algorithmBean = applicationContext.getBean("fixture");
+ assertThat(algorithmBean.getClass().getName(), is(RandomMasterSlaveLoadBalanceAlgorithmFixture.class.getName()));
+ RandomMasterSlaveLoadBalanceAlgorithmFixture algorithmFixture = (RandomMasterSlaveLoadBalanceAlgorithmFixture) algorithmBean;
+ assertNotNull(algorithmFixture.getEnvironment());
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/java/org/apache/shardingsphere/masterslave/spring/boot/fixture/RandomMasterSlaveLoadBalanceAlgorithmFixture.java
similarity index 54%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
copy to shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/java/org/apache/shardingsphere/masterslave/spring/boot/fixture/RandomMasterSlaveLoadBalanceAlgorithmFixture.java
index d9f9747..82246e4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/java/org/apache/shardingsphere/masterslave/spring/boot/fixture/RandomMasterSlaveLoadBalanceAlgorithmFixture.java
@@ -15,22 +15,33 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.yaml.config.algorithm;
+package org.apache.shardingsphere.masterslave.spring.boot.fixture;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ThreadLocalRandom;
+import javax.annotation.Resource;
import lombok.Getter;
import lombok.Setter;
-import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
-
-import java.util.Properties;
+import org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm;
+import org.springframework.core.env.Environment;
-/**
- * ShardingSphere algorithm configuration for YAML.
- */
@Getter
@Setter
-public final class YamlShardingSphereAlgorithmConfiguration implements YamlConfiguration {
-
- private String type;
+public final class RandomMasterSlaveLoadBalanceAlgorithmFixture implements MasterSlaveLoadBalanceAlgorithm {
private Properties props = new Properties();
+
+ @Resource
+ private Environment environment;
+
+ @Override
+ public String getDataSource(final String name, final String masterDataSourceName, final List<String> slaveDataSourceNames) {
+ return slaveDataSourceNames.get(ThreadLocalRandom.current().nextInt(slaveDataSourceNames.size()));
+ }
+
+ @Override
+ public String getType() {
+ return "FIXTURE";
+ }
}
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm
similarity index 88%
copy from shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
copy to shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm
index ffe3544..7ec3ec8 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.masterslave.spi.MasterSlaveLoadBalanceAlgorithm
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper
+org.apache.shardingsphere.masterslave.spring.boot.fixture.RandomMasterSlaveLoadBalanceAlgorithmFixture
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/application-masterslave.properties
similarity index 90%
copy from shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
copy to shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/application-masterslave.properties
index 5c061de..85a1888 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/application-masterslave.properties
@@ -15,4 +15,6 @@
# limitations under the License.
#
-org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper
+
+spring.shardingsphere.rules.master-slave.load-balancers.fixture.type=FIXTURE
+
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/logback-test.xml b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..297e842
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/test/resources/logback-test.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0"?>
+<!--
+ ~ 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.
+ -->
+
+<configuration>
+ <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+ <logger name="org.apache.shardingsphere" level="info" additivity="false">
+ <appender-ref ref="console"/>
+ </logger>
+ <logger name="org.apache.shardingsphere.core.execute.threadlocal.ExecutorExceptionHandler" level="off" />
+ <logger name="org.apache.shardingsphere.infra.log.ConfigurationLogger" level="off" />
+ <logger name="ShardingSphere-metadata" level="off" />
+
+ <root>
+ <level value="error" />
+ <appender-ref ref="console" />
+ </root>
+</configuration>
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/main/java/org/apache/shardingsphere/shadow/spring/boot/ShadowRuleSpringBootConfiguration.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/main/java/org/apache/shardingsphere/shadow/spring/boot/ShadowRuleSpringBootConfiguration.java
index c104c3b..b8aa9a0 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/main/java/org/apache/shardingsphere/shadow/spring/boot/ShadowRuleSpringBootConfiguration.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-spring/shardingsphere-shadow-spring-boot-starter/src/main/java/org/apache/shardingsphere/shadow/spring/boot/ShadowRuleSpringBootConfiguration.java
@@ -17,9 +17,10 @@
package org.apache.shardingsphere.shadow.spring.boot;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.shadow.spring.boot.condition.ShadowSpringBootCondition;
import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.yaml.swapper.ShadowRuleConfigurationYamlSwapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
@@ -30,8 +31,12 @@ import org.springframework.context.annotation.Configuration;
* Shadow rule configuration for spring boot.
*/
@Configuration
+@ConditionalOnClass(YamlShadowRuleConfiguration.class)
+@Conditional(ShadowSpringBootCondition.class)
public class ShadowRuleSpringBootConfiguration {
+ private final ShadowRuleConfigurationYamlSwapper swapper = new ShadowRuleConfigurationYamlSwapper();
+
/**
* Shadow YAML rule spring boot configuration.
*
@@ -39,9 +44,18 @@ public class ShadowRuleSpringBootConfiguration {
*/
@Bean
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.shadow")
- @ConditionalOnClass(YamlShadowRuleConfiguration.class)
- @Conditional(ShadowSpringBootCondition.class)
- public YamlRuleConfiguration shadow() {
+ public YamlShadowRuleConfiguration shadow() {
return new YamlShadowRuleConfiguration();
}
+
+ /**
+ * Shadow rule configuration.
+ *
+ * @param yamlShadowRuleConfiguration YAML shadow rule configuration
+ * @return shadow rule configuration
+ */
+ @Bean
+ public RuleConfiguration shadowRuleConfiguration(final YamlShadowRuleConfiguration yamlShadowRuleConfiguration) {
+ return swapper.swapToObject(yamlShadowRuleConfiguration);
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java
index 0214639..7f4e1fa 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/algorithm/config/AlgorithmProvidedShardingRuleConfiguration.java
@@ -17,8 +17,12 @@
package org.apache.shardingsphere.sharding.algorithm.config;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
+import lombok.Setter;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
@@ -27,31 +31,51 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingS
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
-import java.util.Collection;
-import java.util.Map;
-
/**
* Algorithm provided sharding rule configuration.
*/
-@RequiredArgsConstructor
@Getter
+@Setter
public final class AlgorithmProvidedShardingRuleConfiguration implements RuleConfiguration {
- private final Collection<ShardingTableRuleConfiguration> tables;
+ private Collection<ShardingTableRuleConfiguration> tables = new LinkedList<>();
+
+ private Collection<ShardingAutoTableRuleConfiguration> autoTables = new LinkedList<>();
+
+ private Collection<String> bindingTableGroups = new LinkedList<>();
- private final Collection<ShardingAutoTableRuleConfiguration> autoTables;
+ private Collection<String> broadcastTables = new LinkedList<>();
- private final Collection<String> bindingTableGroups;
+ private ShardingStrategyConfiguration defaultDatabaseShardingStrategy;
- private final Collection<String> broadcastTables;
+ private ShardingStrategyConfiguration defaultTableShardingStrategy;
- private final ShardingStrategyConfiguration defaultDatabaseShardingStrategy;
+ private KeyGenerateStrategyConfiguration defaultKeyGenerateStrategy;
- private final ShardingStrategyConfiguration defaultTableShardingStrategy;
+ private Map<String, ShardingAlgorithm> shardingAlgorithms = new LinkedHashMap<>();
- private final KeyGenerateStrategyConfiguration defaultKeyGenerateStrategy;
+ private Map<String, KeyGenerateAlgorithm> keyGenerators = new LinkedHashMap<>();
- private final Map<String, ShardingAlgorithm> shardingAlgorithms;
+ public AlgorithmProvidedShardingRuleConfiguration() {
+ }
- private final Map<String, KeyGenerateAlgorithm> keyGenerators;
+ public AlgorithmProvidedShardingRuleConfiguration(final Collection<ShardingTableRuleConfiguration> tables,
+ final Collection<ShardingAutoTableRuleConfiguration> autoTables,
+ final Collection<String> bindingTableGroups,
+ final Collection<String> broadcastTables,
+ final ShardingStrategyConfiguration defaultDatabaseShardingStrategy,
+ final ShardingStrategyConfiguration defaultTableShardingStrategy,
+ final KeyGenerateStrategyConfiguration defaultKeyGenerateStrategy,
+ final Map<String, ShardingAlgorithm> shardingAlgorithms,
+ final Map<String, KeyGenerateAlgorithm> keyGenerators) {
+ this.tables = tables;
+ this.autoTables = autoTables;
+ this.bindingTableGroups = bindingTableGroups;
+ this.broadcastTables = broadcastTables;
+ this.defaultDatabaseShardingStrategy = defaultDatabaseShardingStrategy;
+ this.defaultTableShardingStrategy = defaultTableShardingStrategy;
+ this.defaultKeyGenerateStrategy = defaultKeyGenerateStrategy;
+ this.shardingAlgorithms = shardingAlgorithms;
+ this.keyGenerators = keyGenerators;
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
index f7a4436..810e9a7 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/constant/ShardingOrder.java
@@ -30,4 +30,9 @@ public final class ShardingOrder {
* Sharding order.
*/
public static final int ORDER = 0;
+
+ /**
+ * Algorithm provider sharding order.
+ */
+ public static final int ALGORITHM_PROVIDER_SHARDING_ORDER = 1;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
new file mode 100644
index 0000000..6cbf7a6
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/ShardingRuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -0,0 +1,125 @@
+/*
+ * 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.sharding.yaml.swapper;
+
+import java.util.Map.Entry;
+import org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
+import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.constant.ShardingOrder;
+import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingAutoTableRuleConfigurationYamlSwapper;
+import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingTableRuleConfigurationYamlSwapper;
+import org.apache.shardingsphere.sharding.yaml.swapper.strategy.KeyGenerateStrategyConfigurationYamlSwapper;
+import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingStrategyConfigurationYamlSwapper;
+
+/**
+ * Algorithm provider sharding rule configuration YAML swapper.
+ */
+public final class ShardingRuleAlgorithmProviderConfigurationYamlSwapper implements YamlRuleConfigurationSwapper<YamlShardingRuleConfiguration, AlgorithmProvidedShardingRuleConfiguration> {
+
+ private final ShardingTableRuleConfigurationYamlSwapper tableYamlSwapper = new ShardingTableRuleConfigurationYamlSwapper();
+
+ private final ShardingAutoTableRuleConfigurationYamlSwapper autoTableYamlSwapper = new ShardingAutoTableRuleConfigurationYamlSwapper();
+
+ private final ShardingStrategyConfigurationYamlSwapper shardingStrategyYamlSwapper = new ShardingStrategyConfigurationYamlSwapper();
+
+ private final KeyGenerateStrategyConfigurationYamlSwapper keyGenerateStrategyYamlSwapper = new KeyGenerateStrategyConfigurationYamlSwapper();
+
+ @Override
+ public YamlShardingRuleConfiguration swapToYamlConfiguration(final AlgorithmProvidedShardingRuleConfiguration data) {
+ YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration();
+ data.getTables().forEach(each -> result.getTables().put(each.getLogicTable(), tableYamlSwapper.swapToYamlConfiguration(each)));
+ data.getAutoTables().forEach(each -> result.getAutoTables().put(each.getLogicTable(), autoTableYamlSwapper.swapToYamlConfiguration(each)));
+ result.getBindingTables().addAll(data.getBindingTableGroups());
+ result.getBroadcastTables().addAll(data.getBroadcastTables());
+ setYamlDefaultStrategies(data, result);
+ setYamlAlgorithms(data, result);
+ return result;
+ }
+
+ @Override
+ public AlgorithmProvidedShardingRuleConfiguration swapToObject(final YamlShardingRuleConfiguration yamlConfiguration) {
+ AlgorithmProvidedShardingRuleConfiguration result = new AlgorithmProvidedShardingRuleConfiguration();
+ for (Entry<String, YamlTableRuleConfiguration> entry : yamlConfiguration.getTables().entrySet()) {
+ YamlTableRuleConfiguration tableRuleConfig = entry.getValue();
+ tableRuleConfig.setLogicTable(entry.getKey());
+ result.getTables().add(tableYamlSwapper.swapToObject(tableRuleConfig));
+ }
+ for (Entry<String, YamlShardingAutoTableRuleConfiguration> entry : yamlConfiguration.getAutoTables().entrySet()) {
+ YamlShardingAutoTableRuleConfiguration tableRuleConfig = entry.getValue();
+ tableRuleConfig.setLogicTable(entry.getKey());
+ result.getAutoTables().add(autoTableYamlSwapper.swapToObject(tableRuleConfig));
+ }
+ result.getBindingTableGroups().addAll(yamlConfiguration.getBindingTables());
+ result.getBroadcastTables().addAll(yamlConfiguration.getBroadcastTables());
+ setDefaultStrategies(yamlConfiguration, result);
+ return result;
+ }
+
+ private void setDefaultStrategies(final YamlShardingRuleConfiguration yamlConfiguration,
+ final AlgorithmProvidedShardingRuleConfiguration ruleConfiguration) {
+ if (null != yamlConfiguration.getDefaultDatabaseStrategy()) {
+ ruleConfiguration.setDefaultDatabaseShardingStrategy(shardingStrategyYamlSwapper.swapToObject(yamlConfiguration.getDefaultDatabaseStrategy()));
+ }
+ if (null != yamlConfiguration.getDefaultTableStrategy()) {
+ ruleConfiguration.setDefaultTableShardingStrategy(shardingStrategyYamlSwapper.swapToObject(yamlConfiguration.getDefaultTableStrategy()));
+ }
+ if (null != yamlConfiguration.getDefaultKeyGenerateStrategy()) {
+ ruleConfiguration.setDefaultKeyGenerateStrategy(keyGenerateStrategyYamlSwapper.swapToObject(yamlConfiguration.getDefaultKeyGenerateStrategy()));
+ }
+ }
+
+ @Override
+ public Class<AlgorithmProvidedShardingRuleConfiguration> getTypeClass() {
+ return AlgorithmProvidedShardingRuleConfiguration.class;
+ }
+
+ @Override
+ public String getRuleTagName() {
+ return "SHARDING";
+ }
+
+ @Override
+ public int getOrder() {
+ return ShardingOrder.ALGORITHM_PROVIDER_SHARDING_ORDER;
+ }
+
+ private void setYamlDefaultStrategies(final AlgorithmProvidedShardingRuleConfiguration data, final YamlShardingRuleConfiguration yamlConfiguration) {
+ if (null != data.getDefaultDatabaseShardingStrategy()) {
+ yamlConfiguration.setDefaultDatabaseStrategy(shardingStrategyYamlSwapper.swapToYamlConfiguration(data.getDefaultDatabaseShardingStrategy()));
+ }
+ if (null != data.getDefaultTableShardingStrategy()) {
+ yamlConfiguration.setDefaultTableStrategy(shardingStrategyYamlSwapper.swapToYamlConfiguration(data.getDefaultTableShardingStrategy()));
+ }
+ if (null != data.getDefaultKeyGenerateStrategy()) {
+ yamlConfiguration.setDefaultKeyGenerateStrategy(keyGenerateStrategyYamlSwapper.swapToYamlConfiguration(data.getDefaultKeyGenerateStrategy()));
+ }
+ }
+
+ private void setYamlAlgorithms(final AlgorithmProvidedShardingRuleConfiguration data, final YamlShardingRuleConfiguration yamlConfiguration) {
+ if (null != data.getShardingAlgorithms()) {
+ data.getShardingAlgorithms().forEach((key, value) -> yamlConfiguration.getShardingAlgorithms().put(key, YamlShardingSphereAlgorithmConfiguration.buildByTypedSPI(value)));
+ }
+ if (null != data.getKeyGenerators()) {
+ data.getKeyGenerators().forEach((key, value) -> yamlConfiguration.getKeyGenerators().put(key, YamlShardingSphereAlgorithmConfiguration.buildByTypedSPI(value)));
+ }
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
index e4ce553..647e77d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
@@ -15,4 +15,6 @@
# limitations under the License.
#
+org.apache.shardingsphere.sharding.yaml.swapper.ShardingRuleAlgorithmProviderConfigurationYamlSwapper
org.apache.shardingsphere.sharding.yaml.swapper.ShardingRuleConfigurationYamlSwapper
+
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingAlgorithmProvidedBeanRegistry.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingAlgorithmProvidedBeanRegistry.java
new file mode 100644
index 0000000..36a62e9
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingAlgorithmProvidedBeanRegistry.java
@@ -0,0 +1,50 @@
+/*
+ * 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.sharding.spring.boot;
+
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+import org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.core.env.Environment;
+
+/**
+ * Sharding algorithm provided bean registry.
+ */
+public class ShardingAlgorithmProvidedBeanRegistry extends AbstractAlgorithmProvidedBeanRegistry {
+
+ private static final String SHARDING_ALGORITHMS = "spring.shardingsphere.rules.sharding.sharding-algorithms.";
+
+ private static final String KEY_GENERATORS = "spring.shardingsphere.rules.sharding.key-generators.";
+
+ /**
+ * Instantiates a new Sharding algorithm provided bean registry.
+ *
+ * @param environment environment
+ */
+ public ShardingAlgorithmProvidedBeanRegistry(final Environment environment) {
+ super(environment);
+ }
+
+ @Override
+ public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry registry) throws BeansException {
+ registerBean(SHARDING_ALGORITHMS, ShardingAlgorithm.class, registry);
+ registerBean(KEY_GENERATORS, KeyGenerateAlgorithm.class, registry);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java
index 56e9f3a..a8f3f60 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java
@@ -17,31 +17,73 @@
package org.apache.shardingsphere.sharding.spring.boot;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.spring.boot.condition.ShardingSpringBootCondition;
import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.swapper.ShardingRuleAlgorithmProviderConfigurationYamlSwapper;
+import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
/**
* Sharding rule configuration for spring boot.
*/
@Configuration
+@ConditionalOnClass(YamlShardingRuleConfiguration.class)
+@Conditional(ShardingSpringBootCondition.class)
public class ShardingRuleSpringBootConfiguration {
+ private final ShardingRuleAlgorithmProviderConfigurationYamlSwapper swapper = new ShardingRuleAlgorithmProviderConfigurationYamlSwapper();
+
/**
* Sharding YAML rule spring boot configuration.
*
* @return YAML rule configuration
*/
@Bean
- @ConditionalOnClass(YamlShardingRuleConfiguration.class)
@ConfigurationProperties(prefix = "spring.shardingsphere.rules.sharding")
- @Conditional(ShardingSpringBootCondition.class)
- public YamlRuleConfiguration sharding() {
+ public YamlShardingRuleConfiguration shardingConfig() {
return new YamlShardingRuleConfiguration();
}
+
+ /**
+ * Sharding rule configuration.
+ *
+ * @param yamlShardingRuleConfiguration YAML sharding rule configuration
+ * @param shardingAlgorithmProvider sharding algorithm provider
+ * @param keyGenerateAlgorithmProvider key generate algorithm provider
+ * @return the rule configuration
+ */
+ @Bean
+ public RuleConfiguration shardingRuleConfiguration(final YamlShardingRuleConfiguration yamlShardingRuleConfiguration,
+ final ObjectProvider<Map<String, ShardingAlgorithm>> shardingAlgorithmProvider,
+ final ObjectProvider<Map<String, KeyGenerateAlgorithm>> keyGenerateAlgorithmProvider) {
+ Map<String, ShardingAlgorithm> shardingAlgorithmMap = Optional.ofNullable(shardingAlgorithmProvider.getIfAvailable()).orElse(Collections.emptyMap());
+ Map<String, KeyGenerateAlgorithm> keyGenerateAlgorithmMap = Optional.ofNullable(keyGenerateAlgorithmProvider.getIfAvailable()).orElse(Collections.emptyMap());
+ AlgorithmProvidedShardingRuleConfiguration ruleConfiguration = swapper.swapToObject(yamlShardingRuleConfiguration);
+ ruleConfiguration.setShardingAlgorithms(shardingAlgorithmMap);
+ ruleConfiguration.setKeyGenerators(keyGenerateAlgorithmMap);
+ return ruleConfiguration;
+ }
+
+ /**
+ * Sharding algorithm provided bean registry.
+ *
+ * @param environment environment
+ * @return sharding algorithm provided bean registry
+ */
+ @Bean
+ public static ShardingAlgorithmProvidedBeanRegistry shardingAlgorithmProvidedBeanRegistry(final Environment environment) {
+ return new ShardingAlgorithmProvidedBeanRegistry(environment);
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/ShardingSpringBootAlgorithmProviderBeanTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/ShardingSpringBootAlgorithmProviderBeanTest.java
new file mode 100644
index 0000000..73fc229
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/ShardingSpringBootAlgorithmProviderBeanTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.sharding.spring.boot;
+
+import javax.annotation.Resource;
+import org.apache.shardingsphere.sharding.spring.boot.fixture.ExtendInlineShardingAlgorithmFixture;
+import org.apache.shardingsphere.sharding.spring.boot.fixture.IncrementKeyGenerateAlgorithmFixture;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest(classes = ShardingSpringBootAlgorithmProviderBeanTest.class)
+@SpringBootApplication
+@ActiveProfiles("sharding")
+public class ShardingSpringBootAlgorithmProviderBeanTest {
+
+ @Resource
+ private ApplicationContext applicationContext;
+
+ @Test
+ public void assertAlgorithmProviderBean() {
+ Object algorithmBean = applicationContext.getBean("database_inline");
+ assertThat(algorithmBean.getClass().getName(), is(ExtendInlineShardingAlgorithmFixture.class.getName()));
+ ExtendInlineShardingAlgorithmFixture algorithmFixture = (ExtendInlineShardingAlgorithmFixture) algorithmBean;
+ assertNotNull(algorithmFixture.getEnvironment());
+ Object incrementKeyBean = applicationContext.getBean("incrementKey");
+ assertThat(incrementKeyBean.getClass().getName(), is(IncrementKeyGenerateAlgorithmFixture.class.getName()));
+ IncrementKeyGenerateAlgorithmFixture keyGenerateAlgorithmFixture = (IncrementKeyGenerateAlgorithmFixture) incrementKeyBean;
+ assertNotNull(keyGenerateAlgorithmFixture.getEnvironment());
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/fixture/ExtendInlineShardingAlgorithmFixture.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/fixture/ExtendInlineShardingAlgorithmFixture.java
new file mode 100644
index 0000000..157eadd
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/fixture/ExtendInlineShardingAlgorithmFixture.java
@@ -0,0 +1,89 @@
+/*
+ * 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.sharding.spring.boot.fixture;
+
+import com.google.common.base.Preconditions;
+import groovy.lang.Closure;
+import groovy.util.Expando;
+import java.util.Collection;
+import java.util.Properties;
+import javax.annotation.Resource;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineExpressionParser;
+import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
+import org.springframework.core.env.Environment;
+
+public final class ExtendInlineShardingAlgorithmFixture implements StandardShardingAlgorithm<Comparable<?>> {
+
+ private static final String ALGORITHM_EXPRESSION_KEY = "algorithm.expression";
+
+ private static final String ALLOW_RANGE_QUERY_KEY = "allow.range.query.with.inline.sharding";
+
+ private Closure<?> closure;
+
+ private boolean allowRangeQuery;
+
+ @Resource
+ @Getter
+ private Environment environment;
+
+ @Getter
+ @Setter
+ private Properties props = new Properties();
+
+ @Override
+ public void init() {
+ closure = createClosure();
+ allowRangeQuery = isAllowRangeQuery();
+ }
+
+ private Closure<?> createClosure() {
+ String expression = props.getProperty(ALGORITHM_EXPRESSION_KEY);
+ Preconditions.checkNotNull(expression, "Inline sharding algorithm expression cannot be null.");
+ String algorithmExpression = InlineExpressionParser.handlePlaceHolder(expression.trim());
+ Closure<?> result = new InlineExpressionParser(algorithmExpression).evaluateClosure().rehydrate(new Expando(), null, null);
+ result.setResolveStrategy(Closure.DELEGATE_ONLY);
+ return result;
+ }
+
+ private boolean isAllowRangeQuery() {
+ return Boolean.valueOf(props.getOrDefault(ALLOW_RANGE_QUERY_KEY, Boolean.FALSE.toString()).toString());
+ }
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Comparable<?>> shardingValue) {
+ closure.setProperty(shardingValue.getColumnName(), shardingValue.getValue());
+ return closure.call().toString();
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Comparable<?>> shardingValue) {
+ if (allowRangeQuery) {
+ return availableTargetNames;
+ }
+ throw new UnsupportedOperationException("Since the property of `allow.range.query.with.inline.sharding` is false, inline sharding algorithm can not tackle with range query.");
+ }
+
+ @Override
+ public String getType() {
+ return "EXTEND_INLINE";
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/fixture/IncrementKeyGenerateAlgorithmFixture.java
similarity index 55%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/fixture/IncrementKeyGenerateAlgorithmFixture.java
index d9f9747..ef1a640 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/java/org/apache/shardingsphere/sharding/spring/boot/fixture/IncrementKeyGenerateAlgorithmFixture.java
@@ -15,22 +15,39 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.yaml.config.algorithm;
+package org.apache.shardingsphere.sharding.spring.boot.fixture;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicInteger;
+import javax.annotation.Resource;
import lombok.Getter;
import lombok.Setter;
-import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
-
-import java.util.Properties;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
+import org.springframework.core.env.Environment;
-/**
- * ShardingSphere algorithm configuration for YAML.
- */
-@Getter
-@Setter
-public final class YamlShardingSphereAlgorithmConfiguration implements YamlConfiguration {
+public final class IncrementKeyGenerateAlgorithmFixture implements KeyGenerateAlgorithm {
- private String type;
+ private final AtomicInteger count = new AtomicInteger();
+ @Getter
+ @Setter
private Properties props = new Properties();
+
+ @Resource
+ @Getter
+ private Environment environment;
+
+ @Override
+ public void init() {
+ }
+
+ @Override
+ public Comparable<?> generateKey() {
+ return count.incrementAndGet();
+ }
+
+ @Override
+ public String getType() {
+ return "EXTEND_INCREMENT";
+ }
}
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm
similarity index 89%
copy from shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm
index ffe3544..b02c23a 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper
+org.apache.shardingsphere.sharding.spring.boot.fixture.IncrementKeyGenerateAlgorithmFixture
diff --git a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAlgorithm
similarity index 89%
copy from shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAlgorithm
index ffe3544..39b5bff 100644
--- a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAlgorithm
@@ -14,5 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
+
+org.apache.shardingsphere.sharding.spring.boot.fixture.ExtendInlineShardingAlgorithmFixture
-org.apache.shardingsphere.masterslave.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/application-sharding.properties b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/application-sharding.properties
new file mode 100644
index 0000000..773e5a9
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/application-sharding.properties
@@ -0,0 +1,27 @@
+#
+# 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.
+#
+
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=EXTEND_INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=EXTEND_INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=EXTEND_INLINE
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
+
+spring.shardingsphere.rules.sharding.key-generators.incrementKey.type=EXTEND_INCREMENT
+spring.shardingsphere.rules.sharding.key-generators.incrementKey.props.worker.id=123
+
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/logback-test.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..297e842
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/test/resources/logback-test.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0"?>
+<!--
+ ~ 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.
+ -->
+
+<configuration>
+ <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+ <logger name="org.apache.shardingsphere" level="info" additivity="false">
+ <appender-ref ref="console"/>
+ </logger>
+ <logger name="org.apache.shardingsphere.core.execute.threadlocal.ExecutorExceptionHandler" level="off" />
+ <logger name="org.apache.shardingsphere.infra.log.ConfigurationLogger" level="off" />
+ <logger name="ShardingSphere-metadata" level="off" />
+
+ <root>
+ <level value="error" />
+ <appender-ref ref="console" />
+ </root>
+</configuration>
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
index d9f9747..adf51ac 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.yaml.config.algorithm;
import lombok.Getter;
import lombok.Setter;
+import org.apache.shardingsphere.infra.spi.type.TypedSPI;
import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
import java.util.Properties;
@@ -33,4 +34,17 @@ public final class YamlShardingSphereAlgorithmConfiguration implements YamlConfi
private String type;
private Properties props = new Properties();
+
+ /**
+ * Build by typed spi.
+ *
+ * @param value typedSPI
+ * @return YAML sharding sphere algorithm configuration
+ */
+ public static YamlShardingSphereAlgorithmConfiguration buildByTypedSPI(final TypedSPI value) {
+ YamlShardingSphereAlgorithmConfiguration result = new YamlShardingSphereAlgorithmConfiguration();
+ result.setType(value.getType());
+ result.setProps(value.getProps());
+ return result;
+ }
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/SpringBootConfiguration.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/SpringBootConfiguration.java
index e5ab650..98e2378 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/SpringBootConfiguration.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/SpringBootConfiguration.java
@@ -17,11 +17,10 @@
package org.apache.shardingsphere.spring.boot;
+import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.spring.boot.datasource.DataSourceMapSetter;
import org.apache.shardingsphere.spring.boot.prop.SpringBootPropertiesConfiguration;
import org.apache.shardingsphere.spring.transaction.ShardingTransactionTypeScanner;
@@ -63,15 +62,14 @@ public class SpringBootConfiguration implements EnvironmentAware {
/**
* Get ShardingSphere data source bean.
*
- * @param rules YAML rules configuration
+ * @param rules rules configuration
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Autowired(required = false)
- public DataSource shardingSphereDataSource(final ObjectProvider<Collection<YamlRuleConfiguration>> rules) throws SQLException {
- Collection<RuleConfiguration> ruleConfigurations = new YamlRuleConfigurationSwapperEngine()
- .swapToRuleConfigurations(Optional.ofNullable(rules.getIfAvailable()).orElse(Collections.emptyList()));
+ public DataSource shardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules) throws SQLException {
+ Collection<RuleConfiguration> ruleConfigurations = Optional.ofNullable(rules.getIfAvailable()).orElse(Collections.emptyList());
return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, ruleConfigurations, props.getProps());
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-sharding.properties b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-sharding.properties
index 4179573..9716c80 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-sharding.properties
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-sharding.properties
@@ -55,11 +55,11 @@ spring.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.rules.sharding.broadcast-tables=t_config
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/orchestration/OrchestrationSpringBootConfiguration.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/orchestration/OrchestrationSpringB [...]
index 4ec6363..81faecf 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/orchestration/OrchestrationSpringBootConfiguration.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/orchestration/OrchestrationSpringBootConfiguration.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.spring.boot.orchestration;
import com.google.common.base.Preconditions;
+import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.cluster.configuration.swapper.ClusterConfigurationYamlSwapper;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
@@ -25,8 +26,6 @@ import org.apache.shardingsphere.driver.orchestration.internal.datasource.Orches
import org.apache.shardingsphere.spring.boot.orchestration.common.OrchestrationSpringBootRootConfiguration;
import org.apache.shardingsphere.spring.boot.orchestration.rule.LocalRulesCondition;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.center.yaml.config.YamlCenterRepositoryConfiguration;
@@ -49,7 +48,6 @@ import org.springframework.util.CollectionUtils;
import javax.sql.DataSource;
import java.sql.SQLException;
-import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -96,17 +94,16 @@ public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
/**
* Get orchestration ShardingSphere data source bean by local configuration.
*
+ * @param rules rules configuration
* @param orchestrationConfiguration orchestration configuration
- * @param rules YAML rules configuration
* @return orchestration sharding data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(LocalRulesCondition.class)
@Autowired(required = false)
- public DataSource localShardingSphereDataSource(final OrchestrationConfiguration orchestrationConfiguration, final ObjectProvider<Collection<YamlRuleConfiguration>> rules) throws SQLException {
- Collection<RuleConfiguration> ruleConfigurations = new YamlRuleConfigurationSwapperEngine()
- .swapToRuleConfigurations(Optional.ofNullable(rules.getIfAvailable()).orElse(Collections.emptyList()));
+ public DataSource localShardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules, final OrchestrationConfiguration orchestrationConfiguration) throws SQLException {
+ List<RuleConfiguration> ruleConfigurations = Optional.ofNullable(rules.getIfAvailable()).orElse(Collections.emptyList());
return null == root.getCluster() ? new OrchestrationShardingSphereDataSource(new ShardingSphereDataSource(dataSourceMap, ruleConfigurations, root.getProps()), orchestrationConfiguration)
: new OrchestrationShardingSphereDataSource(new ShardingSphereDataSource(dataSourceMap, ruleConfigurations, root.getProps()), orchestrationConfiguration,
new ClusterConfigurationYamlSwapper().swapToObject(root.getCluster()));
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/orchestration/type/OrchestrationSpringBootEncryptTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/orchestration/type/Orchestratio [...]
index 991b470..7817860 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/orchestration/type/OrchestrationSpringBootEncryptTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/orchestration/type/OrchestrationSpringBootEncryptTest.java
@@ -21,8 +21,8 @@ import lombok.SneakyThrows;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
-import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
-import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.spring.boot.orchestration.util.EmbedTestingServer;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -36,7 +36,6 @@ import javax.annotation.Resource;
import javax.sql.DataSource;
import java.lang.reflect.Field;
-import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -65,12 +64,11 @@ public class OrchestrationSpringBootEncryptTest {
BasicDataSource embedDataSource = (BasicDataSource) shardingSphereDataSource.getDataSourceMap().values().iterator().next();
assertThat(embedDataSource.getMaxTotal(), is(100));
assertThat(embedDataSource.getUsername(), is("sa"));
- EncryptRuleConfiguration configuration =
- (EncryptRuleConfiguration) shardingSphereDataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getConfigurations().iterator().next();
+ AlgorithmProvidedEncryptRuleConfiguration configuration =
+ (AlgorithmProvidedEncryptRuleConfiguration) shardingSphereDataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getConfigurations().iterator().next();
assertThat(configuration.getEncryptors().size(), is(1));
- ShardingSphereAlgorithmConfiguration encryptAlgorithmConfiguration = configuration.getEncryptors().get("order_encrypt");
- assertThat(encryptAlgorithmConfiguration, instanceOf(ShardingSphereAlgorithmConfiguration.class));
- assertThat(encryptAlgorithmConfiguration.getType(), is("AES"));
+ EncryptAlgorithm encryptAlgorithm = configuration.getEncryptors().get("order_encrypt");
+ assertThat(encryptAlgorithm.getType(), is("AES"));
assertThat(configuration.getTables().size(), is(1));
assertThat(configuration.getTables().iterator().next().getColumns().iterator().next().getCipherColumn(), is("cipher_order_id"));
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/resources/application-sharding.properties b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/resources/application-sharding.properties
index 6a937a2..1b550b2 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/resources/application-sharding.properties
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-orchestration-spring/shardingsphere-jdbc-orchestration-spring-boot-starter/src/test/resources/application-sharding.properties
@@ -57,11 +57,11 @@ spring.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.rules.sharding.broadcast-tables=t_config
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_${user_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_${order_id % 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/registry/AbstractAlgorithmProvidedBeanRegistry.java b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/registry/AbstractAlgorithmProvidedBeanRegistry.java
new file mode 100644
index 0000000..16a3b5a
--- /dev/null
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/registry/AbstractAlgorithmProvidedBeanRegistry.java
@@ -0,0 +1,92 @@
+/*
+ * 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.spring.boot.registry;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmPostProcessor;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.type.TypedSPI;
+import org.apache.shardingsphere.infra.spi.type.TypedSPIRegistry;
+import org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
+import org.springframework.core.env.Environment;
+
+/**
+ * Abstract algorithm provided bean registry.
+ */
+public abstract class AbstractAlgorithmProvidedBeanRegistry implements BeanDefinitionRegistryPostProcessor, BeanPostProcessor {
+
+ private final Environment environment;
+
+ /**
+ * Instantiates a new abstract algorithm provided bean registry.
+ *
+ * @param environment environment
+ */
+ public AbstractAlgorithmProvidedBeanRegistry(final Environment environment) {
+ this.environment = environment;
+ }
+
+ @SuppressWarnings("all")
+ protected void registerBean(final String preFix, final Class clazz, final BeanDefinitionRegistry registry) {
+ Map<String, Object> paramMap = PropertyUtil.handle(environment, preFix, Map.class);
+ Set<String> keySet = paramMap.keySet().stream().map(k -> k.substring(0, k.indexOf("."))).collect(Collectors.toSet());
+ Map<String, YamlShardingSphereAlgorithmConfiguration> shardingAlgorithmMap = new LinkedHashMap<>();
+ keySet.forEach(key -> {
+ String type = environment.getProperty(preFix + key + ".type");
+ Map<String, Object> propsMap = PropertyUtil.handle(environment, preFix + key + ".props", Map.class);
+ YamlShardingSphereAlgorithmConfiguration configuration = new YamlShardingSphereAlgorithmConfiguration();
+ configuration.setType(type);
+ configuration.getProps().putAll(propsMap);
+ shardingAlgorithmMap.put(key, configuration);
+ });
+ ShardingSphereServiceLoader.register(clazz);
+ shardingAlgorithmMap.forEach((k, v) -> {
+ TypedSPI typedSPI = TypedSPIRegistry.getRegisteredService(clazz, v.getType(), v.getProps());
+ BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(typedSPI.getClass());
+ builder.addPropertyValue("props", v.getProps());
+ registry.registerBeanDefinition(k, builder.getBeanDefinition());
+ });
+ }
+
+ @Override
+ public void postProcessBeanFactory(final ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
+ }
+
+ @Override
+ public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException {
+ return bean;
+ }
+
+ @Override
+ public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
+ if (bean instanceof ShardingSphereAlgorithmPostProcessor) {
+ ((ShardingSphereAlgorithmPostProcessor) bean).init();
+ }
+ return bean;
+ }
+}