1.引入依赖

  1. <dependency>
  2. <groupId>com.xy</groupId>
  3. <artifactId>xy-core-framework-dal-shardingsphere</artifactId>
  4. <version>${xy-core-framework-dal-shardingsphere.version}</version>
  5. </dependency>

2.添加配置

2.1读写分离示列

  1. spring.shardingsphere.datasource.names=master,slave
  2. # 主数据源
  3. spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.master.username=root
  7. spring.shardingsphere.datasource.master.password=123456
  8. # 从数据源
  9. spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
  10. spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
  11. spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
  12. spring.shardingsphere.datasource.slave.username=root
  13. spring.shardingsphere.datasource.slave.password=123456
  14. # 读写分离配置
  15. spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robin
  16. spring.shardingsphere.masterslave.name=dataSource
  17. spring.shardingsphere.masterslave.master-data-source-name=master
  18. spring.shardingsphere.masterslave.slave-data-source-names=slave
  19. # 显示SQL
  20. spring.shardingsphere.props.sql.show=true

2.2垂直拆分

不同的表在不同的库中
  1. spring.shardingsphere.datasource.names=ds0,ds1
  2. # 数据源
  3. spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.ds0.username=root
  7. spring.shardingsphere.datasource.ds0.password=123456
  8. spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
  9. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  10. spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
  11. spring.shardingsphere.datasource.ds1.username=root
  12. spring.shardingsphere.datasource.ds1.password=123456
  13. # 绑定loudong表所在节点
  14. spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
  15. # 绑定user表所在节点
  16. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
  17. spring.shardingsphere.sharding.tables.user.key-generator.column=id
  18. spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE

2.3JDBC 垂直拆分+ 读写分离

不同的表在不同的库中
  1. spring.shardingsphere.datasource.names=ds0,ds0slave,ds1,ds1slave
  2. # 数据源
  3. spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.ds0.username=root
  7. spring.shardingsphere.datasource.ds0.password=123456
  8. spring.shardingsphere.datasource.ds0slave.type=com.alibaba.druid.pool.DruidDataSource
  9. spring.shardingsphere.datasource.ds0slave.driver-class-name=com.mysql.jdbc.Driver
  10. spring.shardingsphere.datasource.ds0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
  11. spring.shardingsphere.datasource.ds0slave.username=root
  12. spring.shardingsphere.datasource.ds0slave.password=123456
  13. spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
  14. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  15. spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
  16. spring.shardingsphere.datasource.ds1.username=root
  17. spring.shardingsphere.datasource.ds1.password=123456
  18. spring.shardingsphere.datasource.ds1slave.type=com.alibaba.druid.pool.DruidDataSource
  19. spring.shardingsphere.datasource.ds1slave.driver-class-name=com.mysql.jdbc.Driver
  20. spring.shardingsphere.datasource.ds1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
  21. spring.shardingsphere.datasource.ds1slave.username=root
  22. spring.shardingsphere.datasource.ds1slave.password=123456
  23. # 绑定loudong表所在节点
  24. spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
  25. # 绑定user表所在节点
  26. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
  27. spring.shardingsphere.sharding.tables.user.key-generator.column=id
  28. spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
  29. # 读写分离
  30. spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0
  31. spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=ds0slave
  32. spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=ds1
  33. spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=ds1slave

2.4不分库,只分表

  1. spring.shardingsphere.datasource.names=master
  2. # 数据源
  3. spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.master.username=root
  7. spring.shardingsphere.datasource.master.password=123456
  8. # 分表配置
  9. spring.shardingsphere.sharding.tables.user.actual-data-nodes=master.user_${0..3}
  10. # 自定义分表算法
  11. #spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=id
  12. #spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm
  13. # inline 表达式
  14. spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
  15. spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}

2.5不分库,只分表+读写分离

  1. spring.shardingsphere.datasource.names=master,slave
  2. # 主数据源
  3. spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.master.username=root
  7. spring.shardingsphere.datasource.master.password=123456
  8. # 从数据源
  9. spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
  10. spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
  11. spring.shardingsphere.datasource.slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
  12. spring.shardingsphere.datasource.slave.username=root
  13. spring.shardingsphere.datasource.slave.password=123456
  14. # 分表配置
  15. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user_${0..3}
  16. spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
  17. spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}
  18. # 读写分离配置
  19. spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master
  20. spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=slave
  21. # 显示SQL
  22. spring.shardingsphere.props.sql.show=true

2.6分库分表

  1. spring.shardingsphere.datasource.names=master
  2. # 数据源
  3. spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.master.username=root
  7. spring.shardingsphere.datasource.master.password=123456
  8. # 分表配置
  9. spring.shardingsphere.sharding.tables.user.actual-data-nodes=master.user_${0..3}
  10. # 自定义分表算法
  11. #spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=id
  12. #spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm
  13. # inline 表达式
  14. spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
  15. spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_${id.longValue() % 4}

