Mixed Rules

Background

ShardingSphere provides a variety of features, such as data sharding, read/write splitting, high availability, and data encryption. These features can be used independently or in combination. Below, you will find the parameters’ explanation and configuration samples based on YAML.

Parameters

  1. rules:
  2. - !SHARDING
  3. tables:
  4. <logic_table_name>: # Logical table name:
  5. actualDataNodes: # consists of logical data source name plus table name (refer to Inline syntax rules)
  6. tableStrategy: # Table shards strategy. The same as database shards strategy
  7. standard:
  8. shardingColumn: # Sharding column name
  9. shardingAlgorithmName: # Sharding algorithm name
  10. keyGenerateStrategy:
  11. column: # Auto-increment column name. By default, the auto-increment primary key generator is not used.
  12. keyGeneratorName: # Distributed sequence algorithm name
  13. defaultDatabaseStrategy:
  14. standard:
  15. shardingColumn: # Sharding column name
  16. shardingAlgorithmName: # Sharding algorithm name
  17. shardingAlgorithms:
  18. <sharding_algorithm_name>: # Sharding algorithm name
  19. type: INLINE
  20. props:
  21. algorithm-expression: # INLINE expression
  22. t_order_inline:
  23. type: INLINE
  24. props:
  25. algorithm-expression: # INLINE expression
  26. keyGenerators:
  27. <key_generate_algorithm_name> (+): # Distributed sequence algorithm name
  28. type: # Distributed sequence algorithm type
  29. props: # Property configuration of distributed sequence algorithm
  30. - !READWRITE_SPLITTING
  31. dataSources:
  32. <data_source_name>: # Read/write splitting logical data source name
  33. dynamicStrategy: # Read/write splitting type
  34. autoAwareDataSourceName: # Database discovery logical data source name
  35. <data_source_name>: # Read/write splitting logical data source name
  36. dynamicStrategy: # Read/write splitting type
  37. autoAwareDataSourceName: # Database discovery logical data source name
  38. - !DB_DISCOVERY
  39. dataSources:
  40. <data_source_name>:
  41. dataSourceNames: # Data source name list
  42. - ds_0
  43. - ds_1
  44. - ds_2
  45. discoveryHeartbeatName: # Detect heartbeat name
  46. discoveryTypeName: # Database discovery type name
  47. <data_source_name>:
  48. dataSourceNames: # Data source name list
  49. - ds_3
  50. - ds_4
  51. - ds_5
  52. discoveryHeartbeatName: # Detect heartbeat name
  53. discoveryTypeName: # Database discovery type name
  54. discoveryHeartbeats:
  55. <discovery_heartbeat_name>: # Heartbeat name
  56. props:
  57. keep-alive-cron: # cron expression, such as '0/5 * * * * ?'
  58. discoveryTypes:
  59. <discovery_type_name>: # Database discovery type name
  60. type: # Database discovery type, such as MySQL.MGR.
  61. props:
  62. group-name: # Required parameter of database discovery type, such as MGR's group-name.
  63. - !ENCRYPT
  64. encryptors:
  65. <encrypt_algorithm_name> (+): # Encryption and decryption algorithm name
  66. type: # Encryption and decryption algorithm type
  67. props: # Encryption and decryption algorithm property configuration
  68. <encrypt_algorithm_name> (+): # Encryption and decryption algorithm name
  69. type: # Encryption and decryption algorithm type
  70. tables:
  71. <table_name>: # Encryption table name
  72. columns:
  73. <column_name> (+): # Encrypt logic column name
  74. plainColumn (?): # Plain column name
  75. cipherColumn: # Cipher column name
  76. encryptorName: # Cipher encrypt algorithm name
  77. assistedQueryColumn (?): # Assisted query column name
  78. assistedQueryEncryptorName: # Assisted query encrypt algorithm name
  79. likeQueryColumn (?): # Like query column name
  80. likeQueryEncryptorName: # Like query encrypt algorithm name
  81. queryWithCipherColumn(?): # The current table whether query with cipher column for data encrypt.

Samples

  1. rules:
  2. - !SHARDING
  3. tables:
  4. t_order:
  5. actualDataNodes: replica_ds_${0..1}.t_order_${0..1}
  6. tableStrategy:
  7. standard:
  8. shardingColumn: order_id
  9. shardingAlgorithmName: t_order_inline
  10. keyGenerateStrategy:
  11. column: order_id
  12. keyGeneratorName: snowflake
  13. defaultDatabaseStrategy:
  14. standard:
  15. shardingColumn: user_id
  16. shardingAlgorithmName: database_inline
  17. shardingAlgorithms:
  18. database_inline:
  19. type: INLINE
  20. props:
  21. algorithm-expression: replica_ds_${user_id % 2}
  22. t_order_inline:
  23. type: INLINE
  24. props:
  25. algorithm-expression: t_order_${order_id % 2}
  26. t_order_item_inline:
  27. type: INLINE
  28. props:
  29. algorithm-expression: t_order_item_${order_id % 2}
  30. keyGenerators:
  31. snowflake:
  32. type: SNOWFLAKE
  33. - !READWRITE_SPLITTING
  34. dataSources:
  35. replica_ds_0:
  36. dynamicStrategy:
  37. autoAwareDataSourceName: readwrite_ds_0
  38. replica_ds_1:
  39. dynamicStrategy:
  40. autoAwareDataSourceName: readwrite_ds_1
  41. - !DB_DISCOVERY
  42. dataSources:
  43. readwrite_ds_0:
  44. dataSourceNames:
  45. - ds_0
  46. - ds_1
  47. - ds_2
  48. discoveryHeartbeatName: mgr_heartbeat
  49. discoveryTypeName: mgr
  50. readwrite_ds_1:
  51. dataSourceNames:
  52. - ds_3
  53. - ds_4
  54. - ds_5
  55. discoveryHeartbeatName: mgr_heartbeat
  56. discoveryTypeName: mgr
  57. discoveryHeartbeats:
  58. mgr_heartbeat:
  59. props:
  60. keep-alive-cron: '0/5 * * * * ?'
  61. discoveryTypes:
  62. mgr:
  63. type: MySQL.MGR
  64. props:
  65. group-name: 558edd3c-02ec-11ea-9bb3-080027e39bd2
  66. - !ENCRYPT
  67. encryptors:
  68. aes_encryptor:
  69. type: AES
  70. props:
  71. aes-key-value: 123456abc
  72. md5_encryptor:
  73. type: MD5
  74. like_encryptor:
  75. type: CHAR_DIGEST_LIKE
  76. tables:
  77. t_encrypt:
  78. columns:
  79. user_id:
  80. plainColumn: user_plain
  81. cipherColumn: user_cipher
  82. encryptorName: aes_encryptor
  83. assistedQueryColumn: assisted_query_user
  84. assistedQueryEncryptorName: aes_encryptor
  85. likeQueryColumn: like_query_user
  86. likeQueryEncryptorName: like_encryptor
  87. order_id:
  88. cipherColumn: order_cipher
  89. encryptorName: md5_encryptor
  90. queryWithCipherColumn: true