1.引入依赖
<dependency>
<groupId>com.xy</groupId>
<artifactId>xy-core-framework-dal-shardingsphere</artifactId>
<version>${xy-core-framework-dal-shardingsphere.version}</version>
</dependency>
2.添加配置
2.1读写分离示列
spring.shardingsphere.datasource.names=master,slave
# 主数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
# 从数据源
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=123456
# 读写分离配置
spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
spring.shardingsphere.masterslave.name=dataSource
spring.shardingsphere.masterslave.master-data-source-name=master
spring.shardingsphere.masterslave.slave-data-source-names=slave
# 显示SQL
spring.shardingsphere.props.sql.show=true
2.2垂直拆分
不同的表在不同的库中
spring.shardingsphere.datasource.names=ds0,ds1
# 数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
# 绑定loudong表所在节点
spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
# 绑定user表所在节点
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
2.3JDBC 垂直拆分+ 读写分离
不同的表在不同的库中
spring.shardingsphere.datasource.names=ds0,ds0slave,ds1,ds1slave
# 数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
spring.shardingsphere.datasource.ds0slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0slave.username=root
spring.shardingsphere.datasource.ds0slave.password=123456
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
spring.shardingsphere.datasource.ds1slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1slave.username=root
spring.shardingsphere.datasource.ds1slave.password=123456
# 绑定loudong表所在节点
spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
# 绑定user表所在节点
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
# 读写分离
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=ds0slave
spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=ds1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=ds1slave
2.4不分库,只分表
spring.shardingsphere.datasource.names=master
# 数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=master.user_${0..3}
# 自定义分表算法
#spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=id
#spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm
# inline 表达式
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}
2.5不分库,只分表+读写分离
spring.shardingsphere.datasource.names=master,slave
# 主数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
# 从数据源
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=123456
# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user_${0..3}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}
# 读写分离配置
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave
# 显示SQL
spring.shardingsphere.props.sql.show=true
2.6分库分表
spring.shardingsphere.datasource.names=master
# 数据源
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=master.user_${0..3}
# 自定义分表算法
#spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=id
#spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm
# inline 表达式
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}
2.7分库分表+读写分离
spring.shardingsphere.datasource.names=master0,master0slave,master1,master1slave
# 数据源
spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=123456
spring.shardingsphere.datasource.master0slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
spring.shardingsphere.datasource.master0slave.username=root
spring.shardingsphere.datasource.master0slave.password=123456
spring.shardingsphere.datasource.master1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.master1.username=root
spring.shardingsphere.datasource.master1.password=123456
spring.shardingsphere.datasource.master1slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master1slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
spring.shardingsphere.datasource.master1slave.username=root
spring.shardingsphere.datasource.master1slave.password=123456
# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds$->{0..1}.user_$->{0..2}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 3}
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
# 分库配置
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 不分库分表的数据源指定
#spring.shardingsphere.sharding.default-data-source-name=ds0
# 广播表,每个节点复制一份,适用于配置类的数据
#spring.shardingsphere.sharding.broadcast-tables=loudong
# 读写分离
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave
spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave
2.8分库分表+读写分离案例
范围分表+取模=无限扩容
spring.shardingsphere.datasource.names=master0,master0slave,master1,master1slave,masters0,master0slave,masters1,master1slave
# 数据源
spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.master0.username=root
spring.shardingsphere.datasource.master0.password=123456
spring.shardingsphere.datasource.master0slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master0slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
spring.shardingsphere.datasource.master0slave.username=root
spring.shardingsphere.datasource.master0slave.password=123456
spring.shardingsphere.datasource.master1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.master1.username=root
spring.shardingsphere.datasource.master1.password=123456
spring.shardingsphere.datasource.master1slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master1slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
spring.shardingsphere.datasource.master1slave.username=root
spring.shardingsphere.datasource.master1slave.password=123456
spring.shardingsphere.datasource.masters0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.masters0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.masters0.url=jdbc:mysql://localhost:3306/ds_2?characterEncoding=utf-8
spring.shardingsphere.datasource.masters0.username=root
spring.shardingsphere.datasource.masters0.password=123456
spring.shardingsphere.datasource.masters1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.masters1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.masters1.url=jdbc:mysql://localhost:3306/ds_3?characterEncoding=utf-8
spring.shardingsphere.datasource.masters1.username=root
spring.shardingsphere.datasource.masters1.password=123456
# 分表配置
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds$->{0..1}.user_$->{0..2},dss$->{0..1}.user_$->{0..2}
spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 3}
# 分库配置
spring.shardingsphere.sharding.default-database-strategy.standard.sharding-column=id
spring.shardingsphere.sharding.default-database-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm
# 不分库分表的数据源指定
spring.shardingsphere.sharding.default-data-source-name=ds0
# 广播表,每个节点复制一份,适用于配置类的数据
#spring.shardingsphere.sharding.broadcast-tables=loudong
# 读写分离
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave
spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave
spring.shardingsphere.sharding.master-slave-rules.dss0.master-data-source-name=masters0
spring.shardingsphere.sharding.master-slave-rules.dss0.slave-data-source-names=master0slave
spring.shardingsphere.sharding.master-slave-rules.dss1.master-data-source-name=masters1
spring.shardingsphere.sharding.master-slave-rules.dss1.slave-data-source-names=master1slave
3.强制选库
TODO
4.本地事务
TODO
5.分布式事务
TODO