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.DruidDataSourcespring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.master.username=rootspring.shardingsphere.datasource.master.password=123456# 从数据源spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8spring.shardingsphere.datasource.slave.username=rootspring.shardingsphere.datasource.slave.password=123456# 读写分离配置spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robinspring.shardingsphere.masterslave.name=dataSourcespring.shardingsphere.masterslave.master-data-source-name=masterspring.shardingsphere.masterslave.slave-data-source-names=slave# 显示SQLspring.shardingsphere.props.sql.show=true
2.2垂直拆分
不同的表在不同的库中
spring.shardingsphere.datasource.names=ds0,ds1# 数据源spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.ds0.username=rootspring.shardingsphere.datasource.ds0.password=123456spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8spring.shardingsphere.datasource.ds1.username=rootspring.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.userspring.shardingsphere.sharding.tables.user.key-generator.column=idspring.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.DruidDataSourcespring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.ds0.username=rootspring.shardingsphere.datasource.ds0.password=123456spring.shardingsphere.datasource.ds0slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds0slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8spring.shardingsphere.datasource.ds0slave.username=rootspring.shardingsphere.datasource.ds0slave.password=123456spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8spring.shardingsphere.datasource.ds1.username=rootspring.shardingsphere.datasource.ds1.password=123456spring.shardingsphere.datasource.ds1slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds1slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8spring.shardingsphere.datasource.ds1slave.username=rootspring.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.userspring.shardingsphere.sharding.tables.user.key-generator.column=idspring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE# 读写分离spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=ds0slavespring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=ds1spring.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.DruidDataSourcespring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.master.username=rootspring.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=idspring.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.DruidDataSourcespring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.master.username=rootspring.shardingsphere.datasource.master.password=123456# 从数据源spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8spring.shardingsphere.datasource.slave.username=rootspring.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=idspring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}# 读写分离配置spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=masterspring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave# 显示SQLspring.shardingsphere.props.sql.show=true
2.6分库分表
spring.shardingsphere.datasource.names=master# 数据源spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.master.username=rootspring.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=idspring.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.DruidDataSourcespring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.master0.username=rootspring.shardingsphere.datasource.master0.password=123456spring.shardingsphere.datasource.master0slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.master0slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8spring.shardingsphere.datasource.master0slave.username=rootspring.shardingsphere.datasource.master0slave.password=123456spring.shardingsphere.datasource.master1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8spring.shardingsphere.datasource.master1.username=rootspring.shardingsphere.datasource.master1.password=123456spring.shardingsphere.datasource.master1slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.master1slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8spring.shardingsphere.datasource.master1slave.username=rootspring.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=idspring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 3}spring.shardingsphere.sharding.tables.user.key-generator.column=idspring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE# 分库配置spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=idspring.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=master0spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slavespring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1spring.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.DruidDataSourcespring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8spring.shardingsphere.datasource.master0.username=rootspring.shardingsphere.datasource.master0.password=123456spring.shardingsphere.datasource.master0slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.master0slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8spring.shardingsphere.datasource.master0slave.username=rootspring.shardingsphere.datasource.master0slave.password=123456spring.shardingsphere.datasource.master1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8spring.shardingsphere.datasource.master1.username=rootspring.shardingsphere.datasource.master1.password=123456spring.shardingsphere.datasource.master1slave.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.master1slave.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.master1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8spring.shardingsphere.datasource.master1slave.username=rootspring.shardingsphere.datasource.master1slave.password=123456spring.shardingsphere.datasource.masters0.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.masters0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.masters0.url=jdbc:mysql://localhost:3306/ds_2?characterEncoding=utf-8spring.shardingsphere.datasource.masters0.username=rootspring.shardingsphere.datasource.masters0.password=123456spring.shardingsphere.datasource.masters1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.masters1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.masters1.url=jdbc:mysql://localhost:3306/ds_3?characterEncoding=utf-8spring.shardingsphere.datasource.masters1.username=rootspring.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=idspring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 3}# 分库配置spring.shardingsphere.sharding.default-database-strategy.standard.sharding-column=idspring.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=master0spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slavespring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slavespring.shardingsphere.sharding.master-slave-rules.dss0.master-data-source-name=masters0spring.shardingsphere.sharding.master-slave-rules.dss0.slave-data-source-names=master0slavespring.shardingsphere.sharding.master-slave-rules.dss1.master-data-source-name=masters1spring.shardingsphere.sharding.master-slave-rules.dss1.slave-data-source-names=master1slave
3.强制选库
TODO
4.本地事务
TODO
5.分布式事务
TODO