2.7分库分表+读写分离

  1. spring.shardingsphere.datasource.names=master0,master0slave,master1,master1slave
  2. # 数据源
  3. spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.master0.username=root
  7. spring.shardingsphere.datasource.master0.password=123456
  8. spring.shardingsphere.datasource.master0slave.type=com.alibaba.druid.pool.DruidDataSource
  9. spring.shardingsphere.datasource.master0slave.driver-class-name=com.mysql.jdbc.Driver
  10. spring.shardingsphere.datasource.master0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
  11. spring.shardingsphere.datasource.master0slave.username=root
  12. spring.shardingsphere.datasource.master0slave.password=123456
  13. spring.shardingsphere.datasource.master1.type=com.alibaba.druid.pool.DruidDataSource
  14. spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
  15. spring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
  16. spring.shardingsphere.datasource.master1.username=root
  17. spring.shardingsphere.datasource.master1.password=123456
  18. spring.shardingsphere.datasource.master1slave.type=com.alibaba.druid.pool.DruidDataSource
  19. spring.shardingsphere.datasource.master1slave.driver-class-name=com.mysql.jdbc.Driver
  20. spring.shardingsphere.datasource.master1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
  21. spring.shardingsphere.datasource.master1slave.username=root
  22. spring.shardingsphere.datasource.master1slave.password=123456
  23. # 分表配置
  24. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds$->{0..1}.user_$->{0..2}
  25. spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
  26. spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 3}
  27. spring.shardingsphere.sharding.tables.user.key-generator.column=id
  28. spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
  29. # 分库配置
  30. spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=id
  31. spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{id % 2}
  32. # 不分库分表的数据源指定
  33. #spring.shardingsphere.sharding.default-data-source-name=ds0
  34. # 广播表,每个节点复制一份,适用于配置类的数据
  35. #spring.shardingsphere.sharding.broadcast-tables=loudong
  36. # 读写分离
  37. spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
  38. spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave
  39. spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1
  40. spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave

2.8分库分表+读写分离案例

范围分表+取模=无限扩容
  1. spring.shardingsphere.datasource.names=master0,master0slave,master1,master1slave,masters0,master0slave,masters1,master1slave
  2. # 数据源
  3. spring.shardingsphere.datasource.master0.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.master0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
  6. spring.shardingsphere.datasource.master0.username=root
  7. spring.shardingsphere.datasource.master0.password=123456
  8. spring.shardingsphere.datasource.master0slave.type=com.alibaba.druid.pool.DruidDataSource
  9. spring.shardingsphere.datasource.master0slave.driver-class-name=com.mysql.jdbc.Driver
  10. spring.shardingsphere.datasource.master0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
  11. spring.shardingsphere.datasource.master0slave.username=root
  12. spring.shardingsphere.datasource.master0slave.password=123456
  13. spring.shardingsphere.datasource.master1.type=com.alibaba.druid.pool.DruidDataSource
  14. spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
  15. spring.shardingsphere.datasource.master1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
  16. spring.shardingsphere.datasource.master1.username=root
  17. spring.shardingsphere.datasource.master1.password=123456
  18. spring.shardingsphere.datasource.master1slave.type=com.alibaba.druid.pool.DruidDataSource
  19. spring.shardingsphere.datasource.master1slave.driver-class-name=com.mysql.jdbc.Driver
  20. spring.shardingsphere.datasource.master1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
  21. spring.shardingsphere.datasource.master1slave.username=root
  22. spring.shardingsphere.datasource.master1slave.password=123456
  23. spring.shardingsphere.datasource.masters0.type=com.alibaba.druid.pool.DruidDataSource
  24. spring.shardingsphere.datasource.masters0.driver-class-name=com.mysql.jdbc.Driver
  25. spring.shardingsphere.datasource.masters0.url=jdbc:mysql://localhost:3306/ds_2?characterEncoding=utf-8
  26. spring.shardingsphere.datasource.masters0.username=root
  27. spring.shardingsphere.datasource.masters0.password=123456
  28. spring.shardingsphere.datasource.masters1.type=com.alibaba.druid.pool.DruidDataSource
  29. spring.shardingsphere.datasource.masters1.driver-class-name=com.mysql.jdbc.Driver
  30. spring.shardingsphere.datasource.masters1.url=jdbc:mysql://localhost:3306/ds_3?characterEncoding=utf-8
  31. spring.shardingsphere.datasource.masters1.username=root
  32. spring.shardingsphere.datasource.masters1.password=123456
  33. # 分表配置
  34. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds$->{0..1}.user_$->{0..2},dss$->{0..1}.user_$->{0..2}
  35. spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
  36. spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id % 3}
  37. # 分库配置
  38. spring.shardingsphere.sharding.default-database-strategy.standard.sharding-column=id
  39. spring.shardingsphere.sharding.default-database-strategy.standard.precise-algorithm-class-name=com.cxytiandi.sharding.algorithm.MyPreciseShardingAlgorithm
  40. # 不分库分表的数据源指定
  41. spring.shardingsphere.sharding.default-data-source-name=ds0
  42. # 广播表,每个节点复制一份,适用于配置类的数据
  43. #spring.shardingsphere.sharding.broadcast-tables=loudong
  44. # 读写分离
  45. spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
  46. spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave
  47. spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1
  48. spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave
  49. spring.shardingsphere.sharding.master-slave-rules.dss0.master-data-source-name=masters0
  50. spring.shardingsphere.sharding.master-slave-rules.dss0.slave-data-source-names=master0slave
  51. spring.shardingsphere.sharding.master-slave-rules.dss1.master-data-source-name=masters1
  52. spring.shardingsphere.sharding.master-slave-rules.dss1.slave-data-source-names=master1slave

3.强制选库

TODO

4.本地事务

TODO

5.分布式事务

TODO