配置手册

数据源与分片配置示例

Sharding-Proxy支持多逻辑数据源,每个以config-前缀命名的yaml配置文件,即为一个逻辑数据源。以下是config-xxx.yaml的配置配置示例。

数据分片

dataSources:

  1. schemaName: sharding_db
  2. dataSources:
  3. ds0:
  4. url: jdbc:mysql://localhost:3306/ds0
  5. username: root
  6. password:
  7. autoCommit: true
  8. connectionTimeout: 30000
  9. idleTimeout: 60000
  10. maxLifetime: 1800000
  11. maximumPoolSize: 65
  12. ds1:
  13. url: jdbc:mysql://localhost:3306/ds1
  14. username: root
  15. password:
  16. autoCommit: true
  17. connectionTimeout: 30000
  18. idleTimeout: 60000
  19. maxLifetime: 1800000
  20. maximumPoolSize: 65
  21. shardingRule:
  22. tables:
  23. t_order:
  24. actualDataNodes: ds${0..1}.t_order${0..1}
  25. databaseStrategy:
  26. inline:
  27. shardingColumn: user_id
  28. algorithmExpression: ds${user_id % 2}
  29. tableStrategy:
  30. inline:
  31. shardingColumn: order_id
  32. algorithmExpression: t_order${order_id % 2}
  33. keyGeneratorColumnName: order_id
  34. t_order_item:
  35. actualDataNodes: ds${0..1}.t_order_item${0..1}
  36. databaseStrategy:
  37. inline:
  38. shardingColumn: user_id
  39. algorithmExpression: ds${user_id % 2}
  40. tableStrategy:
  41. inline:
  42. shardingColumn: order_id
  43. algorithmExpression: t_order_item${order_id % 2}
  44. bindingTables:
  45. - t_order,t_order_item
  46. defaultTableStrategy:
  47. none:
  48. defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator

读写分离

  1. schemaName: master_slave_db
  2. dataSources:
  3. ds_master:
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. autoCommit: true
  8. connectionTimeout: 30000
  9. idleTimeout: 60000
  10. maxLifetime: 1800000
  11. maximumPoolSize: 65
  12. ds_slave0:
  13. url: jdbc:mysql://localhost:3306/ds_slave0
  14. username: root
  15. password:
  16. autoCommit: true
  17. connectionTimeout: 30000
  18. idleTimeout: 60000
  19. maxLifetime: 1800000
  20. maximumPoolSize: 65
  21. ds_slave1:
  22. url: jdbc:mysql://localhost:3306/ds_slave1
  23. username: root
  24. password:
  25. autoCommit: true
  26. connectionTimeout: 30000
  27. idleTimeout: 60000
  28. maxLifetime: 1800000
  29. maximumPoolSize: 65
  30. masterSlaveRule:
  31. name: ds_ms
  32. masterDataSourceName: ds_master
  33. slaveDataSourceNames:
  34. - ds_slave0
  35. - ds_slave1

