You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "liming0 (via GitHub)" <gi...@apache.org> on 2023/05/04 02:33:31 UTC

[GitHub] [shardingsphere] liming0 opened a new issue, #25445: Sharding-jdbc supports nacos configuration

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: notifications-unsubscribe@shardingsphere.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] github-actions[bot] commented on issue #25445: Sharding-jdbc supports nacos configuration

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on issue #25445:
URL: https://github.com/apache/shardingsphere/issues/25445#issuecomment-1682905063

   There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Sharding-jdbc supports nacos configuration [shardingsphere]

Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu commented on issue #25445:
URL: https://github.com/apache/shardingsphere/issues/25445#issuecomment-2212333895

   Due to no feedback from the author for an long period, I am closing this issue for now.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] boyjoy1127 commented on issue #25445: Sharding-jdbc supports nacos configuration

Posted by "boyjoy1127 (via GitHub)" <gi...@apache.org>.
boyjoy1127 commented on issue #25445:
URL: https://github.com/apache/shardingsphere/issues/25445#issuecomment-1585976327

   Will this issue be completed before June 15 which is the final date of version 5.4.0?


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Sharding-jdbc supports nacos configuration [shardingsphere]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on issue #25445:
URL: https://github.com/apache/shardingsphere/issues/25445#issuecomment-1973838871

   There hasn't been any activity on this issue recently, and in order to prioritize active issues, it will be marked as stale.


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] liming0 commented on issue #25445: Sharding-jdbc supports nacos configuration

Posted by "liming0 (via GitHub)" <gi...@apache.org>.
liming0 commented on issue #25445:
URL: https://github.com/apache/shardingsphere/issues/25445#issuecomment-1550958931

   > It seems that there have no `ShardingSphereDriverURLProvider.class` and `NacosDriverURLProvider.class` under 5.2.0.(the crash 5.3.x but who cares) so welcome to use override.. welcome
   > 
   > ```
   > @Slf4j
   > @SuppressWarnings({"PMD.ClassNamingShouldBeCamelRule"})
   > public final class ShardingSphereDriverURL {
   > 
   >     /**
   >      * naocs default group
   >      */
   >     private static final String DEFAULT_GROUP = "DEFAULT_GROUP";
   > 
   >     public ShardingSphereDriverURL(String url) {
   >         log.info("==== [ShardingSphereDriverURL.initing]: {}");
   >     }
   > 
   >     public byte[] toConfigurationBytes() {
   >         String config = getShardingConfig();
   >         Preconditions.checkArgument(config != null, "Nacos config [ archetype-sharding ] is Empty.");
   >         return config.getBytes(StandardCharsets.UTF_8);
   >     }
   > 
   >     public String getShardingConfig(){
   >         NacosConfig nacosConfig = SpringBeanContext.getContext().getBean(NacosConfig.class);
   >         Properties properties = new Properties();
   >         properties.put("serverAddr" , nacosConfig.getServerAddr());
   >         properties.put("username"   , nacosConfig.getUserName());
   >         properties.put("password"   , nacosConfig.getPassword());
   >         properties.put("namespace"  , nacosConfig.getNamespace());
   >         // for print log
   >         properties.put("nacosImport", nacosConfig.getShardingConf());
   >         try {
   >             log.info("==== [ShardingConfig.loading]: {} ", JSON.toJSONString(properties, SerializerFeature.PrettyFormat));
   >             ConfigService configService = NacosFactory.createConfigService(properties);
   >             String content = configService.getConfig(nacosConfig.getShardingConf(), DEFAULT_GROUP, 5000);
   >             log.info("==== [ShardingSphereDriverURL.inited]: {}");
   >             return content;
   >         }catch (NacosException e) {
   >             log.error("==== [ShardingSphereDriverURL.failed]: {}", e);
   >         }
   >         return null;
   >     }
   > 
   > 
   > }
   > ```
   
   Yes, it only came in 5.3.2,My modification is based on 5.3.2. The modification has been completed, but I encountered an unknown problem when merging, which has not been solved for the time being


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] zhaojinchao95 commented on issue #25445: Sharding-jdbc supports nacos configuration

Posted by "zhaojinchao95 (via GitHub)" <gi...@apache.org>.
zhaojinchao95 commented on issue #25445:
URL: https://github.com/apache/shardingsphere/issues/25445#issuecomment-1639937256

   @liming0 Hi, we can go on finish it?


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] vanniuner commented on issue #25445: Sharding-jdbc supports nacos configuration

Posted by "vanniuner (via GitHub)" <gi...@apache.org>.
vanniuner commented on issue #25445:
URL: https://github.com/apache/shardingsphere/issues/25445#issuecomment-1550929617

   It seems that there have no `ShardingSphereDriverURLProvider.class` and `NacosDriverURLProvider.class` under 5.2.0.(the crash 5.3.x but who cares)
   so welcome to use override.. welcome
   
   ```
   @Slf4j
   @SuppressWarnings({"PMD.ClassNamingShouldBeCamelRule"})
   public final class ShardingSphereDriverURL {
   
       /**
        * naocs default group
        */
       private static final String DEFAULT_GROUP = "DEFAULT_GROUP";
   
       public ShardingSphereDriverURL(String url) {
           log.info("==== [ShardingSphereDriverURL.initing]: {}");
       }
   
       public byte[] toConfigurationBytes() {
           String config = getShardingConfig();
           Preconditions.checkArgument(config != null, "Nacos config [ archetype-sharding ] is Empty.");
           return config.getBytes(StandardCharsets.UTF_8);
       }
   
       public String getShardingConfig(){
           NacosConfig nacosConfig = SpringBeanContext.getContext().getBean(NacosConfig.class);
           Properties properties = new Properties();
           properties.put("serverAddr" , nacosConfig.getServerAddr());
           properties.put("username"   , nacosConfig.getUserName());
           properties.put("password"   , nacosConfig.getPassword());
           properties.put("namespace"  , nacosConfig.getNamespace());
           // for print log
           properties.put("nacosImport", nacosConfig.getShardingConf());
           try {
               log.info("==== [ShardingConfig.loading]: {} ", JSON.toJSONString(properties, SerializerFeature.PrettyFormat));
               ConfigService configService = NacosFactory.createConfigService(properties);
               String content = configService.getConfig(nacosConfig.getShardingConf(), DEFAULT_GROUP, 5000);
               log.info("==== [ShardingSphereDriverURL.inited]: {}");
               return content;
           }catch (NacosException e) {
               log.error("==== [ShardingSphereDriverURL.failed]: {}", e);
           }
           return null;
       }
   
   
   }
   
   ```


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Sharding-jdbc supports nacos configuration [shardingsphere]

Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu closed issue #25445: Sharding-jdbc supports nacos configuration
URL: https://github.com/apache/shardingsphere/issues/25445


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org