Mixed Rules

Background

ShardingSphere provides a variety of features, such as data sharding, read/write splitting, high availability, and data decryption. These features can be used independently or in combination.

Below, you will find the configuration samples based on Spring Namespace.

Samples

  1. <!-- Sharding configuration -->
  2. <sharding:standard-strategy id="databaseStrategy" sharding-column="user_id" algorithm-ref="inlineStrategyShardingAlgorithm" />
  3. <sharding:sharding-algorithm id="inlineStrategyShardingAlgorithm" type="INLINE">
  4. <props>
  5. <prop key="algorithm-expression">replica_ds_${user_id % 2}</prop>
  6. </props>
  7. </sharding:sharding-algorithm>
  8. <sharding:key-generate-algorithm id="snowflakeAlgorithm" type="SNOWFLAKE">
  9. </sharding:key-generate-algorithm>
  10. <sharding:key-generate-strategy id="orderKeyGenerator" column="order_id" algorithm-ref="snowflakeAlgorithm" />
  11. <sharding:rule id="shardingRule">
  12. <sharding:table-rules>
  13. <sharding:table-rule logic-table="t_order" database-strategy-ref="databaseStrategy" key-generate-strategy-ref="orderKeyGenerator" />
  14. </sharding:table-rules>
  15. </sharding:rule>
  16. <!-- Dynamic read/write splitting configuration -->
  17. <readwrite-splitting:rule id="readWriteSplittingRule">
  18. <readwrite-splitting:data-source-rule id="replica_ds_0">
  19. <readwrite-splitting:dynamic-strategy id="dynamicStrategy" auto-aware-data-source-name="readwrite_ds_0" />
  20. </readwrite-splitting:data-source-rule>
  21. <readwrite-splitting:data-source-rule id="replica_ds_1">
  22. <readwrite-splitting:dynamic-strategy id="dynamicStrategy" auto-aware-data-source-name="readwrite_ds_1" />
  23. </readwrite-splitting:data-source-rule>
  24. </readwrite-splitting:rule>
  25. <!-- Database discovery configuration -->
  26. <database-discovery:rule id="mgrDatabaseDiscoveryRule">
  27. <database-discovery:data-source-rule id="readwrite_ds_0" data-source-names="ds_0,ds_1,ds_2" discovery-heartbeat-name="mgr-heartbeat" discovery-type-name="mgr" />
  28. <database-discovery:data-source-rule id="readwrite_ds_1" data-source-names="ds_3,ds_4,ds_5" discovery-heartbeat-name="mgr-heartbeat" discovery-type-name="mgr" />
  29. <database-discovery:discovery-heartbeat id="mgr-heartbeat">
  30. <props>
  31. <prop key="keep-alive-cron" >0/5 * * * * ?</prop>
  32. </props>
  33. </database-discovery:discovery-heartbeat>
  34. </database-discovery:rule>
  35. <database-discovery:discovery-type id="mgr" type="MySQL.MGR">
  36. <props>
  37. <prop key="group-name">558edd3c-02ec-11ea-9bb3-080027e39bd2</prop>
  38. </props>
  39. </database-discovery:discovery-type>
  40. <!-- Data decryption configuration -->
  41. <encrypt:encrypt-algorithm id="name_encryptor" type="AES">
  42. <props>
  43. <prop key="aes-key-value">123456</prop>
  44. </props>
  45. </encrypt:encrypt-algorithm>
  46. <encrypt:encrypt-algorithm id="pwd_encryptor" type="assistedTest" />
  47. <encrypt:rule id="encryptRule">
  48. <encrypt:table name="t_user">
  49. <encrypt:column logic-column="username" cipher-column="username" plain-column="username_plain" encrypt-algorithm-ref="name_encryptor" />
  50. <encrypt:column logic-column="pwd" cipher-column="pwd" assisted-query-column="assisted_query_pwd" encrypt-algorithm-ref="pwd_encryptor" />
  51. </encrypt:table>
  52. </encrypt:rule>