Spring Boot配置

注意事项

行表达式标识符可以使用${…}$->{…},但前者与Spring本身的属性文件占位符冲突,因此在Spring环境中使用行表达式标识符建议使用$->{…}

配置示例

数据分片

  1. sharding.jdbc.datasource.names=ds0,ds1
  2. sharding.jdbc.datasource.ds0.type=org.apache.commons.dbcp.BasicDataSource
  3. sharding.jdbc.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
  4. sharding.jdbc.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0
  5. sharding.jdbc.datasource.ds0.username=root
  6. sharding.jdbc.datasource.ds0.password=
  7. sharding.jdbc.datasource.ds1.type=org.apache.commons.dbcp.BasicDataSource
  8. sharding.jdbc.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  9. sharding.jdbc.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1
  10. sharding.jdbc.datasource.ds1.username=root
  11. sharding.jdbc.datasource.ds1.password=
  12. sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
  13. sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
  14. sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
  15. sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=order_id
  16. sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item$->{0..1}
  17. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
  18. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}
  19. sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
  20. sharding.jdbc.config.sharding.binding-tables=t_order,t_order_item
  21. sharding.jdbc.config.sharding.broadcast-tables=t_config
  22. sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
  23. sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}

读写分离

  1. sharding.jdbc.datasource.names=master,slave0,slave1
  2. sharding.jdbc.datasource.master.type=org.apache.commons.dbcp.BasicDataSource
  3. sharding.jdbc.datasource.master.driver-class-name=com.mysql.jdbc.Driver
  4. sharding.jdbc.datasource.master.url=jdbc:mysql://localhost:3306/master
  5. sharding.jdbc.datasource.master.username=root
  6. sharding.jdbc.datasource.master.password=
  7. sharding.jdbc.datasource.slave0.type=org.apache.commons.dbcp.BasicDataSource
  8. sharding.jdbc.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
  9. sharding.jdbc.datasource.slave0.url=jdbc:mysql://localhost:3306/slave0
  10. sharding.jdbc.datasource.slave0.username=root
  11. sharding.jdbc.datasource.slave0.password=
  12. sharding.jdbc.datasource.slave1.type=org.apache.commons.dbcp.BasicDataSource
  13. sharding.jdbc.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
  14. sharding.jdbc.datasource.slave1.url=jdbc:mysql://localhost:3306/slave1
  15. sharding.jdbc.datasource.slave1.username=root
  16. sharding.jdbc.datasource.slave1.password=
  17. sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
  18. sharding.jdbc.config.masterslave.name=ms
  19. sharding.jdbc.config.masterslave.master-data-source-name=master
  20. sharding.jdbc.config.masterslave.slave-data-source-names=slave0,slave1
  21. sharding.jdbc.config.props.sql.show=true

