Sharding

Background

SpringBoot Starter’s data sharding configuration applies to business scenarios that use SpringBoot, which can maximize SpringBoot’s capabilities, such as configuration initialization and Bean management. It can complete the creation of the ShardingSphereDataSource object and reduce unnecessary coding.

Parameters

  1. spring.shardingsphere.datasource.names= # Omit the data source configuration, please refer to the usage
  2. # Standard sharding table configuration
  3. spring.shardingsphere.rules.sharding.tables.<table-name>.actual-data-nodes= # Describe data source names and actual tables, delimiter as point, multiple data nodes separated with comma, support inline expression. Absent means sharding databases only.
  4. # Databases sharding strategy, use default databases sharding strategy if absent. sharding strategy below can choose only one.
  5. # For single sharding column scenario
  6. spring.shardingsphere.rules.sharding.tables.<table-name>.database-strategy.standard.sharding-column= # Sharding column name
  7. spring.shardingsphere.rules.sharding.tables.<table-name>.database-strategy.standard.sharding-algorithm-name= # Sharding algorithm name
  8. # For multiple sharding columns scenario
  9. spring.shardingsphere.rules.sharding.tables.<table-name>.database-strategy.complex.sharding-columns= # Sharding column names, multiple columns separated with comma
  10. spring.shardingsphere.rules.sharding.tables.<table-name>.database-strategy.complex.sharding-algorithm-name= # Sharding algorithm name
  11. # Sharding by hint
  12. spring.shardingsphere.rules.sharding.tables.<table-name>.database-strategy.hint.sharding-algorithm-name= # Sharding algorithm name
  13. # Tables sharding strategy, same as database sharding strategy
  14. spring.shardingsphere.rules.sharding.tables.<table-name>.table-strategy.xxx= # Omitted
  15. # Auto sharding table configuraiton
  16. spring.shardingsphere.rules.sharding.auto-tables.<auto-table-name>.actual-data-sources= # data source names
  17. spring.shardingsphere.rules.sharding.auto-tables.<auto-table-name>.sharding-strategy.standard.sharding-column= # Sharding column name
  18. spring.shardingsphere.rules.sharding.auto-tables.<auto-table-name>.sharding-strategy.standard.sharding-algorithm-name= # Auto sharding algorithm name
  19. # Key generator strategy configuration
  20. spring.shardingsphere.rules.sharding.tables.<table-name>.key-generate-strategy.column= # Column name of key generator
  21. spring.shardingsphere.rules.sharding.tables.<table-name>.key-generate-strategy.key-generator-name= # Key generator name
  22. # Sharding auditor strategy configuration
  23. spring.shardingsphere.rules.sharding.tables.<table-name>.audit-strategy.auditor-names= # Sharding auditor name
  24. spring.shardingsphere.rules.sharding.tables.<table-name>.audit-strategy.allow-hint-disable= # Enable or disable sharding audit hint
  25. spring.shardingsphere.rules.sharding.binding-tables[0]= # Binding table name
  26. spring.shardingsphere.rules.sharding.binding-tables[1]= # Binding table name
  27. spring.shardingsphere.rules.sharding.binding-tables[x]= # Binding table name
  28. spring.shardingsphere.rules.sharding.broadcast-tables[0]= # Broadcast tables
  29. spring.shardingsphere.rules.sharding.broadcast-tables[1]= # Broadcast tables
  30. spring.shardingsphere.rules.sharding.broadcast-tables[x]= # Broadcast tables
  31. spring.shardingsphere.rules.sharding.default-database-strategy.xxx= # Default strategy for database sharding
  32. spring.shardingsphere.rules.sharding.default-table-strategy.xxx= # Default strategy for table sharding
  33. spring.shardingsphere.rules.sharding.default-key-generate-strategy.xxx= # Default Key generator strategy
  34. spring.shardingsphere.rules.sharding.default-sharding-column= # Default sharding column name
  35. # Sharding algorithm configuration
  36. spring.shardingsphere.rules.sharding.sharding-algorithms.<sharding-algorithm-name>.type= # Sharding algorithm type
  37. spring.shardingsphere.rules.sharding.sharding-algorithms.<sharding-algorithm-name>.props.xxx=# Sharding algorithm properties
  38. # Key generate algorithm configuration
  39. spring.shardingsphere.rules.sharding.key-generators.<key-generate-algorithm-name>.type= # Key generate algorithm type
  40. spring.shardingsphere.rules.sharding.key-generators.<key-generate-algorithm-name>.props.xxx= # Key generate algorithm properties
  41. # Sharding audit algorithm configuration
  42. spring.shardingsphere.rules.sharding.auditors.<sharding-audit-algorithm-name>.type= # Sharing audit algorithm type
  43. spring.shardingsphere.rules.sharding.auditors.<sharding-audit-algorithm-name>.props.xxx= # Sharding audit algorithm properties

Please refer to Built-in Sharding Algorithm List and Built-in Key Generate Algorithm List for more details about type of algorithm.

Attention: Inline expression identifier can use ${...} or $->{...}, but ${...} is conflict with spring placeholder of properties, so use $->{...} on spring environment is better.

Procedure

  1. Configure data sharding rules in the SpringBoot file, including data sources, sharding rules, and global attributes.
  2. Start the SpringBoot program. The configuration is automatically loaded and the ShardingSphereDataSource is initialized.

Sample

  1. spring.shardingsphere.mode.type=Standalone
  2. spring.shardingsphere.mode.repository.type=JDBC
  3. spring.shardingsphere.datasource.names=ds-0,ds-1
  4. spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  5. spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
  6. spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
  7. spring.shardingsphere.datasource.ds-0.username=root
  8. spring.shardingsphere.datasource.ds-0.password=
  9. spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  10. spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
  11. spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
  12. spring.shardingsphere.datasource.ds-1.username=root
  13. spring.shardingsphere.datasource.ds-1.password=
  14. spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=user_id
  15. spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=database-inline
  16. spring.shardingsphere.rules.sharding.binding-tables[0]=t_order,t_order_item
  17. spring.shardingsphere.rules.sharding.broadcast-tables=t_address
  18. spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds-$->{0..1}.t_order_$->{0..1}
  19. spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
  20. spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=t-order-inline
  21. spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id
  22. spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake
  23. spring.shardingsphere.rules.sharding.tables.t_order.audit-strategy.auditor-names=shardingKeyAudit
  24. spring.shardingsphere.rules.sharding.tables.t_order.audit-strategy.allow-hint-disable=true
  25. spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds-$->{0..1}.t_order_item_$->{0..1}
  26. spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column=order_id
  27. spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name=t-order-item-inline
  28. spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id
  29. spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
  30. spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
  31. spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds-$->{user_id % 2}
  32. spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.type=INLINE
  33. spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.props.algorithm-expression=t_order_$->{order_id % 2}
  34. spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.type=INLINE
  35. spring.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.props.algorithm-expression=t_order_item_$->{order_id % 2}
  36. spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
  37. spring.shardingsphere.rules.sharding.auditors.shardingKeyAudit.type=DML_SHARDING_CONDITIONS