You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2023/01/07 03:30:45 UTC

[GitHub] [shardingsphere] fallsea opened a new issue, #23388: ShardingProxy5.3.0 + Apollo Implementation mode

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

   ## Override Driver Class
   
   `org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURL`
   
   
   ```java
   /*
    * 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.driver.jdbc.core.driver;
   
   import java.io.BufferedReader;
   import java.io.File;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.nio.charset.StandardCharsets;
   import java.nio.file.Files;
   import java.util.Objects;
   
   import com.ctrip.framework.apollo.ConfigFile;
   import com.ctrip.framework.apollo.ConfigService;
   import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
   import com.google.common.base.Preconditions;
   
   import lombok.SneakyThrows;
   
   /**
    * ShardingSphere driver URL.
    */
   public final class ShardingSphereDriverURL {
       
       private static final String CLASSPATH_TYPE = "classpath:";
   
       private static final String APOLLO_TYPE = "apollo://";
       
       private final String file;
       
       private final boolean inClasspath;
   
       private final boolean inApollo;
       
       public ShardingSphereDriverURL(final String url) {
           String configuredFile = url.substring("jdbc:shardingsphere:".length(), url.contains("?") ? url.indexOf("?") : url.length());
           if (configuredFile.startsWith(APOLLO_TYPE)) {
           	file = configuredFile.substring(APOLLO_TYPE.length());
           	inApollo = true;
           	inClasspath = false;
           }else if (configuredFile.startsWith(CLASSPATH_TYPE)) {
               file = configuredFile.substring(CLASSPATH_TYPE.length());
               inClasspath = true;
               inApollo = false;
           } else {
               file = configuredFile;
               inClasspath = false;
               inApollo = false;
           }
           Preconditions.checkArgument(!file.isEmpty(), "Configuration file is required in ShardingSphere driver URL.");
       }
       
       /**
        * Generate to configuration bytes.
        * 
        * @return generated configuration bytes
        */
       @SneakyThrows(IOException.class)
       public byte[] toConfigurationBytes() {
       	if(inApollo) {
       		int index = file.lastIndexOf('.');
       		String namespace = file;
       		ConfigFileFormat configFileFormat = ConfigFileFormat.Properties;
           	if(index>-1) {
           		namespace = file.substring(0,index);
           		configFileFormat = ConfigFileFormat.fromString(file.substring(index+1));
           	}
       		ConfigFile configFile = ConfigService.getConfigFile(namespace,configFileFormat);
       		return configFile.getContent().getBytes(StandardCharsets.UTF_8);
       	}else {
       		try (InputStream stream = inClasspath ? ShardingSphereDriverURL.class.getResourceAsStream("/" + file) : Files.newInputStream(new File(file).toPath())) {
       			Objects.requireNonNull(stream, String.format("Can not find configuration file `%s`.", file));
       			BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
       			StringBuilder builder = new StringBuilder();
       			String line;
       			while ((line = reader.readLine()) != null) {
       				if (!line.startsWith("#")) {
       					builder.append(line).append('\n');
       				}
       			}
       			return builder.toString().getBytes(StandardCharsets.UTF_8);
       		}
       	}
       }
   }
   
   ```
   
   ## Apollo Configuration
   
   ```yaml
   spring:
     datasource: 
       driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
       url: jdbc:shardingsphere:apollo://sharding.txt
   ```


-- 
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] totalo commented on issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

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

   > @totalo Of course. You can make a simple design first, and then add the content to the issue.
   
   Ok~


-- 
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] strongduanmu commented on issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

Posted by GitBox <gi...@apache.org>.
strongduanmu commented on issue #23388:
URL: https://github.com/apache/shardingsphere/issues/23388#issuecomment-1374404200

   Hi @fallsea, can we design the reading of configuration files as an SPI? ShardingSphere provides classpath and apollo implementations by default. Users can configure different url formats to load different implementation classes.


-- 
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] totalo commented on issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

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

   Can I help solve this problem?


-- 
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] strongduanmu closed issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

Posted by "strongduanmu (via GitHub)" <gi...@apache.org>.
strongduanmu closed issue #23388: ShardingProxy5.3.0 + Apollo  Implementation mode
URL: https://github.com/apache/shardingsphere/issues/23388


-- 
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] totalo commented on issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

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

   > 
   
   Ok.


-- 
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] strongduanmu commented on issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

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

   @totalo Of course. You can make a simple design first, and then add the content to the issue.


-- 
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] totalo commented on issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

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

   > @totalo Of course. You can make a simple design first, and then add the content to the issue.
   
   ![Untitled drawio (1)](https://user-images.githubusercontent.com/29777558/218926209-14e4a6ce-d3d2-4871-881f-0290aac533b3.png)
   
   - Take out a `ShardingsphereDriverURLProvider` spi, there are two main methods, `accept` method determine whether is suitable for the current Provider, `getContent` users achieve specific access to content.
   - `ShardingSphereDriverURLManager` by loading the specific SPI implementation, through the accept method determine whether is suitable for the current content, use the implementation class satisfy the conditions for content of logic. At present there are two main implementation is a `ClasspathDriverURLProvider` used to load the class path of files, another is `ApolloDriverURLProvider` loading Apollo related documents.


-- 
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] tristaZero commented on issue #23388: ShardingProxy5.3.0 + Apollo Implementation mode

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

   Sound cool!


-- 
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