Sharding jdbc sub-database and sub-table
There are three libraries in total, m(mian), ds0, ds1. Among them, the m library is not divided into databases and tables. ds0 and ds1 are divided into databases and tables. Each database has three tables, that is, 2 databases and 6 tables. surface
Database structure
├─192.168.43.200
│ ├─dsm
│ │ └─leader
│ ├─ds0
│ │ └─order_000
│ │ └─order_001
│ │ └─order_002
│ ├─ds1
│ │ └─order_000
│ │ └─order_001
│ │ └─order_002
│ │ │
rely
pom.xml
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.0.0.M3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
If there is a conflict at startup, add this to the configuration file
spring.main.allow-bean-definition-overriding=true
Row expression sharding strategy
Corresponds to InlineShardingStrategy. Using Groovy expressions, it provides support for sharding operations of = and IN in SQL statements, and only supports single sharding keys. For simple sharding algorithms, it can be used through simple configuration, thereby avoiding tedious Java code development, such as: t_order_000$->{order_id % 3}
indicating that t_order_000
the table is divided according to order_id
the module 3
, 3张表
the table name is t_order_0000
to t_order_0002
.
Just write the configuration file
###########################Sharding Jdbc配置###########################
sharding.jdbc.datasource.names=m,ds0,ds1
sharding.jdbc.datasource.m.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.m.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.m.url=jdbc:mysql://192.168.79.131:3306/grp_leader
sharding.jdbc.datasource.m.username=root
sharding.jdbc.datasource.m.password=123456
sharding.jdbc.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds0.url=jdbc:mysql://192.168.79.131:3306/grp_manage_01
sharding.jdbc.datasource.ds0.username=root
sharding.jdbc.datasource.ds0.password=123456
sharding.jdbc.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds1.url=jdbc:mysql://192.168.79.131:3306/grp_manage_02
sharding.jdbc.datasource.ds1.username=root
sharding.jdbc.datasource.ds1.password=123456
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_000$->{0..1}
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_000$->{order_id % 3}
sharding.jdbc.config.sharding.tables.leader.actual-data-nodes=m.leader
sharding.jdbc.config.sharding.tables.leader.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.leader.table-strategy.inline.algorithm-expression=leader
sharding.jdbc.config.sharding.props.sql.show=true
Standard sharding strategy
Corresponds to StandardShardingStrategy. Provides support for sharding operations for =, >, <, >=, <=, IN and BETWEEN AND in SQL statements. StandardShardingStrategy only supports single sharding keys and provides two sharding algorithms: PreciseShardingAlgorithm and RangeShardingAlgorithm. PreciseShardingAlgorithm is required and is used to handle = and IN sharding. RangeShardingAlgorithm is optional and is used to process BETWEEN AND, >, <, >=, <= sharding. If RangeShardingAlgorithm is not configured, BETWEEN AND in SQL will be processed according to the full library routing.
PreciseShardingAlgorithm
Customized precise database and table sharding strategy
Here, the sub-database and table are all using the mod operation and the row expression sharding strategy above (two strategies can be set for sub-database and table).
/**
* sharding jdbc 精准 `分库` 策略
* */
@Slf4j
public class PreciseShardingDatabaseAlgorithm implements PreciseShardingAlgorithm<Long> {
/**
* @param collection 拆分的所有库名 比如 [ds0,ds1]
* @param preciseShardingValue database-strategy.standard.sharding-column
* */
@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
log.info("[Database PreciseShardingAlgorithm] collection:{} ,preciseShardingValue: {}",
JSON.toJSONString(collection),JSON.toJSONString(preciseShardingValue));
for (String name : collection) {
// 分库的规则
if (name.endsWith(preciseShardingValue.getValue() % collection.size() + "")) {
log.info("[Database PreciseShardingAlgorithm ] return database name:{}",name);
return name;
}
}
throw new UnsupportedOperationException();
}
}
/**
* sharding jdbc 精准`分表`策略
* */
@Slf4j
public class PreciseShardingTableAlgorithm implements PreciseShardingAlgorithm<Long> {
/**
* @param collection 拆分的所有表 比如 [t_order0000,t_order0001,t_order0002]
* @param preciseShardingValue table-strategy.standard.sharding-column
* */
@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
log.info("[Table PreciseShardingAlgorithm ] collection:{} ,preciseShardingValue: {}",
JSON.toJSONString(collection),JSON.toJSONString(preciseShardingValue));
for (String name : collection) {
// 分表的规则
if (name.endsWith(preciseShardingValue.getValue() % collection.size() + "")) {
log.info("[Table PreciseShardingAlgorithm ] return table name:{}",name);
return name;
}
}
throw new UnsupportedOperationException();
}
}
Configuration file
###########################Sharding Jdbc配置###########################
sharding.jdbc.datasource.names=m,ds0,ds1
sharding.jdbc.datasource.m.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.m.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.m.url=jdbc:mysql://192.168.79.131:3306/grp_leader
sharding.jdbc.datasource.m.username=root
sharding.jdbc.datasource.m.password=123456
sharding.jdbc.datasource.m.initialSize=${initialSize}
sharding.jdbc.datasource.m.minIdle=${minIdle}
sharding.jdbc.datasource.m.maxActive=${maxActive}
sharding.jdbc.datasource.m.maxWait=${maxWait}
sharding.jdbc.datasource.m.validationQuery=SELECT 1 FROM DUAL
sharding.jdbc.datasource.m.timeBetweenEvictionRunsMillis=${timeBetweenEvictionRunsMillis}
sharding.jdbc.datasource.m.minEvictableIdleTimeMillis=${minEvictableIdleTimeMillis}
sharding.jdbc.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds0.url=jdbc:mysql://192.168.79.131:3306/grp_manage_01
sharding.jdbc.datasource.ds0.username=root
sharding.jdbc.datasource.ds0.password=123456
sharding.jdbc.datasource.ds0.initialSize=${initialSize}
sharding.jdbc.datasource.ds0.minIdle=${minIdle}
sharding.jdbc.datasource.ds0.maxActive=${maxActive}
sharding.jdbc.datasource.ds0.maxWait=${maxWait}
sharding.jdbc.datasource.ds0.validationQuery=SELECT 1 FROM DUAL
sharding.jdbc.datasource.ds0.timeBetweenEvictionRunsMillis=${timeBetweenEvictionRunsMillis}
sharding.jdbc.datasource.ds0.minEvictableIdleTimeMillis=${minEvictableIdleTimeMillis}
sharding.jdbc.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds1.url=jdbc:mysql://192.168.79.131:3306/grp_manage_02
sharding.jdbc.datasource.ds1.username=root
sharding.jdbc.datasource.ds1.password=123456
sharding.jdbc.datasource.ds1.initialSize=${initialSize}
sharding.jdbc.datasource.ds1.minIdle=${minIdle}
sharding.jdbc.datasource.ds1.maxActive=${maxActive}
sharding.jdbc.datasource.ds1.maxWait=${maxWait}
sharding.jdbc.datasource.ds1.validationQuery=SELECT 1 FROM DUAL
sharding.jdbc.datasource.ds1.timeBetweenEvictionRunsMillis=${timeBetweenEvictionRunsMillis}
sharding.jdbc.datasource.ds1.minEvictableIdleTimeMillis=${minEvictableIdleTimeMillis}
#分库配置
sharding.jdbc.config.sharding.default-database-strategy.standard.sharding-column=order_id
sharding.jdbc.config.sharding.default-database-strategy.standard.precise-algorithm-class-name=cn.hujinbo.sharding.config.database.PreciseShardingDatabaseAlgorithm
sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_000$->{0..2}
sharding.jdbc.config.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order.table-strategy.standard.precise-algorithm-class-name=cn.hujinbo.sharding.config.database.PreciseShardingTableAlgorithm
sharding.jdbc.config.sharding.tables.leader.actual-data-nodes=m.leader
sharding.jdbc.config.sharding.tables.leader.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.leader.table-strategy.inline.algorithm-expression=leader
sharding.jdbc.config.sharding.props.sql.show=true