Sharding

Background

The configuration method of data sharding Spring Namespace is applicable to traditional Spring projects. The sharding rules and attributes are configured through the namespace xml configuration file. Spring completes the creation and management of ShardingSphereDataSource objects to avoid additional coding work.

Parameters

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding-5.2.1.xsd

<sharding:rule />

NameTypeDescription
idAttributeSpring Bean Id
table-rules (?)TagSharding table rule configuration
auto-table-rules (?)TagAutomatic sharding table rule configuration
binding-table-rules (?)TagBinding table rule configuration
broadcast-table-rules (?)TagBroadcast table rule configuration
default-database-strategy-ref (?)AttributeDefault database strategy name
default-table-strategy-ref (?)AttributeDefault table strategy name
default-key-generate-strategy-ref (?)AttributeDefault key generate strategy name
default-audit-strategy-ref (?)AttributeDefault sharding audit strategy name
default-sharding-column (?)AttributeDefault sharding column name

<sharding:table-rule />

NameTypeDescription
logic-tableAttributeLogic table name
actual-data-nodesAttributeDescribe data source names and actual tables, delimiter as point, multiple data nodes separated with comma, support inline expression. Absent means sharding databases only.
actual-data-sourcesAttributeData source names for auto sharding table
database-strategy-refAttributeDatabase strategy name for standard sharding table
table-strategy-refAttributeTable strategy name for standard sharding table
sharding-strategy-refAttributesharding strategy name for auto sharding table
key-generate-strategy-refAttributeKey generate strategy name
audit-strategy-refAttributeSharding audit strategy name

<sharding:binding-table-rules />

NameTypeDescription
binding-table-rule (+)TagBinding table rule configuration

<sharding:binding-table-rule />

NameTypeDescription
logic-tablesAttributeBinding table name, multiple tables separated with comma

<sharding:broadcast-table-rules />

NameTypeDescription
broadcast-table-rule (+)TagBroadcast table rule configuration

<sharding:broadcast-table-rule />

NameTypeDescription
tableAttributeBroadcast table name

<sharding:standard-strategy />

NameTypeDescription
idAttributeStandard sharding strategy name
sharding-columnAttributeSharding column name
algorithm-refAttributeSharding algorithm name

<sharding:complex-strategy />

NameTypeDescription
idAttributeComplex sharding strategy name
sharding-columnsAttributeSharding column names, multiple columns separated with comma
algorithm-refAttributeSharding algorithm name

<sharding:hint-strategy />

NameTypeDescription
idAttributeHint sharding strategy name
algorithm-refAttributeSharding algorithm name

<sharding:none-strategy />

NameTypeDescription
idAttributeSharding strategy name

<sharding:key-generate-strategy />

NameTypeDescription
idAttributeKey generate strategy name
columnAttributeKey generate column name
algorithm-refAttributeKey generate algorithm name

<sharding:audit-strategy />

NameTypeDescription
idAttributeSharding audit strategy name
allow-hint-disableAttributeEnable or disable sharding audit hint
auditorsTagSharding audit algorithm name

<sharding:auditors />

NameTypeDescription
auditorTagSharding audit algorithm name

<sharding:auditor />

NameTypeDescription
algorithm-refAttributeSharding audit algorithm name

<sharding:sharding-algorithm />

NameTypeDescription
idAttributeSharding algorithm name
typeAttributeSharding algorithm type
props (?)TagSharding algorithm properties

<sharding:key-generate-algorithm />

NameTypeDescription
idAttributeKey generate algorithm name
typeAttributeKey generate algorithm type
props (?)TagKey generate algorithm properties

<sharding:audit-algorithm />

NameTypeDescription
idAttributeSharding audit algorithm name
typeAttributeSharding audit algorithm type
props (?)TagSharding audit algorithm properties

Please refer to Built-in Sharding Audit Algorithm List, Built-in Key Generate Algorithm List and Built-in Sharding Audit 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 Spring Namespace configuration file, including data source, sharding rules, global attributes and other configuration items.
  2. Start the Spring program, the configuration will be loaded automatically, and the ShardingSphereDataSource will be initialized.

Sample

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
  6. xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://shardingsphere.apache.org/schema/shardingsphere/datasource
  14. http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
  15. http://shardingsphere.apache.org/schema/shardingsphere/sharding
  16. http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
  17. ">
  18. <context:component-scan base-package="org.apache.shardingsphere.example.core.mybatis" />
  19. <bean id="demo_ds_0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  20. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  21. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
  22. <property name="username" value="root"/>
  23. <property name="password" value=""/>
  24. </bean>
  25. <bean id="demo_ds_1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  26. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  27. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
  28. <property name="username" value="root"/>
  29. <property name="password" value=""/>
  30. </bean>
  31. <sharding:standard-strategy id="databaseStrategy" sharding-column="user_id" algorithm-ref="inlineStrategyShardingAlgorithm" />
  32. <sharding:sharding-algorithm id="inlineStrategyShardingAlgorithm" type="INLINE">
  33. <props>
  34. <prop key="algorithm-expression">demo_ds_${user_id % 2}</prop>
  35. </props>
  36. </sharding:sharding-algorithm>
  37. <sharding:key-generate-algorithm id="snowflakeAlgorithm" type="SNOWFLAKE">
  38. </sharding:key-generate-algorithm>
  39. <sharding:audit-algorithm id="auditAlgorithm" type="DML_SHARDING_CONDITIONS" />
  40. <sharding:key-generate-strategy id="orderKeyGenerator" column="order_id" algorithm-ref="snowflakeAlgorithm" />
  41. <sharding:key-generate-strategy id="itemKeyGenerator" column="order_item_id" algorithm-ref="snowflakeAlgorithm" />
  42. <sharding:audit-strategy id="defaultAudit" allow-hint-disable="true">
  43. <sharding:auditors>
  44. <sharding:auditor algorithm-ref="auditAlgorithm" />
  45. </sharding:auditors>
  46. </sharding:audit-strategy>
  47. <sharding:audit-strategy id="shardingKeyAudit" allow-hint-disable="true">
  48. <sharding:auditors>
  49. <sharding:auditor algorithm-ref="auditAlgorithm" />
  50. </sharding:auditors>
  51. </sharding:audit-strategy>
  52. <sharding:rule id="shardingRule">
  53. <sharding:table-rules>
  54. <sharding:table-rule logic-table="t_order" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="orderKeyGenerator" audit-strategy-ref="shardingKeyAudit" />
  55. <sharding:table-rule logic-table="t_order_item" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="itemKeyGenerator" />
  56. </sharding:table-rules>
  57. <sharding:binding-table-rules>
  58. <sharding:binding-table-rule logic-tables="t_order,t_order_item"/>
  59. </sharding:binding-table-rules>
  60. <sharding:broadcast-table-rules>
  61. <sharding:broadcast-table-rule table="t_address"/>
  62. </sharding:broadcast-table-rules>
  63. </sharding:rule>
  64. <shardingsphere:data-source id="shardingDataSource" database-name="sharding-databases" data-source-names="demo_ds_0, demo_ds_1" rule-refs="shardingRule" />
  65. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  66. <property name="dataSource" ref="shardingDataSource" />
  67. </bean>
  68. <tx:annotation-driven />
  69. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  70. <property name="dataSource" ref="shardingDataSource"/>
  71. <property name="mapperLocations" value="classpath*:META-INF/mappers/*.xml"/>
  72. </bean>
  73. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  74. <property name="basePackage" value="org.apache.shardingsphere.example.core.mybatis.repository"/>
  75. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  76. </bean>
  77. </beans>