Skip to content

Commit

Permalink
Refactor class-based-sharding-algorithm-example with ShardingSphereDr…
Browse files Browse the repository at this point in the history
…iver (#22534)
  • Loading branch information
Pace2Car committed Nov 30, 2022
1 parent bfbf944 commit 794a8de
Show file tree
Hide file tree
Showing 34 changed files with 948 additions and 526 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<artifactId>shardingsphere-jdbc-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Collection;
import java.util.Properties;

public final class ClassBasedDataSourceStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {
public final class ClassBasedDatabaseStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {

private static final String SHARDING_COUNT = "sharding-count";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,5 @@
# limitations under the License.
#

spring.shardingsphere.mode.type=Standalone
spring.shardingsphere.mode.repository.type=JDBC

spring.shardingsphere.datasource.names=ds-0,ds-1

spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=

spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=

spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=datasource-class-based
spring.shardingsphere.rules.sharding.binding-tables[0]=t_order,t_order_item
spring.shardingsphere.rules.sharding.broadcast-tables=t_address

spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds-$->{0..1}.t_order_$->{0..1}
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=t-order-class-based

spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake

spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds-$->{0..1}.t_order_item_$->{0..1}
spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column=order_id
spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name=t-order-item-class-based

spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake

spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.type=CLASS_BASED
spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.props.strategy=standard
spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.props.algorithmClassName=org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedDataSourceStandardShardingAlgorithmFixture
spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.props.sharding-count=2
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-class-based.type=CLASS_BASED
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-class-based.props.strategy=standard
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-class-based.props.algorithmClassName=org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedOrderStandardShardingAlgorithmFixture
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-class-based.props.sharding-count=2
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-class-based.type=CLASS_BASED
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-class-based.props.strategy=standard
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-class-based.props.algorithmClassName=org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedOrderItemStandardShardingAlgorithmFixture
spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-class-based.props.sharding-count=2

spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE

spring.shardingsphere.props.sql-show=true
spring.datasource.driver-class-name=org.apache.shardingsphere.driver.ShardingSphereDriver
spring.datasource.url=jdbc:shardingsphere:classpath:sharding-databases-tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,5 @@
# limitations under the License.
#

spring.shardingsphere.datasource.names=ds-0,ds-1

spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-0.username=root
spring.shardingsphere.datasource.ds-0.password=

spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds-1.username=root
spring.shardingsphere.datasource.ds-1.password=

spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=datasource-class-based
spring.shardingsphere.rules.sharding.binding-tables[0]=t_order,t_order_item
spring.shardingsphere.rules.sharding.broadcast-tables=t_address

spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds-$->{0..1}.t_order

spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake

spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds-$->{0..1}.t_order_item

spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake

spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.type=CLASS_BASED
spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.props.strategy=standard
spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.props.algorithmClassName=org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedDataSourceStandardShardingAlgorithmFixture
spring.shardingsphere.rules.sharding.sharding-algorithms.datasource-class-based.props.sharding-count=2

spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE

spring.shardingsphere.props.sql-show=true
spring.datasource.driver-class-name=org.apache.shardingsphere.driver.ShardingSphereDriver
spring.datasource.url=jdbc:shardingsphere:classpath:sharding-databases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# limitations under the License.
#

spring.datasource.driver-class-name=org.apache.shardingsphere.driver.ShardingSphereDriver
spring.datasource.url=jdbc:shardingsphere:classpath:sharding-tables.yaml

spring.shardingsphere.datasource.names=ds

spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#
# 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_$->{0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_class_based
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_$->{0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_item_class_based
keyGenerateStrategy:
column: order_item_id
keyGeneratorName: snowflake
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_address
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database_class_based

shardingAlgorithms:
database_class_based:
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedDatabaseStandardShardingAlgorithmFixture
sharding-count: 2
t_order_class_based:
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedOrderStandardShardingAlgorithmFixture
sharding-count: 2
t_order_item_class_based:
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedOrderItemStandardShardingAlgorithmFixture
sharding-count: 2

keyGenerators:
snowflake:
type: SNOWFLAKE

props:
spl-show: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# 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_class_based

shardingAlgorithms:
database_class_based:
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedDatabaseStandardShardingAlgorithmFixture
sharding-count: 2

keyGenerators:
snowflake:
type: SNOWFLAKE

props:
spl-show: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# 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:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:

rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds.t_order_$->{0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_class_based
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
t_order_item:
actualDataNodes: ds.t_order_item_$->{0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_item_class_based
keyGenerateStrategy:
column: order_item_id
keyGeneratorName: snowflake
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_address

shardingAlgorithms:
t_order_class_based:
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedOrderStandardShardingAlgorithmFixture
sharding-count: 2
t_order_item_class_based:
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: org.apache.shardingsphere.example.extension.classbased.sharding.spring.boot.jpa.fixture.ClassBasedOrderItemStandardShardingAlgorithmFixture
sharding-count: 2

keyGenerators:
snowflake:
type: SNOWFLAKE

props:
spl-show: true
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<artifactId>shardingsphere-jdbc-core</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Collection;
import java.util.Properties;

public final class ClassBasedDataSourceStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {
public final class ClassBasedDatabaseStandardShardingAlgorithmFixture implements StandardShardingAlgorithm<Integer> {

private static final String SHARDING_COUNT = "sharding-count";

Expand Down
Loading

0 comments on commit 794a8de

Please sign in to comment.