数据分片 + 读写分离

  1. schemaName: sharding_master_slave_db
  2. dataSources:
  3. ds0:
  4. url: jdbc:mysql://localhost:3306/ds0
  5. username: root
  6. password:
  7. autoCommit: true
  8. connectionTimeout: 30000
  9. idleTimeout: 60000
  10. maxLifetime: 1800000
  11. maximumPoolSize: 65
  12. ds0_slave0:
  13. url: jdbc:mysql://localhost:3306/ds0_slave0
  14. username: root
  15. password:
  16. autoCommit: true
  17. connectionTimeout: 30000
  18. idleTimeout: 60000
  19. maxLifetime: 1800000
  20. maximumPoolSize: 65
  21. ds0_slave1:
  22. url: jdbc:mysql://localhost:3306/ds0_slave1
  23. username: root
  24. password:
  25. autoCommit: true
  26. connectionTimeout: 30000
  27. idleTimeout: 60000
  28. maxLifetime: 1800000
  29. maximumPoolSize: 65
  30. ds1:
  31. url: jdbc:mysql://localhost:3306/ds1
  32. username: root
  33. password:
  34. autoCommit: true
  35. connectionTimeout: 30000
  36. idleTimeout: 60000
  37. maxLifetime: 1800000
  38. maximumPoolSize: 65
  39. ds1_slave0:
  40. url: jdbc:mysql://localhost:3306/ds1_slave0
  41. username: root
  42. password:
  43. autoCommit: true
  44. connectionTimeout: 30000
  45. idleTimeout: 60000
  46. maxLifetime: 1800000
  47. maximumPoolSize: 65
  48. ds1_slave1:
  49. url: jdbc:mysql://localhost:3306/ds1_slave1
  50. username: root
  51. password:
  52. autoCommit: true
  53. connectionTimeout: 30000
  54. idleTimeout: 60000
  55. maxLifetime: 1800000
  56. maximumPoolSize: 65
  57. shardingRule:
  58. tables:
  59. t_order:
  60. actualDataNodes: ms_ds${0..1}.t_order${0..1}
  61. databaseStrategy:
  62. inline:
  63. shardingColumn: user_id
  64. algorithmExpression: ms_ds${user_id % 2}
  65. tableStrategy:
  66. inline:
  67. shardingColumn: order_id
  68. algorithmExpression: t_order${order_id % 2}
  69. keyGeneratorColumnName: order_id
  70. t_order_item:
  71. actualDataNodes: ms_ds${0..1}.t_order_item${0..1}
  72. databaseStrategy:
  73. inline:
  74. shardingColumn: user_id
  75. algorithmExpression: ms_ds${user_id % 2}
  76. tableStrategy:
  77. inline:
  78. shardingColumn: order_id
  79. algorithmExpression: t_order_item${order_id % 2}
  80. bindingTables:
  81. - t_order,t_order_item
  82. broadcastTables:
  83. - t_config
  84. defaultDataSourceName: ds0
  85. defaultTableStrategy:
  86. none:
  87. defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator
  88. masterSlaveRules:
  89. ms_ds0:
  90. masterDataSourceName: ds0
  91. slaveDataSourceNames:
  92. - ds0_slave0
  93. - ds0_slave1
  94. loadBalanceAlgorithmType: ROUND_ROBIN
  95. configMap:
  96. master-slave-key0: master-slave-value0
  97. ms_ds1:
  98. masterDataSourceName: ds1
  99. slaveDataSourceNames:
  100. - ds1_slave0
  101. - ds1_slave1
  102. loadBalanceAlgorithmType: ROUND_ROBIN
  103. configMap:
  104. master-slave-key1: master-slave-value1

全局配置示例

Sharding-Proxy使用conf/server.yaml配置注册中心、认证信息以及公用属性。

数据治理

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

认证信息

  1. authentication:
  2. username: root
  3. password:

公用属性

  1. props:
  2. executor.size: 16
  3. sql.show: false

数据源与分片配置项说明

数据分片

  1. schemaName: #逻辑数据源名称
  2. dataSources: #数据源配置,可配置多个data_source_name
  3. <data_source_name>: #与Sharding-JDBC配置不同,无需配置数据库连接池
  4. url: #数据库url连接
  5. username: #数据库用户名
  6. password: #数据库密码
  7. autoCommit: true #hikari连接池默认配置
  8. connectionTimeout: 30000 #hikari连接池默认配置
  9. idleTimeout: 60000 #hikari连接池默认配置
  10. maxLifetime: 1800000 #hikari连接池默认配置
  11. maximumPoolSize: 65 #hikari连接池默认配置
  12. shardingRule: #省略数据分片配置,与Sharding-JDBC配置一致

读写分离

  1. schemaName: #逻辑数据源名称
  2. dataSources: #省略数据源配置,与数据分片一致
  3. masterSlaveRule: #省略读写分离配置,与Sharding-JDBC配置一致

全局配置项说明

数据治理

与Sharding-JDBC配置一致。

Proxy属性

  1. #省略与Sharding-JDBC一致的配置属性
  2. props:
  3. acceptor.size: #用于设置接收客户端请求的工作线程个数,默认为CPU核数*2
  4. proxy.transaction.enabled: #是否开启事务, 目前仅支持XA事务,默认为不开启
  5. proxy.opentracing.enabled: #是否开启链路追踪功能,默认为不开启。详情请参见[链路追踪](/cn/features/orchestration/apm/)
  6. check.table.metadata.enabled: #是否在启动时检查分表元数据一致性,默认值: false

权限验证

用于执行登录Sharding Proxy的权限验证。配置用户名、密码后,必须使用正确的用户名、密码才可登录Proxy。

  1. authentication:
  2. username: root
  3. password:

Yaml语法说明

!! 表示实例化该类

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

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