liming0 opened a new issue, #25445:
URL: https://github.com/apache/shardingsphere/issues/25445

   ## Question
   
   Support for nacos configuration has been achieved through SPI
   
   ## Todo
   
   - [ ] Add NacosDriverURLProvider.java files to 
org.apache.shardingsphere.driver.jdbc.core.driver.spi
   - [ ] Add the configuration of NacosDriverURLProvider in 
org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider
   - [ ] Add nacos dependency in pom file
   - [ ] Add configuration examples
   ### Detail
   NacosDriverURLProvider.java
   ```
   package org.apache.shardingsphere.driver.jdbc.core.driver.spi;
   
   import com.alibaba.nacos.api.NacosFactory;
   import com.alibaba.nacos.api.common.Constants;
   import com.alibaba.nacos.api.config.ConfigService;
   import com.alibaba.nacos.api.exception.NacosException;
   import com.google.common.base.Preconditions;
   import lombok.SneakyThrows;
   import org.apache.commons.lang3.StringUtils;
   import 
org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider;
   
   import java.nio.charset.StandardCharsets;
   import java.util.HashMap;
   import java.util.Map;
   import java.util.Properties;
   
   public class NacosDriverURLProvider implements 
ShardingSphereDriverURLProvider {
   
       private static final String PARAM_PREFIX = "?";
       private static final String NACOS_TYPE = "nacos:";
       private static final String SHRDING_URL_PREFIX = "jdbc:shardingsphere:";
   
       @Override
       public boolean accept(String url) {
           return StringUtils.isNotBlank(url) && url.contains(NACOS_TYPE);
       }
   
       @Override
       @SneakyThrows(NacosException.class)
       public byte[] getContent(String url) {
           Properties props = getNacosConfigByUrl(url);
           ConfigService configService = 
NacosFactory.createConfigService(props);
           String dataId = props.getProperty(Constants.DATAID);
           String config = configService.getConfig(dataId, 
props.getProperty(Constants.GROUP, Constants.DEFAULT_GROUP), 500);
           Preconditions.checkArgument(config != null, "Nacos config [" + 
dataId + "] is Empty.");
           return config.getBytes(StandardCharsets.UTF_8);
       }
   
       private Properties getNacosConfigByUrl(String url) {
           String nacosConfig = StringUtils.removeStart(url, SHRDING_URL_PREFIX 
+ NACOS_TYPE);
           Preconditions.checkArgument(nacosConfig.indexOf(PARAM_PREFIX) > 0, 
"Nacos param is required in ShardingSphere driver URL.");
           String dataId = StringUtils.substringBefore(nacosConfig, 
PARAM_PREFIX);
           //'withKeyValueSeparator(java.lang.String)' is marked unstable with 
@Beta
           //Map<String, String> confMap = 
Splitter.on("&").withKeyValueSeparator("=").split(nacosConfig);
           Map<String, String> confMap = 
getUrlParameters(StringUtils.substringAfter(nacosConfig, PARAM_PREFIX));
           Properties properties = new Properties();
           properties.putAll(confMap);
           properties.put(Constants.DATAID, dataId);
           return properties;
       }
   
       public static Map<String, String> getUrlParameters(String urlParam) {
           Map<String, String> mapRequest = new HashMap<>();
           String[] arrSplit;
           if (urlParam == null) {
               return mapRequest;
           }
           arrSplit = urlParam.split("[&]");
           for (String strSplit : arrSplit) {
               String[] arrSplitEqual;
               arrSplitEqual = strSplit.split("[=]");
               if (arrSplitEqual.length > 1) {
                   mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
               } else {
                   if (!"".equals(arrSplitEqual[0])) {
                       mapRequest.put(arrSplitEqual[0], "");
                   }
               }
           }
           return mapRequest;
       }
   
   }
   ```
   
org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider
   ```text
   org.apache.shardingsphere.driver.jdbc.core.driver.spi.NacosDriverURLProvider
   ```
   
   Add dependencies to pom files in shardingsphere
   ```
   <nacos-client.version>1.4.2</nacos-client.version>
   
   <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>${nacos-client.version}</version>
            <scope>provided</scope>
   </dependency>
   ```
   
   Add dependencies to pom files in shardingsphere-jdbc-core
   ```
   <dependency>
               <groupId>com.alibaba.nacos</groupId>
               <artifactId>nacos-client</artifactId>
   </dependency>
   ```
   Example
   ```
   url: 
jdbc:shardingsphere:nacos:sharding-config-test.yaml?serverAddr=172.16.11.45:8848&namespace=dev&group=DEFAULT_GROUP&username=nacos&password=nacos
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: 
[email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to