You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/08/31 08:25:18 UTC
[shardingsphere] branch master updated: Support mode on spring boot
starter (#12126)
This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 7dcdbae Support mode on spring boot starter (#12126)
7dcdbae is described below
commit 7dcdbae00e395adad6c05a7a7e06b4ed73aae529
Author: zhaojinchao <33...@users.noreply.github.com>
AuthorDate: Tue Aug 31 16:24:50 2021 +0800
Support mode on spring boot starter (#12126)
* jdbc spring boot starter support mode
* update
* update
* update
* fixed
---
.../spring/boot/ShardingSphereAutoConfiguration.java | 20 ++++++++++++++++++--
.../boot/prop/SpringBootPropertiesConfiguration.java | 3 +++
.../additional-spring-configuration-metadata.json | 5 +++++
.../src/test/resources/application-common.properties | 2 ++
.../src/test/resources/application-jndi.properties | 2 ++
5 files changed, 30 insertions(+), 2 deletions(-)
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/ShardingSphereAutoConfiguration.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/ShardingSphereAutoConfiguration.java
index c3b3f17..a200d8a 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/ShardingSphereAutoConfiguration.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/ShardingSphereAutoConfiguration.java
@@ -17,9 +17,12 @@
package org.apache.shardingsphere.spring.boot;
+import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.swapper.mode.ModeConfigurationYamlSwapper;
import org.apache.shardingsphere.spring.boot.datasource.DataSourceMapSetter;
import org.apache.shardingsphere.spring.boot.prop.SpringBootPropertiesConfiguration;
import org.apache.shardingsphere.spring.boot.schema.SchemaNameSetter;
@@ -43,6 +46,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
/**
@@ -63,17 +67,29 @@ public class ShardingSphereAutoConfiguration implements EnvironmentAware {
private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();
/**
+ * Get mode configuration.
+ *
+ * @return mode configuration
+ */
+ @Bean
+ public ModeConfiguration modeConfiguration() {
+ Preconditions.checkState(Objects.nonNull(props.getMode()), "The mode configuration is invalid, please configure mode");
+ return new ModeConfigurationYamlSwapper().swapToObject(props.getMode());
+ }
+
+ /**
* Get ShardingSphere data source bean.
*
* @param rules rules configuration
+ * @param modeConfig mode configuration
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Autowired(required = false)
- public DataSource shardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules) throws SQLException {
+ public DataSource shardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules, final ModeConfiguration modeConfig) throws SQLException {
Collection<RuleConfiguration> ruleConfigs = Optional.ofNullable(rules.getIfAvailable()).orElse(Collections.emptyList());
- return ShardingSphereDataSourceFactory.createDataSource(schemaName, dataSourceMap, ruleConfigs, props.getProps());
+ return ShardingSphereDataSourceFactory.createDataSource(schemaName, modeConfig, dataSourceMap, ruleConfigs, props.getProps());
}
/**
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/prop/SpringBootPropertiesConfiguration.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/prop/SpringBootPropertiesConfiguration.java
index 691504b..cd62bf9 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/prop/SpringBootPropertiesConfiguration.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/prop/SpringBootPropertiesConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.spring.boot.prop;
import lombok.Getter;
import lombok.Setter;
+import org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Properties;
@@ -32,4 +33,6 @@ import java.util.Properties;
public final class SpringBootPropertiesConfiguration {
private Properties props = new Properties();
+
+ private YamlModeConfiguration mode;
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json
index 025a39a..2e661a4 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json
@@ -76,6 +76,11 @@
"description": "Customize shardingsphere properties."
},
{
+ "name": "spring.shardingsphere.model",
+ "type": "org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration",
+ "sourceType": "org.apache.shardingsphere.spring.boot.prop.SpringBootPropertiesConfiguration"
+ },
+ {
"name": "spring.shardingsphere.datasource.names",
"type": "java.lang.String",
"sourceType": "org.apache.shardingsphere.spring.boot.ShardingSphereAutoConfiguration"
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
index b739f4d..2d9832b 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
@@ -17,6 +17,8 @@
spring.shardingsphere.schema.name=logic_db
+spring.shardingsphere.mode.type=Memory
+
spring.shardingsphere.datasource.names=ds_${0..1}
spring.shardingsphere.datasource.ds_0.type=org.apache.shardingsphere.test.mock.MockedDataSource
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties
index 3c4ccbe..62eee25 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties
@@ -15,6 +15,8 @@
# limitations under the License.
#
+spring.shardingsphere.mode.type=Memory
+
spring.shardingsphere.datasource.names=jndi0,jndi1
spring.shardingsphere.datasource.jndi0.jndi-name=java:comp/env/jdbc/jndi0
spring.shardingsphere.datasource.jndi1.jndi-name=jdbc/jndi1