ShardingSphere-JDBC水平分库分表
ShardingSphere-JDBC水平分库分表非常简单,只需要添加ShardingSphere-JDBC依赖并进行配置即可
1 2 3 4 5 6
| <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency>
|
水平分库配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| spring: application: name: sharding-jdbc-test shardingsphere: datasource: names: ds0,ds1 ds0: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?useSSL=false username: root password: 123456 ds1: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis1?useSSL=false username: root password: 123456 sharding: tables: classes: database-strategy: inline: sahrding-column: db algorithm-expression: ds$->{id % 2} actual-data-nodes: ds$->{0..1}.classes_$->{0..1} key-generator: column: id type: SNOWFLAKE table-strategy: inline: sharding-column: id algorithm-expression: classes_$->{id % 2} props: sql: show: true
|
水平分表配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| spring: application: name: sharding-jdbc-test shardingsphere: datasource: names: ds0 ds0: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?useSSL=false username: root password: 123456 sharding: tables: classes: actual-data-nodes: ds0.classes_$->{0..1} key-generator: column: id type: SNOWFLAKE table-strategy: inline: sharding-column: id algorithm-expression: classes_$->{id % 2} props: sql: show: true
|