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:
[email protected]
For queries about this service, please contact Infrastructure at:
[email protected]