数据分片 + 读写分离

  1. sharding.jdbc.datasource.names=master0,master1,master0slave0,master0slave1,master1slave0,master1slave1
  2. sharding.jdbc.datasource.master0.type=org.apache.commons.dbcp.BasicDataSource
  3. sharding.jdbc.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
  4. sharding.jdbc.datasource.master0.url=jdbc:mysql://localhost:3306/master0
  5. sharding.jdbc.datasource.master0.username=root
  6. sharding.jdbc.datasource.master0.password=
  7. sharding.jdbc.datasource.master0slave0.type=org.apache.commons.dbcp.BasicDataSource
  8. sharding.jdbc.datasource.master0slave0.driver-class-name=com.mysql.jdbc.Driver
  9. sharding.jdbc.datasource.master0slave0.url=jdbc:mysql://localhost:3306/master0slave0
  10. sharding.jdbc.datasource.master0slave0.username=root
  11. sharding.jdbc.datasource.master0slave0.password=
  12. sharding.jdbc.datasource.master0slave1.type=org.apache.commons.dbcp.BasicDataSource
  13. sharding.jdbc.datasource.master0slave1.driver-class-name=com.mysql.jdbc.Driver
  14. sharding.jdbc.datasource.master0slave1.url=jdbc:mysql://localhost:3306/master0slave1
  15. sharding.jdbc.datasource.master0slave1.username=root
  16. sharding.jdbc.datasource.master0slave1.password=
  17. sharding.jdbc.datasource.master1.type=org.apache.commons.dbcp.BasicDataSource
  18. sharding.jdbc.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
  19. sharding.jdbc.datasource.master1.url=jdbc:mysql://localhost:3306/master1
  20. sharding.jdbc.datasource.master1.username=root
  21. sharding.jdbc.datasource.master1.password=
  22. sharding.jdbc.datasource.master1slave0.type=org.apache.commons.dbcp.BasicDataSource
  23. sharding.jdbc.datasource.master1slave0.driver-class-name=com.mysql.jdbc.Driver
  24. sharding.jdbc.datasource.master1slave0.url=jdbc:mysql://localhost:3306/master1slave0
  25. sharding.jdbc.datasource.master1slave0.username=root
  26. sharding.jdbc.datasource.master1slave0.password=
  27. sharding.jdbc.datasource.master1slave1.type=org.apache.commons.dbcp.BasicDataSource
  28. sharding.jdbc.datasource.master1slave1.driver-class-name=com.mysql.jdbc.Driver
  29. sharding.jdbc.datasource.master1slave1.url=jdbc:mysql://localhost:3306/master1slave1
  30. sharding.jdbc.datasource.master1slave1.username=root
  31. sharding.jdbc.datasource.master1slave1.password=
  32. sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
  33. sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
  34. sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
  35. sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=order_id
  36. sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item$->{0..1}
  37. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
  38. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}
  39. sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
  40. sharding.jdbc.config.sharding.binding-tables=t_order,t_order_item
  41. sharding.jdbc.config.sharding.broadcast-tables=t_config
  42. sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
  43. sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=master$->{user_id % 2}
  44. sharding.jdbc.config.sharding.master-slave-rules.ds0.master-data-source-name=master0
  45. sharding.jdbc.config.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave0, master0slave1
  46. sharding.jdbc.config.sharding.master-slave-rules.ds1.master-data-source-name=master1
  47. sharding.jdbc.config.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave0, master1slave1

数据治理

  1. sharding.jdbc.datasource.names=ds,ds0,ds1
  2. sharding.jdbc.datasource.ds.type=org.apache.commons.dbcp.BasicDataSource
  3. sharding.jdbc.datasource.ds.driver-class-name=org.h2.Driver
  4. sharding.jdbc.datasource.ds.url=jdbc:mysql://localhost:3306/ds
  5. sharding.jdbc.datasource.ds.username=root
  6. sharding.jdbc.datasource.ds.password=
  7. sharding.jdbc.datasource.ds0.type=org.apache.commons.dbcp.BasicDataSource
  8. sharding.jdbc.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
  9. sharding.jdbc.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0
  10. sharding.jdbc.datasource.ds0.username=root
  11. sharding.jdbc.datasource.ds0.password=
  12. sharding.jdbc.datasource.ds1.type=org.apache.commons.dbcp.BasicDataSource
  13. sharding.jdbc.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  14. sharding.jdbc.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1
  15. sharding.jdbc.datasource.ds1.username=root
  16. sharding.jdbc.datasource.ds1.password=
  17. sharding.jdbc.config.sharding.default-data-source-name=ds
  18. sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
  19. sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}
  20. sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
  21. sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
  22. sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
  23. sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=order_id
  24. sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item$->{0..1}
  25. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
  26. sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}
  27. sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
  28. sharding.jdbc.config.sharding.binding-tables=t_order,t_order_item
  29. sharding.jdbc.config.sharding.broadcast-tables=t_config
  30. sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
  31. sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=master$->{user_id % 2}
  32. sharding.jdbc.config.orchestration.name=spring_boot_ds_sharding
  33. sharding.jdbc.config.orchestration.overwrite=true
  34. sharding.jdbc.config.orchestration.registry.namespace=orchestration-spring-boot-sharding-test
  35. sharding.jdbc.config.orchestration.registry.server-lists=localhost:2181

