Yaml配置

配置示例

数据分片

  1. dataSources:
  2. ds0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds0
  5. username: root
  6. password:
  7. ds1: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds1
  10. username: root
  11. password:
  12. shardingRule:
  13. tables:
  14. t_order:
  15. actualDataNodes: ds${0..1}.t_order${0..1}
  16. databaseStrategy:
  17. inline:
  18. shardingColumn: user_id
  19. algorithmExpression: ds${user_id % 2}
  20. tableStrategy:
  21. inline:
  22. shardingColumn: order_id
  23. algorithmExpression: t_order${order_id % 2}
  24. keyGenerator:
  25. type: SNOWFLAKE
  26. column: order_id
  27. t_order_item:
  28. actualDataNodes: ds${0..1}.t_order_item${0..1}
  29. databaseStrategy:
  30. inline:
  31. shardingColumn: user_id
  32. algorithmExpression: ds${user_id % 2}
  33. tableStrategy:
  34. inline:
  35. shardingColumn: order_id
  36. algorithmExpression: t_order_item${order_id % 2}
  37. bindingTables:
  38. - t_order,t_order_item
  39. broadcastTables:
  40. - t_config
  41. defaultDataSourceName: ds0
  42. defaultTableStrategy:
  43. none:
  44. defaultKeyGenerator:
  45. type: SNOWFLAKE
  46. column: order_id
  47. props:
  48. sql.show: true

读写分离

  1. dataSources:
  2. ds_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_slave0
  10. username: root
  11. password:
  12. ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds_slave1
  15. username: root
  16. password:
  17. masterSlaveRule:
  18. name: ds_ms
  19. masterDataSourceName: ds_master
  20. slaveDataSourceNames:
  21. - ds_slave0
  22. - ds_slave1
  23. props:
  24. sql.show: true

数据脱敏

  1. dataSource: !!org.apache.commons.dbcp2.BasicDataSource
  2. driverClassName: com.mysql.jdbc.Driver
  3. url: jdbc:mysql://127.0.0.1:3306/encrypt?serverTimezone=UTC&useSSL=false
  4. username: root
  5. password:
  6. encryptRule:
  7. encryptors:
  8. encryptor_aes:
  9. type: aes
  10. props:
  11. aes.key.value: 123456abc
  12. encryptor_md5:
  13. type: md5
  14. tables:
  15. t_encrypt:
  16. columns:
  17. user_id:
  18. plainColumn: user_plain
  19. cipherColumn: user_cipher
  20. encryptor: encryptor_aes
  21. order_id:
  22. cipherColumn: order_cipher
  23. encryptor: encryptor_md5
  24. props:
  25. query.with.cipher.column: true #是否使用密文列查询

