You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/03/03 02:21:35 UTC
[shardingsphere] branch master updated: Add apollo driver url provider (#24429)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 adff4e4e83f Add apollo driver url provider (#24429)
adff4e4e83f is described below
commit adff4e4e83fa22e6489dfe8f22824487c3a9754e
Author: Guocheng Tang <to...@apache.org>
AuthorDate: Fri Mar 3 10:21:27 2023 +0800
Add apollo driver url provider (#24429)
* Add apollo url provider
* Add apollo url provider
* update doc version
* update doc description
* for cr
* for cr
* update pom
---
docs/document/content/dev-manual/data-source.cn.md | 21 ++++++++--
docs/document/content/dev-manual/data-source.en.md | 19 +++++++--
jdbc/core/pom.xml | 6 +++
.../core/driver/spi/ApolloDriverURLProvider.java | 49 ++++++++++++++++++++++
...dbc.core.driver.ShardingSphereDriverURLProvider | 1 +
.../driver/ShardingSphereDriverURLManagerTest.java | 30 +++++++++++--
pom.xml | 8 ++++
7 files changed, 124 insertions(+), 10 deletions(-)
diff --git a/docs/document/content/dev-manual/data-source.cn.md b/docs/document/content/dev-manual/data-source.cn.md
index 5510b5a09b6..076dd433f37 100644
--- a/docs/document/content/dev-manual/data-source.cn.md
+++ b/docs/document/content/dev-manual/data-source.cn.md
@@ -113,7 +113,20 @@ ShardingSphere 驱动 URL 提供器
### 已知实现
-| *配置标识* | *详细说明* | *全限定类名* |
-|-----------------------------------------|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| jdbc:shardingsphere:classpath:<path> | 驱动的类路径加载器 | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.ClasspathDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathDriverURLProvider.java) |
-| jdbc:shardingsphere:absolutepath:<path> | 驱动的绝对路径加载器 | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.AbsolutePathDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathDriverURLProvider.java) |
+| *配置标识* | *详细说明* | *全限定类名* |
+|-----------------------------------------|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| jdbc:shardingsphere:classpath:<path> | 驱动的类路径加载器 | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.ClasspathDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathDriverURLProvider.java) |
+| jdbc:shardingsphere:absolutepath:<path> | 驱动的绝对路径加载器 | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.AbsolutePathDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathDriverURLProvider.java) |
+| jdbc:shardingsphere:apollo:<namespace> | 驱动的 Apollo 加载器 | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.ApolloDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloDriverURLProvider.java) |
+
+### 注意
+
+当您使用 Apollo 加载器时,需要添加对应的 apollo 的 pom 依赖,目前适配的版本为 `1.9.0` , 如下:
+
+```xml
+<dependency>
+ <groupId>com.ctrip.framework.apollo</groupId>
+ <artifactId>apollo-client</artifactId>
+ <version>${apollo.version}</version>
+</dependency>
+```
\ No newline at end of file
diff --git a/docs/document/content/dev-manual/data-source.en.md b/docs/document/content/dev-manual/data-source.en.md
index 52cf23ef569..44b6fe772fe 100644
--- a/docs/document/content/dev-manual/data-source.en.md
+++ b/docs/document/content/dev-manual/data-source.en.md
@@ -96,7 +96,20 @@ ShardingSphere driver URL provider
### Implementation classes
-| *Configuration Type* | *Description* | *Fully-qualified class name* |
-|-----------------------------------------|---------------------------------------| ---------------------------- |
-| jdbc:shardingsphere:classpath:<path> | The classpath driver URL provider | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.ClasspathDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathDriverURLProvider.java) |
+| *Configuration Type* | *Description* | *Fully-qualified class name* |
+|-----------------------------------------|---------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+| jdbc:shardingsphere:classpath:<path> | The classpath driver URL provider | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.ClasspathDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ClasspathDriverURLProvider.java) |
| jdbc:shardingsphere:absolutepath:<path> | The absolute path driver URL provider | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.AbsolutePathDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/AbsolutePathDriverURLProvider.java) |
+| jdbc:shardingsphere:apollo:<namespace> | The apollo driver URL provider | [`org.apache.shardingsphere.driver.jdbc.core.driver.spi.ApolloDriverURLProvider`](https://github.com/apache/shardingsphere/blob/master/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloDriverURLProvider.java) |
+
+### Notice
+
+When you use the Apollo driver url provider, you need to add the corresponding apollo pom dependency, currently available at version `1.9.0`, as follows:
+
+```xml
+<dependency>
+ <groupId>com.ctrip.framework.apollo</groupId>
+ <artifactId>apollo-client</artifactId>
+ <version>${apollo.version}</version>
+</dependency>
+```
\ No newline at end of file
diff --git a/jdbc/core/pom.xml b/jdbc/core/pom.xml
index a6bee6ec7b4..53afd2384e5 100644
--- a/jdbc/core/pom.xml
+++ b/jdbc/core/pom.xml
@@ -215,5 +215,11 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>com.ctrip.framework.apollo</groupId>
+ <artifactId>apollo-client</artifactId>
+ </dependency>
+
</dependencies>
</project>
diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloDriverURLProvider.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloDriverURLProvider.java
new file mode 100644
index 00000000000..7bc5febbc0f
--- /dev/null
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/spi/ApolloDriverURLProvider.java
@@ -0,0 +1,49 @@
+/*
+ * 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.spi;
+
+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 org.apache.commons.lang3.StringUtils;
+import org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * Apollo driver URL provider.
+ */
+public final class ApolloDriverURLProvider implements ShardingSphereDriverURLProvider {
+
+ private static final String APOLLO_TYPE = "apollo:";
+
+ @Override
+ public boolean accept(final String url) {
+ return StringUtils.isNotBlank(url) && url.contains(APOLLO_TYPE);
+ }
+
+ @Override
+ public byte[] getContent(final String url) {
+ String configPath = url.substring("jdbc:shardingsphere:".length(), url.contains("?") ? url.indexOf("?") : url.length());
+ String namespace = configPath.substring(APOLLO_TYPE.length());
+ Preconditions.checkArgument(!namespace.isEmpty(), "Apollo namespace is required in ShardingSphere driver URL.");
+ ConfigFile configFile = ConfigService.getConfigFile(namespace, ConfigFileFormat.YAML);
+ return configFile.getContent().getBytes(StandardCharsets.UTF_8);
+ }
+}
diff --git a/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider b/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider
index 527409ec260..5a03d04716e 100644
--- a/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider
+++ b/jdbc/core/src/main/resources/META-INF/services/org.apache.shardingsphere.driver.jdbc.core.driver.ShardingSphereDriverURLProvider
@@ -17,3 +17,4 @@
org.apache.shardingsphere.driver.jdbc.core.driver.spi.AbsolutePathDriverURLProvider
org.apache.shardingsphere.driver.jdbc.core.driver.spi.ClasspathDriverURLProvider
+org.apache.shardingsphere.driver.jdbc.core.driver.spi.ApolloDriverURLProvider
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURLManagerTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURLManagerTest.java
index 85c49641bca..319e409e4c3 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURLManagerTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/ShardingSphereDriverURLManagerTest.java
@@ -17,21 +17,35 @@
package org.apache.shardingsphere.driver.jdbc.core.driver;
+import com.ctrip.framework.apollo.ConfigFile;
+import com.ctrip.framework.apollo.ConfigService;
+import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import org.apache.shardingsphere.driver.jdbc.exception.syntax.DriverURLProviderNotFoundException;
-import org.junit.Test;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import java.nio.charset.StandardCharsets;
import java.util.Objects;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ConfigService.class)
public final class ShardingSphereDriverURLManagerTest {
private final int fooDriverConfigLength = 999;
- @Test(expected = DriverURLProviderNotFoundException.class)
+ @Test
public void assertNewConstructorWithEmptyURL() {
- ShardingSphereDriverURLManager.getContent("jdbc:shardingsphere:");
+ assertThrows(DriverURLProviderNotFoundException.class, () -> ShardingSphereDriverURLManager.getContent("jdbc:shardingsphere:"));
}
@Test
@@ -46,4 +60,14 @@ public final class ShardingSphereDriverURLManagerTest {
byte[] actual = ShardingSphereDriverURLManager.getContent("jdbc:shardingsphere:absolutepath:" + absolutePath);
assertThat(actual.length, is(fooDriverConfigLength));
}
+
+ @Test
+ public void assertToApolloConfigurationFile() {
+ ConfigFile configFile = mock(ConfigFile.class);
+ when(configFile.getContent()).thenReturn("config content");
+ when(ConfigService.getConfigFile(anyString(), any(ConfigFileFormat.class))).thenReturn(configFile);
+ String url = "jdbc:shardingsphere:apollo:namespace";
+ byte[] content = ShardingSphereDriverURLManager.getContent(url);
+ assertThat("config content".getBytes(StandardCharsets.UTF_8), is(content));
+ }
}
diff --git a/pom.xml b/pom.xml
index f187d332ed0..80ab49f2e3e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -149,6 +149,7 @@
<git-commit-id-plugin.version>4.9.10</git-commit-id-plugin.version>
<spotless-maven-plugin.version>2.22.1</spotless-maven-plugin.version>
<build-helper-maven-plugin.version>3.3.0</build-helper-maven-plugin.version>
+ <apollo-client.version>1.9.0</apollo-client.version>
</properties>
<dependencyManagement>
@@ -607,6 +608,13 @@
<artifactId>truffle-api</artifactId>
<version>${truffle-api.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>com.ctrip.framework.apollo</groupId>
+ <artifactId>apollo-client</artifactId>
+ <version>${apollo-client.version}</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
</dependencyManagement>