You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/06/26 05:46:19 UTC
[shardingsphere] branch master updated: Add class based sharding
algorithm example (#10994)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 940e053 Add class based sharding algorithm example (#10994)
940e053 is described below
commit 940e053f45b5c1e2acd3c18bb9baf88c2b244b4f
Author: Raigor <ra...@gmail.com>
AuthorDate: Sat Jun 26 13:45:39 2021 +0800
Add class based sharding algorithm example (#10994)
---
examples/pom.xml | 5 --
.../pom.xml | 22 ++++---
.../YamlClassBasedShardingAlgorithmExample.java | 42 +++++++++++++
.../classbased/factory/YamlDataSourceFactory.java | 42 +++++++++++++
...ClassBasedStandardShardingAlgorithmFixture.java | 51 ++++++++++++++++
.../resources/META-INF/sharding-databases.yaml | 70 ++++++++++++++++++++++
.../src/main/resources/logback.xml} | 34 +++++------
.../custom-sharding-algortihm-example}/pom.xml | 11 ++--
.../{ => extension-example}/pom.xml | 11 ++--
examples/shardingsphere-jdbc-example/pom.xml | 1 +
10 files changed, 242 insertions(+), 47 deletions(-)
diff --git a/examples/pom.xml b/examples/pom.xml
index ba24f3d..dd1863c 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -150,11 +150,6 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
- <artifactId>shardingsphere-governance-repository-nacos</artifactId>
- <version>${shardingsphere.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-governance-repository-etcd</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
diff --git a/examples/shardingsphere-jdbc-example/pom.xml b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/pom.xml
similarity index 70%
copy from examples/shardingsphere-jdbc-example/pom.xml
copy to examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/pom.xml
index 44ec4d2..1006358 100644
--- a/examples/shardingsphere-jdbc-example/pom.xml
+++ b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/pom.xml
@@ -22,17 +22,21 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere.example</groupId>
- <artifactId>shardingsphere-example</artifactId>
+ <artifactId>custom-sharding-algortihm-example</artifactId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-jdbc-example</artifactId>
- <packaging>pom</packaging>
+ <artifactId>class-based-sharding-algorithm-example</artifactId>
<name>${project.artifactId}</name>
- <modules>
- <module>sharding-example</module>
- <module>governance-example</module>
- <module>transaction-example</module>
- <module>other-feature-example</module>
- </modules>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere.example</groupId>
+ <artifactId>example-raw-jdbc</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core</artifactId>
+ </dependency>
+ </dependencies>
</project>
diff --git a/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/YamlClassBasedShardingAlgorithmExample.java b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/Ya [...]
new file mode 100644
index 0000000..cac71e0
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/YamlClassBasedShardingAlgorithmExample.java
@@ -0,0 +1,42 @@
+/*
+ * 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.example.extension.sharding.algortihm.classbased;
+
+import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
+import org.apache.shardingsphere.example.core.api.service.ExampleService;
+import org.apache.shardingsphere.example.core.jdbc.service.OrderServiceImpl;
+import org.apache.shardingsphere.example.extension.sharding.algortihm.classbased.factory.YamlDataSourceFactory;
+import org.apache.shardingsphere.example.type.ShardingType;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.sql.SQLException;
+
+public final class YamlClassBasedShardingAlgorithmExample {
+
+ private static ShardingType shardingType = ShardingType.SHARDING_DATABASES;
+
+ public static void main(final String[] args) throws SQLException, IOException {
+ DataSource dataSource = YamlDataSourceFactory.newInstance(shardingType);
+ ExampleExecuteTemplate.run(getExampleService(dataSource));
+ }
+
+ private static ExampleService getExampleService(final DataSource dataSource) {
+ return new OrderServiceImpl(dataSource);
+ }
+}
diff --git a/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/factory/YamlDataSourceFactory.java b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/factory/Yam [...]
new file mode 100644
index 0000000..0c593cf
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/factory/YamlDataSourceFactory.java
@@ -0,0 +1,42 @@
+/*
+ * 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.example.extension.sharding.algortihm.classbased.factory;
+
+import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.example.type.ShardingType;
+
+import javax.sql.DataSource;
+import java.io.File;
+import java.io.IOException;
+import java.sql.SQLException;
+
+public final class YamlDataSourceFactory {
+
+ public static DataSource newInstance(final ShardingType shardingType) throws SQLException, IOException {
+ switch (shardingType) {
+ case SHARDING_DATABASES:
+ return YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-databases.yaml"));
+ default:
+ throw new UnsupportedOperationException(shardingType.name());
+ }
+ }
+
+ private static File getFile(final String fileName) {
+ return new File(YamlDataSourceFactory.class.getResource(fileName).getFile());
+ }
+}
diff --git a/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/fixture/ClassBasedStandardShardingAlgorithmFixture.java b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/c [...]
new file mode 100644
index 0000000..7125216
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/java/org/apache/shardingsphere/example/extension/sharding/algortihm/classbased/fixture/ClassBasedStandardShardingAlgorithmFixture.java
@@ -0,0 +1,51 @@
+/*
+ * 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.example.extension.sharding.algortihm.classbased.fixture;
+
+import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
+
+import java.util.Collection;
+
+public final class ClassBasedStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {
+
+ @Override
+ public void init() {
+ }
+
+ @Override
+ public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
+ for (String each : availableTargetNames) {
+ if (each.endsWith(String.valueOf(shardingValue.getValue() % 2))) {
+ return each;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Collection<String> doSharding(final Collection<String> availableTargetNames, final RangeShardingValue<Integer> shardingValue) {
+ return availableTargetNames;
+ }
+
+ @Override
+ public String getType() {
+ return null;
+ }
+}
diff --git a/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/META-INF/sharding-databases.yaml b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/META-INF/sharding-databases.yaml
new file mode 100644
index 0000000..3804eb5
--- /dev/null
+++ b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/META-INF/sharding-databases.yaml
@@ -0,0 +1,70 @@
+#
+# 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.
+#
+
+dataSources:
+ ds_0:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.jdbc.Driver
+ jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+ username: root
+ password:
+ ds_1:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.jdbc.Driver
+ jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+ username: root
+ password:
+
+rules:
+- !SHARDING
+ tables:
+ t_order:
+ actualDataNodes: ds_${0..1}.t_order
+ keyGenerateStrategy:
+ column: order_id
+ keyGeneratorName: snowflake
+ t_order_item:
+ actualDataNodes: ds_${0..1}.t_order_item
+ keyGenerateStrategy:
+ column: order_item_id
+ keyGeneratorName: snowflake
+ bindingTables:
+ - t_order,t_order_item
+ broadcastTables:
+ - t_address
+ defaultDatabaseStrategy:
+ standard:
+ shardingColumn: user_id
+ shardingAlgorithmName: database_classbased
+ defaultTableStrategy:
+ none:
+
+ shardingAlgorithms:
+ database_classbased:
+ type: CLASS_BASED
+ props:
+ strategy: standard
+ algorithmClassName: org.apache.shardingsphere.example.extension.sharding.algortihm.classbased.fixture.ClassBasedStandardShardingAlgorithmFixture
+
+ keyGenerators:
+ snowflake:
+ type: SNOWFLAKE
+ props:
+ worker-id: 123
+
+props:
+ sql-show: false
diff --git a/examples/shardingsphere-jdbc-example/pom.xml b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/logback.xml
similarity index 52%
copy from examples/shardingsphere-jdbc-example/pom.xml
copy to examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/logback.xml
index 44ec4d2..ff5d027 100644
--- a/examples/shardingsphere-jdbc-example/pom.xml
+++ b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/class-based-sharding-algorithm-example/src/main/resources/logback.xml
@@ -16,23 +16,19 @@
~ limitations under the License.
-->
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.apache.shardingsphere.example</groupId>
- <artifactId>shardingsphere-example</artifactId>
- <version>5.0.0-RC1-SNAPSHOT</version>
- </parent>
- <artifactId>shardingsphere-jdbc-example</artifactId>
- <packaging>pom</packaging>
- <name>${project.artifactId}</name>
+<configuration>
+ <property name="log.context.name" value="shardingsphere-jdbc-api-quickstart" />
+ <property name="log.charset" value="UTF-8" />
+ <property name="log.pattern" value="[%-5level] %date --%thread-- [%logger] %msg %n" />
+ <contextName>${log.context.name}</contextName>
- <modules>
- <module>sharding-example</module>
- <module>governance-example</module>
- <module>transaction-example</module>
- <module>other-feature-example</module>
- </modules>
-</project>
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder charset="${log.charset}">
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ </appender>
+ <root>
+ <level value="INFO" />
+ <appender-ref ref="STDOUT" />
+ </root>
+</configuration>
diff --git a/examples/shardingsphere-jdbc-example/pom.xml b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/pom.xml
similarity index 82%
copy from examples/shardingsphere-jdbc-example/pom.xml
copy to examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/pom.xml
index 44ec4d2..a6197ab 100644
--- a/examples/shardingsphere-jdbc-example/pom.xml
+++ b/examples/shardingsphere-jdbc-example/extension-example/custom-sharding-algortihm-example/pom.xml
@@ -22,17 +22,14 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere.example</groupId>
- <artifactId>shardingsphere-example</artifactId>
+ <artifactId>extension-example</artifactId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-jdbc-example</artifactId>
- <packaging>pom</packaging>
+ <artifactId>custom-sharding-algortihm-example</artifactId>
<name>${project.artifactId}</name>
+ <packaging>pom</packaging>
<modules>
- <module>sharding-example</module>
- <module>governance-example</module>
- <module>transaction-example</module>
- <module>other-feature-example</module>
+ <module>class-based-sharding-algorithm-example</module>
</modules>
</project>
diff --git a/examples/shardingsphere-jdbc-example/pom.xml b/examples/shardingsphere-jdbc-example/extension-example/pom.xml
similarity index 82%
copy from examples/shardingsphere-jdbc-example/pom.xml
copy to examples/shardingsphere-jdbc-example/extension-example/pom.xml
index 44ec4d2..ba67c96 100644
--- a/examples/shardingsphere-jdbc-example/pom.xml
+++ b/examples/shardingsphere-jdbc-example/extension-example/pom.xml
@@ -22,17 +22,14 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere.example</groupId>
- <artifactId>shardingsphere-example</artifactId>
+ <artifactId>shardingsphere-jdbc-example</artifactId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
- <artifactId>shardingsphere-jdbc-example</artifactId>
- <packaging>pom</packaging>
+ <artifactId>extension-example</artifactId>
<name>${project.artifactId}</name>
+ <packaging>pom</packaging>
<modules>
- <module>sharding-example</module>
- <module>governance-example</module>
- <module>transaction-example</module>
- <module>other-feature-example</module>
+ <module>custom-sharding-algortihm-example</module>
</modules>
</project>
diff --git a/examples/shardingsphere-jdbc-example/pom.xml b/examples/shardingsphere-jdbc-example/pom.xml
index 44ec4d2..60fd670 100644
--- a/examples/shardingsphere-jdbc-example/pom.xml
+++ b/examples/shardingsphere-jdbc-example/pom.xml
@@ -34,5 +34,6 @@
<module>governance-example</module>
<module>transaction-example</module>
<module>other-feature-example</module>
+ <module>extension-example</module>
</modules>
</project>