数据分片 + 读写分离

  1. dataSources:
  2. ds0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds0
  5. username: root
  6. password:
  7. ds0_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds0_slave0
  10. username: root
  11. password:
  12. ds0_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds0_slave1
  15. username: root
  16. password:
  17. ds1: !!org.apache.commons.dbcp.BasicDataSource
  18. driverClassName: com.mysql.jdbc.Driver
  19. url: jdbc:mysql://localhost:3306/ds1
  20. username: root
  21. password:
  22. ds1_slave0: !!org.apache.commons.dbcp.BasicDataSource
  23. driverClassName: com.mysql.jdbc.Driver
  24. url: jdbc:mysql://localhost:3306/ds1_slave0
  25. username: root
  26. password:
  27. ds1_slave1: !!org.apache.commons.dbcp.BasicDataSource
  28. driverClassName: com.mysql.jdbc.Driver
  29. url: jdbc:mysql://localhost:3306/ds1_slave1
  30. username: root
  31. password:
  32. shardingRule:
  33. tables:
  34. t_order:
  35. actualDataNodes: ms_ds${0..1}.t_order${0..1}
  36. databaseStrategy:
  37. inline:
  38. shardingColumn: user_id
  39. algorithmExpression: ms_ds${user_id % 2}
  40. tableStrategy:
  41. inline:
  42. shardingColumn: order_id
  43. algorithmExpression: t_order${order_id % 2}
  44. keyGenerator:
  45. type: SNOWFLAKE
  46. column: order_id
  47. t_order_item:
  48. actualDataNodes: ms_ds${0..1}.t_order_item${0..1}
  49. databaseStrategy:
  50. inline:
  51. shardingColumn: user_id
  52. algorithmExpression: ms_ds${user_id % 2}
  53. tableStrategy:
  54. inline:
  55. shardingColumn: order_id
  56. algorithmExpression: t_order_item${order_id % 2}
  57. bindingTables:
  58. - t_order,t_order_item
  59. broadcastTables:
  60. - t_config
  61. defaultDataSourceName: ds_0
  62. defaultTableStrategy:
  63. none:
  64. defaultKeyGenerator:
  65. type: SNOWFLAKE
  66. column: order_id
  67. masterSlaveRules:
  68. ms_ds0:
  69. masterDataSourceName: ds0
  70. slaveDataSourceNames:
  71. - ds0_slave0
  72. - ds0_slave1
  73. loadBalanceAlgorithmType: ROUND_ROBIN
  74. ms_ds1:
  75. masterDataSourceName: ds1
  76. slaveDataSourceNames:
  77. - ds1_slave0
  78. - ds1_slave1
  79. loadBalanceAlgorithmType: ROUND_ROBIN
  80. props:
  81. sql.show: true

数据分片 + 数据脱敏

  1. dataSources:
  2. ds_0: !!com.zaxxer.hikari.HikariDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0
  5. username: root
  6. password:
  7. ds_1: !!com.zaxxer.hikari.HikariDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1
  10. username: root
  11. password:
  12. shardingRule:
  13. tables:
  14. t_order:
  15. actualDataNodes: ds_${0..1}.t_order_${0..1}
  16. databaseStrategy:
  17. inline:
  18. shardingColumn: user_id
  19. algorithmExpression: ds_${user_id % 2}
  20. tableStrategy:
  21. inline:
  22. shardingColumn: order_id
  23. algorithmExpression: t_order_${order_id % 2}
  24. keyGenerator:
  25. type: SNOWFLAKE
  26. column: order_id
  27. t_order_item:
  28. actualDataNodes: ds_${0..1}.t_order_item_${0..1}
  29. databaseStrategy:
  30. inline:
  31. shardingColumn: user_id
  32. algorithmExpression: ds_${user_id % 2}
  33. tableStrategy:
  34. inline:
  35. shardingColumn: order_id
  36. algorithmExpression: t_order_item_${order_id % 2}
  37. bindingTables:
  38. - t_order,t_order_item
  39. defaultTableStrategy:
  40. none:
  41. encryptRule:
  42. encryptors:
  43. encryptor_aes:
  44. type: aes
  45. props:
  46. aes.key.value: 123456abc
  47. tables:
  48. t_order:
  49. columns:
  50. order_id:
  51. plainColumn: order_plain
  52. cipherColumn: order_cipher
  53. encryptor: encryptor_aes
  54. props:
  55. sql.show: true

治理

  1. #省略数据分片、读写分离和数据脱敏配置
  2. orchestration:
  3. name: orchestration_ds
  4. overwrite: true
  5. registry:
  6. type: zookeeper
  7. namespace: orchestration
  8. serverLists: localhost:2181

配置项说明