配置项说明

数据分片

  1. sharding.jdbc.datasource.names= #数据源名称,多数据源以逗号分隔
  2. sharding.jdbc.datasource.<data-source-name>.type= #数据库连接池类名称
  3. sharding.jdbc.datasource.<data-source-name>.driver-class-name= #数据库驱动类名
  4. sharding.jdbc.datasource.<data-source-name>.url= #数据库url连接
  5. sharding.jdbc.datasource.<data-source-name>.username= #数据库用户名
  6. sharding.jdbc.datasource.<data-source-name>.password= #数据库密码
  7. sharding.jdbc.datasource.<data-source-name>.xxx= #数据库连接池的其它属性
  8. sharding.jdbc.config.sharding.tables.<logic-table-name>.actual-data-nodes= #由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点。用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
  9. #分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
  10. #用于单分片键的标准分片场景
  11. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.standard.sharding-column= #分片列名称
  12. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.standard.precise-algorithm-class-name= #精确分片算法类名称,用于=和IN。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
  13. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.standard.range-algorithm-class-name= #范围分片算法类名称,用于BETWEEN,可选。该类需实现RangeShardingAlgorithm接口并提供无参数的构造器
  14. #用于多分片键的复合分片场景
  15. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.complex.sharding-columns= #分片列名称,多个列以逗号分隔
  16. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.complex.algorithm-class-name= #复合分片算法类名称。该类需实现ComplexKeysShardingAlgorithm接口并提供无参数的构造器
  17. #行表达式分片策略
  18. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.inline.sharding-column= #分片列名称
  19. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.inline.algorithm-expression= #分片算法行表达式,需符合groovy语法
  20. #Hint分片策略
  21. sharding.jdbc.config.sharding.tables.<logic-table-name>.database-strategy.hint.algorithm-class-name= #Hint分片算法类名称。该类需实现HintShardingAlgorithm接口并提供无参数的构造器
  22. #分表策略,同分库策略
  23. sharding.jdbc.config.sharding.tables.<logic-table-name>.table-strategy.xxx= #省略
  24. sharding.jdbc.config.sharding.tables.<logic-table-name>.key-generator-column-name= #自增列名称,缺省表示不使用自增主键生成器
  25. sharding.jdbc.config.sharding.tables.<logic-table-name>.key-generator-class-name= #自增列值生成器类名称,缺省表示使用默认自增列值生成器。该类需提供无参数的构造器
  26. sharding.jdbc.config.sharding.tables.<logic-table-name>.logic-index= #逻辑索引名称,对于分表的Oracle/PostgreSQL数据库中DROP INDEX XXX语句,需要通过配置逻辑索引名称定位所执行SQL的真实分表
  27. sharding.jdbc.config.sharding.binding-tables[0]= #绑定表规则列表
  28. sharding.jdbc.config.sharding.binding-tables[1]= #绑定表规则列表
  29. sharding.jdbc.config.sharding.binding-tables[x]= #绑定表规则列表
  30. sharding.jdbc.config.sharding.broadcast-tables[0]= #广播表规则列表
  31. sharding.jdbc.config.sharding.broadcast-tables[1]= #广播表规则列表
  32. sharding.jdbc.config.sharding.broadcast-tables[x]= #广播表规则列表
  33. sharding.jdbc.config.sharding.default-data-source-name= #未配置分片规则的表将通过默认数据源定位
  34. sharding.jdbc.config.sharding.default-database-strategy.xxx= #默认数据库分片策略,同分库策略
  35. sharding.jdbc.config.sharding.default-table-strategy.xxx= #默认表分片策略,同分表策略
  36. sharding.jdbc.config.sharding.default-key-generator-class-name= #默认自增列值生成器类名称,缺省使用io.shardingsphere.core.keygen.DefaultKeyGenerator。该类需实现KeyGenerator接口并提供无参数的构造器
  37. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.master-data-source-name= #详见读写分离部分
  38. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[0]= #详见读写分离部分
  39. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[1]= #详见读写分离部分
  40. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[x]= #详见读写分离部分
  41. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-class-name= #详见读写分离部分
  42. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-type= #详见读写分离部分
  43. sharding.jdbc.config.config.map.key1= #详见读写分离部分
  44. sharding.jdbc.config.config.map.key2= #详见读写分离部分
  45. sharding.jdbc.config.config.map.keyx= #详见读写分离部分
  46. sharding.jdbc.config.props.sql.show= #是否开启SQL显示,默认值: false
  47. sharding.jdbc.config.props.executor.size= #工作线程数量,默认值: CPU核数
  48. sharding.jdbc.config.config.map.key1= #用户自定义配置
  49. sharding.jdbc.config.config.map.key2= #用户自定义配置
  50. sharding.jdbc.config.config.map.keyx= #用户自定义配置

