数据分片 + 读写分离

一. 配置模板

1. 配置(数据分片 + 读写分离)模板

  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. tableStrategy:
  62. inline:
  63. shardingColumn: order_id
  64. algorithmExpression: t_order${order_id % 2}
  65. keyGeneratorColumnName: order_id
  66. t_order_item:
  67. actualDataNodes: ms_ds${0..1}.t_order_item${0..1}
  68. tableStrategy:
  69. inline:
  70. shardingColumn: order_id
  71. algorithmExpression: t_order_item${order_id % 2}
  72. bindingTables:
  73. - t_order,t_order_item
  74. broadcastTables:
  75. - t_config
  76. defaultDataSourceName: ds0
  77. defaultDatabaseStrategy:
  78. inline:
  79. shardingColumn: user_id
  80. algorithmExpression: ms_ds${user_id % 2}
  81. defaultTableStrategy:
  82. none:
  83. defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator
  84. masterSlaveRules:
  85. ms_ds0:
  86. masterDataSourceName: ds0
  87. slaveDataSourceNames:
  88. - ds0_slave0
  89. - ds0_slave1
  90. loadBalanceAlgorithmType: ROUND_ROBIN
  91. configMap:
  92. master-slave-key0: master-slave-value0
  93. ms_ds1:
  94. masterDataSourceName: ds1
  95. slaveDataSourceNames:
  96. - ds1_slave0
  97. - ds1_slave1
  98. loadBalanceAlgorithmType: ROUND_ROBIN
  99. configMap:
  100. master-slave-key1: master-slave-value1

2. 配套 SQL