数据分片

  1. dataSources:
  2. ds0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds0
  5. username: root
  6. password:
  7. ds1: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds1
  10. username: root
  11. password:
  12. shardingRule:
  13. tables:
  14. t_order:
  15. actualDataNodes: ds${0..1}.t_order${0..1}
  16. databaseStrategy:
  17. inline:
  18. shardingColumn: user_id
  19. algorithmExpression: ds${user_id % 2}
  20. tableStrategy:
  21. inline:
  22. shardingColumn: order_id
  23. algorithmExpression: t_order${order_id % 2}
  24. keyGenerator:
  25. type: SNOWFLAKE
  26. column: order_id
  27. t_order_item:
  28. actualDataNodes: ds${0..1}.t_order_item${0..1}
  29. databaseStrategy:
  30. inline:
  31. shardingColumn: user_id
  32. algorithmExpression: ds${user_id % 2}
  33. tableStrategy:
  34. inline:
  35. shardingColumn: order_id
  36. algorithmExpression: t_order_item${order_id % 2}
  37. bindingTables:
  38. - t_order,t_order_item
  39. broadcastTables:
  40. - t_config
  41. defaultDataSourceName: ds0
  42. defaultTableStrategy:
  43. none:
  44. defaultKeyGenerator:
  45. type: SNOWFLAKE
  46. column: order_id
  47. props:
  48. sql.show: true

读写分离

  1. dataSources:
  2. ds_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_slave0
  10. username: root
  11. password:
  12. ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds_slave1
  15. username: root
  16. password:
  17. masterSlaveRule:
  18. name: ds_ms
  19. masterDataSourceName: ds_master
  20. slaveDataSourceNames:
  21. - ds_slave0
  22. - ds_slave1
  23. props:
  24. sql.show: true

数据分片

  1. dataSources: #数据源配置,可配置多个data_source_name
  2. <data_source_name>: #<!!数据库连接池实现类> `!!`表示实例化该类
  3. driverClassName: #数据库驱动类名
  4. url: #数据库url连接
  5. username: #数据库用户名
  6. password: #数据库密码
  7. # ... 数据库连接池的其它属性
  8. shardingRule:
  9. tables: #数据分片规则配置,可配置多个logic_table_name
  10. <logic_table_name>: #逻辑表名称
  11. actualDataNodes: #由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点。用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
  12. databaseStrategy: #分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
  13. standard: #用于单分片键的标准分片场景
  14. shardingColumn: #分片列名称
  15. preciseAlgorithmClassName: #精确分片算法类名称,用于=和IN。。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
  16. rangeAlgorithmClassName: #范围分片算法类名称,用于BETWEEN,可选。。该类需实现RangeShardingAlgorithm接口并提供无参数的构造器
  17. complex: #用于多分片键的复合分片场景
  18. shardingColumns: #分片列名称,多个列以逗号分隔
  19. algorithmClassName: #复合分片算法类名称。该类需实现ComplexKeysShardingAlgorithm接口并提供无参数的构造器
  20. inline: #行表达式分片策略
  21. shardingColumn: #分片列名称
  22. algorithmInlineExpression: #分片算法行表达式,需符合groovy语法
  23. hint: #Hint分片策略
  24. algorithmClassName: #Hint分片算法类名称。该类需实现HintShardingAlgorithm接口并提供无参数的构造器
  25. none: #不分片
  26. tableStrategy: #分表策略,同分库策略
  27. keyGenerator:
  28. column: #自增列名称,缺省表示不使用自增主键生成器
  29. type: #自增列值生成器类型,缺省表示使用默认自增列值生成器。可使用用户自定义的列值生成器或选择内置类型:SNOWFLAKE/UUID/LEAF_SEGMENT
  30. props: #属性配置, 注意:使用SNOWFLAKE算法,需要配置worker.id与max.tolerate.time.difference.milliseconds属性。若使用此算法生成值作分片值,建议配置max.vibration.offset属性
  31. <property-name>: 属性名称
  32. bindingTables: #绑定表规则列表
  33. - <logic_table_name1, logic_table_name2, ...>
  34. - <logic_table_name3, logic_table_name4, ...>
  35. - <logic_table_name_x, logic_table_name_y, ...>
  36. broadcastTables: #广播表规则列表
  37. - table_name1
  38. - table_name2
  39. - table_name_x
  40. defaultDataSourceName: #未配置分片规则的表将通过默认数据源定位
  41. defaultDatabaseStrategy: #默认数据库分片策略,同分库策略
  42. defaultTableStrategy: #默认表分片策略,同分库策略
  43. defaultKeyGenerator: #默认的主键生成算法 如果没有设置,默认为SNOWFLAKE算法
  44. type: #默认自增列值生成器类型,缺省将使用org.apache.shardingsphere.core.keygen.generator.impl.SnowflakeKeyGenerator。可使用用户自定义的列值生成器或选择内置类型:SNOWFLAKE/UUID/LEAF_SEGMENT
  45. props:
  46. <property-name>: #自增列值生成器属性配置, 比如SNOWFLAKE算法的worker.id与max.tolerate.time.difference.milliseconds
  47. masterSlaveRules: #读写分离规则,详见读写分离部分
  48. <data_source_name>: #数据源名称,需要与真实数据源匹配,可配置多个data_source_name
  49. masterDataSourceName: #详见读写分离部分
  50. slaveDataSourceNames: #详见读写分离部分
  51. loadBalanceAlgorithmType: #详见读写分离部分
  52. props: #读写分离负载算法的属性配置
  53. <property-name>: #属性值
  54. props: #属性配置
  55. sql.show: #是否开启SQL显示,默认值: false
  56. executor.size: #工作线程数量,默认值: CPU核数
  57. max.connections.size.per.query: # 每个查询可以打开的最大连接数量,默认为1
  58. check.table.metadata.enabled: #是否在启动时检查分表元数据一致性,默认值: false

