采用properties文件进行配置水平分库,分表。
# 配置数据源
spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.datasource.ds0.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=
spring.shardingsphere.datasource.ds1.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=
# 数据节点
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
# 主键生成策略
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE
# 分库策略
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds$->{order_id % 2}
# 分表策略
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
# 输出日志
spring.shardingsphere.props.sql.show=true
对不参与分库和分表的表可以只配置其数据源
# 配置数据源 spring.shardingsphere.datasource.names=ds0,ds1 spring.shardingsphere.datasource.ds0.type=org.apache.commons.dbcp2.BasicDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password= spring.shardingsphere.datasource.ds1.type=org.apache.commons.dbcp2.BasicDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds0 spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password= # 不分库也不分表的数据节点配置 spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds0.t_order
限定了数据节点以后,就可以指定的数据节点进行操作了。
广播表配置(通常的字典表):
spring.shardingsphere.sharding.broadcast-tables=t_dict
每个库中都应该建立对应的表。
读写分离配置:
# 配置数据源 spring.shardingsphere.datasource.names=mds0,sds1 spring.shardingsphere.datasource.mds0.type=org.apache.commons.dbcp2.BasicDataSource spring.shardingsphere.datasource.mds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.mds0.url=jdbc:mysql://localhost:3306/ds0 spring.shardingsphere.datasource.mds0.username=root spring.shardingsphere.datasource.mds0.password= spring.shardingsphere.datasource.sds1.type=org.apache.commons.dbcp2.BasicDataSource spring.shardingsphere.datasource.sds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.sds1.url=jdbc:mysql://localhost:3306/ds0 spring.shardingsphere.datasource.sds1.username=root spring.shardingsphere.datasource.sds1.password= # 读写数据源配置 spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=mds0 spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-name=sds1 # 此时的ds0就有了读写的两个数据源 spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds0.t_order



