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 c83ed7b  Fixed database discovery feature when use springboot 
properties. (#14095)
c83ed7b is described below

commit c83ed7b17b1024df75b97fdd05edc62cf9ecd172
Author: zhaojinchao <[email protected]>
AuthorDate: Thu Dec 16 10:30:18 2021 +0800

    Fixed database discovery feature when use springboot properties. (#14095)
    
    * update
    
    * update pr_ds
    
    * fixed
    
    * update
---
 ...eAlgorithmProviderConfigurationYamlSwapper.java |  6 +++
 ...tabaseDiscoveryRuleSpringbootConfiguration.java | 30 +++++++++++++--
 ...baseDiscoveryAlgorithmProvidedBeanRegistry.java | 45 ++++++++++++++++++++++
 .../pom.xml                                        |  5 +++
 .../resources/conf/config-database-discovery.yaml  |  4 +-
 5 files changed, 85 insertions(+), 5 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapper.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapper.java
index 6927c0c..20358b8 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapper.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/yaml/swapper/DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapper.java
@@ -24,6 +24,7 @@ import 
org.apache.shardingsphere.dbdiscovery.constant.DatabaseDiscoveryOrder;
 import 
org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.yaml.config.rule.YamlDatabaseDiscoveryDataSourceRuleConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.yaml.config.rule.YamlDatabaseDiscoveryHeartBeatConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlShardingSphereAlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapper;
 
 import java.util.Collection;
@@ -47,12 +48,17 @@ public final class 
DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwappe
         if (null != data.getDiscoveryHeartbeats()) {
             data.getDiscoveryHeartbeats().forEach((key, value) -> 
result.getDiscoveryHeartbeats().put(key, swapToYamlConfiguration(value)));
         }
+        if (null != data.getDiscoveryTypes()) {
+            data.getDiscoveryTypes().forEach((key, value) -> 
result.getDiscoveryTypes().put(key, new 
YamlShardingSphereAlgorithmConfiguration(value.getType(), value.getProps())));
+        }
         return result;
     }
     
     private YamlDatabaseDiscoveryDataSourceRuleConfiguration 
swapToYamlConfiguration(final DatabaseDiscoveryDataSourceRuleConfiguration 
dataSourceRuleConfig) {
         YamlDatabaseDiscoveryDataSourceRuleConfiguration result = new 
YamlDatabaseDiscoveryDataSourceRuleConfiguration();
         result.setDataSourceNames(dataSourceRuleConfig.getDataSourceNames());
+        
result.setDiscoveryHeartbeatName(dataSourceRuleConfig.getDiscoveryHeartbeatName());
+        
result.setDiscoveryTypeName(dataSourceRuleConfig.getDiscoveryTypeName());
         return result;
     }
     
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-boot-starter/src/main/java/org/apache/shardingsphere/dbdiscovery/spring/boot/DatabaseDiscoveryRuleSpringbootConfiguration.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-boot-starter/src/main/java/org/apache/shardingsphere/dbdiscovery/spring/boot/DatabaseDiscoveryRuleSpringboo
 [...]
index 60d2eb9..9292cc1 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-boot-starter/src/main/java/org/apache/shardingsphere/dbdiscovery/spring/boot/DatabaseDiscoveryRuleSpringbootConfiguration.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-boot-starter/src/main/java/org/apache/shardingsphere/dbdiscovery/spring/boot/DatabaseDiscoveryRuleSpringbootConfiguration.java
@@ -18,16 +18,25 @@
 package org.apache.shardingsphere.dbdiscovery.spring.boot;
 
 import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.dbdiscovery.algorithm.config.AlgorithmProvidedDatabaseDiscoveryRuleConfiguration;
+import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
+import 
org.apache.shardingsphere.dbdiscovery.spring.boot.algorithm.DatabaseDiscoveryAlgorithmProvidedBeanRegistry;
 import 
org.apache.shardingsphere.dbdiscovery.spring.boot.rule.YamlDatabaseDiscoveryRuleSpringBootConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.spring.boot.condition.DatabaseDiscoverySpringBootCondition;
 import 
org.apache.shardingsphere.dbdiscovery.yaml.config.YamlDatabaseDiscoveryRuleConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.yaml.swapper.DatabaseDiscoveryRuleAlgorithmProviderConfigurationYamlSwapper;
 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.EnableConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Conditional;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
 
 /**
  * Rule spring boot configuration for data base discovery.
@@ -46,10 +55,25 @@ public class DatabaseDiscoveryRuleSpringbootConfiguration {
     /**
      * Database discovery rule configuration for spring boot.
      *
-     * @return database discovery rule configuration
+     * @param discoveryTypes database discovery types
+     * @return discovery rule configuration
+     */
+    @Bean
+    public RuleConfiguration discoveryRuleConfiguration(final 
ObjectProvider<Map<String, DatabaseDiscoveryType>> discoveryTypes) {
+        AlgorithmProvidedDatabaseDiscoveryRuleConfiguration result = 
swapper.swapToObject(yamlConfig.getDatabaseDiscovery());
+        Map<String, DatabaseDiscoveryType> discoveryTypeMap = 
Optional.ofNullable(discoveryTypes.getIfAvailable()).orElse(Collections.emptyMap());
+        result.setDiscoveryTypes(discoveryTypeMap);
+        return result;
+    }
+    
+    /**
+     * Database discovery algorithm provided bean registry.
+     *
+     * @param environment environment
+     * @return database discovery algorithm provided bean registry
      */
     @Bean
-    public RuleConfiguration databaseDiscoveryRuleConfiguration() {
-        return swapper.swapToObject(yamlConfig.getDatabaseDiscovery());
+    public static DatabaseDiscoveryAlgorithmProvidedBeanRegistry 
databaseDiscoveryAlgorithmProvidedBeanRegistry(final Environment environment) {
+        return new DatabaseDiscoveryAlgorithmProvidedBeanRegistry(environment);
     }
 }
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-boot-starter/src/main/java/org/apache/shardingsphere/dbdiscovery/spring/boot/algorithm/DatabaseDiscoveryAlgorithmProvidedBeanRegistry.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-boot-starter/src/main/java/org/apache/shardingsphere/dbdiscovery/spring/boot/algorithm/Database
 [...]
new file mode 100644
index 0000000..faacd44
--- /dev/null
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-boot-starter/src/main/java/org/apache/shardingsphere/dbdiscovery/spring/boot/algorithm/DatabaseDiscoveryAlgorithmProvidedBeanRegistry.java
@@ -0,0 +1,45 @@
+/*
+ * 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.dbdiscovery.spring.boot.algorithm;
+
+import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
+import 
org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.core.env.Environment;
+
+/**
+ * Database-discovery algorithm provided bean registry.
+ */
+public final class DatabaseDiscoveryAlgorithmProvidedBeanRegistry extends 
AbstractAlgorithmProvidedBeanRegistry<DatabaseDiscoveryType> {
+    
+    private static final String ALGORITHMS = 
"spring.shardingsphere.rules.database-discovery.discovery-types.";
+    
+    /**
+     * Instantiates a new database-discovery algorithm provided bean registry.
+     *
+     * @param environment environment
+     */
+    public DatabaseDiscoveryAlgorithmProvidedBeanRegistry(final Environment 
environment) {
+        super(environment);
+    }
+    
+    @Override
+    public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry 
registry) {
+        registerBean(ALGORITHMS, DatabaseDiscoveryType.class, registry);
+    }
+}
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/pom.xml
 
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/pom.xml
index d634dbf..140fd6f 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/pom.xml
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/pom.xml
@@ -39,6 +39,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
+            
<artifactId>shardingsphere-db-discovery-spring-boot-starter</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-encrypt-spring-boot-starter</artifactId>
             <version>${project.version}</version>
         </dependency>
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
index 1672d8e..287dd1d 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-database-discovery.yaml
@@ -56,7 +56,7 @@
 #rules:
 #- !DB_DISCOVERY
 #  dataSources:
-#    primary_replica_ds:
+#    pr_ds:
 #      dataSourceNames:
 #        - ds_0
 #        - ds_1
@@ -113,7 +113,7 @@
 #rules:
 #- !DB_DISCOVERY
 #  dataSources:
-#    primary_replica_ds:
+#    pr_ds:
 #      dataSourceNames:
 #        - ds_0
 #        - ds_1

Reply via email to