读写分离

  1. #省略数据源配置,与数据分片一致
  2. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.master-data-source-name= #主库数据源名称
  3. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[0]= #从库数据源名称列表
  4. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[1]= #从库数据源名称列表
  5. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.slave-data-source-names[x]= #从库数据源名称列表
  6. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-class-name= #从库负载均衡算法类名称。该类需实现MasterSlaveLoadBalanceAlgorithm接口且提供无参数构造器
  7. sharding.jdbc.config.sharding.master-slave-rules.<master-slave-data-source-name>.load-balance-algorithm-type= #从库负载均衡算法类型,可选值:ROUND_ROBIN,RANDOM。若`load-balance-algorithm-class-name`存在则忽略该配置
  8. sharding.jdbc.config.config.map.key1= #用户自定义配置
  9. sharding.jdbc.config.config.map.key2= #用户自定义配置
  10. sharding.jdbc.config.config.map.keyx= #用户自定义配置
  11. sharding.jdbc.config.props.sql.show= #是否开启SQL显示,默认值: false
  12. sharding.jdbc.config.props.executor.size= #工作线程数量,默认值: CPU核数
  13. sharding.jdbc.config.props.check.table.metadata.enabled= #是否在启动时检查分表元数据一致性,默认值: false

数据治理

  1. #省略数据源、数据分片和读写分离配置
  2. sharding.jdbc.config.sharding.orchestration.name= #数据治理实例名称
  3. sharding.jdbc.config.sharding.orchestration.overwrite= #本地配置是否覆盖注册中心配置。如果可覆盖,每次启动都以本地配置为准
  4. sharding.jdbc.config.sharding.orchestration.registry.server-lists= #连接注册中心服务器的列表。包括IP地址和端口号。多个地址用逗号分隔。如: host1:2181,host2:2181
  5. sharding.jdbc.config.sharding.orchestration.registry.namespace= #注册中心的命名空间
  6. sharding.jdbc.config.sharding.orchestration.registry.digest= #连接注册中心的权限令牌。缺省为不需要权限验证
  7. sharding.jdbc.config.sharding.orchestration.registry.operation-timeout-milliseconds= #操作超时的毫秒数,默认500毫秒
  8. sharding.jdbc.config.sharding.orchestration.registry.max-retries= #连接失败后的最大重试次数,默认3次
  9. sharding.jdbc.config.sharding.orchestration.registry.retry-interval-milliseconds= #重试间隔毫秒数,默认500毫秒
  10. sharding.jdbc.config.sharding.orchestration.registry.time-to-live-seconds= #临时节点存活秒数,默认60秒