读写分离

  1. dataSources: #省略数据源配置,与数据分片一致
  2. masterSlaveRule:
  3. name: #读写分离数据源名称
  4. masterDataSourceName: #主库数据源名称
  5. slaveDataSourceNames: #从库数据源名称列表
  6. - <data_source_name1>
  7. - <data_source_name2>
  8. - <data_source_name_x>
  9. loadBalanceAlgorithmType: #从库负载均衡算法类型,可选值:ROUND_ROBIN,RANDOM。若`loadBalanceAlgorithmClassName`存在则忽略该配置
  10. props: #读写分离负载算法的属性配置
  11. <property-name>: #属性值

数据脱敏

  1. dataSource: #省略数据源配置
  2. encryptRule:
  3. encryptors:
  4. <encryptor-name>:
  5. type: #加解密器类型,可自定义或选择内置类型:MD5/AES
  6. props: #属性配置, 注意:使用AES加密器,需要配置AES加密器的KEY属性:aes.key.value
  7. aes.key.value:
  8. tables:
  9. <table-name>:
  10. columns:
  11. <logic-column-name>:
  12. plainColumn: #存储明文的字段
  13. cipherColumn: #存储密文的字段
  14. assistedQueryColumn: #辅助查询字段,针对ShardingQueryAssistedEncryptor类型的加解密器进行辅助查询
  15. encryptor: #加密器名字

治理

  1. dataSources: #省略数据源配置
  2. shardingRule: #省略分片规则配置
  3. masterSlaveRule: #省略读写分离规则配置
  4. encryptRule: #省略数据脱敏规则配置
  5. orchestration:
  6. name: #治理实例名称
  7. overwrite: #本地配置是否覆盖注册中心配置。如果可覆盖,每次启动都以本地配置为准
  8. registry: #注册中心配置
  9. type: #配置中心类型。如:zookeeper
  10. serverLists: #连接注册中心服务器的列表。包括IP地址和端口号。多个地址用逗号分隔。如: host1:2181,host2:2181
  11. namespace: #注册中心的命名空间
  12. digest: #连接注册中心的权限令牌。缺省为不需要权限验证
  13. operationTimeoutMilliseconds: #操作超时的毫秒数,默认500毫秒
  14. maxRetries: #连接失败后的最大重试次数,默认3次
  15. retryIntervalMilliseconds: #重试间隔毫秒数,默认500毫秒
  16. timeToLiveSeconds: #临时节点存活秒数,默认60秒

Yaml语法说明

!! 表示实例化该类

- 表示可以包含一个或多个

[] 表示数组,可以与减号相互替换使用