YAML configuration

5.0.0-alpha

Data Sharding

Configuration Item Explanation

  1. dataSources: # Omit the data source configuration, please refer to the usage
  2. rules:
  3. - !SHARDING
  4. tables: # Sharding table configuration
  5. <logic-table-name> (+): # Logic table name
  6. actualDataNodes (?): # Describe data source names and actual tables (refer to Inline syntax rules)
  7. databaseStrategy (?): # Databases sharding strategy, use default databases sharding strategy if absent. sharding strategy below can choose only one.
  8. standard: # For single sharding column scenario
  9. shardingColumn: # Sharding column name
  10. shardingAlgorithmName: # Sharding algorithm name
  11. complex: # For multiple sharding columns scenario
  12. shardingColumns: # Sharding column names, multiple columns separated with comma
  13. shardingAlgorithmName: # Sharding algorithm name
  14. hint: # Sharding by hint
  15. shardingAlgorithmName: # Sharding algorithm name
  16. none: # Do not sharding
  17. tableStrategy: # Tables sharding strategy, same as database sharding strategy
  18. keyGenerateStrategy: # Key generator strategy
  19. column: # Column name of key generator
  20. keyGeneratorName: # Key generator name
  21. autoTables: # Auto Sharding table configuration
  22. t_order_auto: # Logic table name
  23. actualDataSources (?): # Data source names
  24. shardingStrategy: # Sharding strategy
  25. standard: # For single sharding column scenario
  26. shardingColumn: # Sharding column name
  27. shardingAlgorithmName: # Auto sharding algorithm name
  28. bindingTables (+): # Binding tables
  29. - <logic_table_name_1, logic_table_name_2, ...>
  30. - <logic_table_name_1, logic_table_name_2, ...>
  31. broadcastTables (+): # Broadcast tables
  32. - <table-name>
  33. - <table-name>
  34. defaultDatabaseStrategy: # Default strategy for database sharding
  35. defaultTableStrategy: # Default strategy for table sharding
  36. defaultKeyGenerateStrategy: # Default Key generator strategy
  37. # Sharding algorithm configuration
  38. shardingAlgorithms:
  39. <sharding-algorithm-name> (+): # Sharding algorithm name
  40. type: # Sharding algorithm type
  41. props: # Sharding algorithm properties
  42. # ...
  43. # Key generate algorithm configuration
  44. keyGenerators:
  45. <key-generate-algorithm-name> (+): # Key generate algorithm name
  46. type: # Key generate algorithm type
  47. props: # Key generate algorithm properties
  48. # ...
  49. props:
  50. # ...

Replica Query

Configuration Item Explanation

  1. dataSources: # Omit the data source configuration, please refer to the usage
  2. rules:
  3. - !REPLICA_QUERY
  4. dataSources:
  5. <data-source-name> (+): # Logic data source name of replica query
  6. primaryDataSourceName: # Primary data source name
  7. replicaDataSourceNames:
  8. - <replica-data_source-name> (+) # Replica data source name
  9. loadBalancerName: # Load balance algorithm name
  10. # Load balance algorithm configuration
  11. loadBalancers:
  12. <load-balancer-name> (+): # Load balance algorithm name
  13. type: # Load balance algorithm type
  14. props: # Load balance algorithm properties
  15. # ...
  16. props:
  17. # ...

Please refer to Built-in Load Balance Algorithm List for more details about type of algorithm.

Encryption

Configuration Item Explanation

  1. dataSource: # Omit the data source configuration, please refer to the usage
  2. rules:
  3. - !ENCRYPT
  4. tables:
  5. <table-name> (+): # Encrypt table name
  6. columns:
  7. <column-name> (+): # Encrypt logic column name
  8. cipherColumn: # Cipher column name
  9. assistedQueryColumn (?): # Assisted query column name
  10. plainColumn (?): # Plain column name
  11. encryptorName: # Encrypt algorithm name
  12. # Encrypt algorithm configuration
  13. encryptors:
  14. <encrypt-algorithm-name> (+): # Encrypt algorithm name
  15. type: # Encrypt algorithm type
  16. props: # Encrypt algorithm properties
  17. # ...
  18. queryWithCipherColumn: # Whether query with cipher column for data encrypt. User you can use plaintext to query if have

Please refer to Built-in Encrypt Algorithm List for more details about type of algorithm.

Shadow DB

Configuration Item Explanation

  1. dataSources: # Omit the data source configuration, please refer to the usage
  2. rules:
  3. - !SHADOW
  4. column: # Shadow column name
  5. sourceDataSourceNames: # Source Data Source names
  6. # ...
  7. shadowDataSourceNames: # Shadow Data Source names
  8. # ...
  9. props:
  10. # ...

Governance

Configuration Item Explanation

  1. governance:
  2. name: # Governance name
  3. registryCenter: # Registry center
  4. type: # Governance instance type. Example:Zookeeper, etcd
  5. serverLists: # The list of servers that connect to governance instance, including IP and port number; use commas to separate
  6. overwrite: # Whether to overwrite local configurations with config center configurations; if it can, each initialization should refer to local configurations

ShardingSphere-4.x

Data Sharding

Configuration Item Explanation

  1. dataSources:
  2. ds0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds0
  5. username: root
  6. password:
  7. ds1: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds1
  10. username: root
  11. password:
  12. shardingRule:
  13. tables:
  14. t_order:
  15. actualDataNodes: ds${0..1}.t_order${0..1}
  16. databaseStrategy:
  17. inline:
  18. shardingColumn: user_id
  19. algorithmExpression: ds${user_id % 2}
  20. tableStrategy:
  21. inline:
  22. shardingColumn: order_id
  23. algorithmExpression: t_order${order_id % 2}
  24. keyGenerator:
  25. type: SNOWFLAKE
  26. column: order_id
  27. t_order_item:
  28. actualDataNodes: ds${0..1}.t_order_item${0..1}
  29. databaseStrategy:
  30. inline:
  31. shardingColumn: user_id
  32. algorithmExpression: ds${user_id % 2}
  33. tableStrategy:
  34. inline:
  35. shardingColumn: order_id
  36. algorithmExpression: t_order_item${order_id % 2}
  37. bindingTables:
  38. - t_order,t_order_item
  39. broadcastTables:
  40. - t_config
  41. defaultDataSourceName: ds0
  42. defaultTableStrategy:
  43. none:
  44. defaultKeyGenerator:
  45. type: SNOWFLAKE
  46. column: order_id
  47. props:
  48. sql.show: true

Read-Write Split

Configuration Item Explanation

  1. dataSources:
  2. ds_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_slave0
  10. username: root
  11. password:
  12. ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds_slave1
  15. username: root
  16. password:
  17. masterSlaveRule:
  18. name: ds_ms
  19. masterDataSourceName: ds_master
  20. slaveDataSourceNames: [ds_slave0, ds_slave1]
  21. props:
  22. sql.show: true

Create a DataSource through the YamlMasterSlaveDataSourceFactory factory class:

  1. DataSource dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);

Data Masking

Configuration Item Explanation

  1. dataSource: !!org.apache.commons.dbcp2.BasicDataSource
  2. driverClassName: com.mysql.jdbc.Driver
  3. url: jdbc:mysql://127.0.0.1:3306/encrypt?serverTimezone=UTC&useSSL=false
  4. username: root
  5. password:
  6. encryptRule:
  7. encryptors:
  8. encryptor_aes:
  9. type: aes
  10. props:
  11. aes.key.value: 123456abc
  12. encryptor_md5:
  13. type: md5
  14. tables:
  15. t_encrypt:
  16. columns:
  17. user_id:
  18. plainColumn: user_plain
  19. cipherColumn: user_cipher
  20. encryptor: encryptor_aes
  21. order_id:
  22. cipherColumn: order_cipher
  23. encryptor: encryptor_md5
  24. props:
  25. query.with.cipher.column: true # use ciphertext column query

Orchestration

Configuration Item Explanation

  1. # Omit data sharding, Read-Write split, and Data masking configuration.
  2. orchestration:
  3. name: orchestration_ds
  4. overwrite: true
  5. registry:
  6. type: zookeeper
  7. namespace: orchestration
  8. serverLists: localhost:2181

ShardingSphere-3.x

Data Sharding

Configuration Item Explanation

  1. dataSources:
  2. ds0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds0
  5. username: root
  6. password:
  7. ds1: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds1
  10. username: root
  11. password:
  12. shardingRule:
  13. tables:
  14. t_order:
  15. actualDataNodes: ds${0..1}.t_order${0..1}
  16. databaseStrategy:
  17. inline:
  18. shardingColumn: user_id
  19. algorithmExpression: ds${user_id % 2}
  20. tableStrategy:
  21. inline:
  22. shardingColumn: order_id
  23. algorithmExpression: t_order${order_id % 2}
  24. keyGeneratorColumnName: order_id
  25. t_order_item:
  26. actualDataNodes: ds${0..1}.t_order_item${0..1}
  27. databaseStrategy:
  28. inline:
  29. shardingColumn: user_id
  30. algorithmExpression: ds${user_id % 2}
  31. tableStrategy:
  32. inline:
  33. shardingColumn: order_id
  34. algorithmExpression: t_order_item${order_id % 2}
  35. bindingTables:
  36. - t_order,t_order_item
  37. broadcastTables:
  38. - t_config
  39. defaultDataSourceName: ds0
  40. defaultTableStrategy:
  41. none:
  42. defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator
  43. props:
  44. sql.show: true

Read-Write Split

Configuration Item Explanation

  1. dataSources:
  2. ds_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_slave0
  10. username: root
  11. password:
  12. ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds_slave1
  15. username: root
  16. password:
  17. masterSlaveRule:
  18. name: ds_ms
  19. masterDataSourceName: ds_master
  20. slaveDataSourceNames: [ds_slave0, ds_slave1]
  21. props:
  22. sql.show: true
  23. configMap:
  24. key1: value1

Create a DataSource through the YamlMasterSlaveDataSourceFactory factory class:

  1. DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);

Orchestration

Configuration Item Explanation

  1. # Omit data sharding, Read-Write split configuration.
  2. orchestration:
  3. name: orchestration_ds
  4. overwrite: true
  5. registry:
  6. namespace: orchestration
  7. serverLists: localhost:2181

ShardingSphere-2.x

Data Sharding

Configuration Item Explanation

  1. dataSources:
  2. db0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: org.h2.Driver
  4. url: jdbc:h2:mem:db0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  5. username: sa
  6. password:
  7. maxActive: 100
  8. db1: !!org.apache.commons.dbcp.BasicDataSource
  9. driverClassName: org.h2.Driver
  10. url: jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  11. username: sa
  12. password:
  13. maxActive: 100
  14. shardingRule:
  15. tables:
  16. config:
  17. actualDataNodes: db${0..1}.t_config
  18. t_order:
  19. actualDataNodes: db${0..1}.t_order_${0..1}
  20. databaseStrategy:
  21. standard:
  22. shardingColumn: user_id
  23. preciseAlgorithmClassName: io.shardingjdbc.core.yaml.fixture.SingleAlgorithm
  24. tableStrategy:
  25. inline:
  26. shardingColumn: order_id
  27. algorithmInlineExpression: t_order_${order_id % 2}
  28. keyGeneratorColumnName: order_id
  29. keyGeneratorClass: io.shardingjdbc.core.yaml.fixture.IncrementKeyGenerator
  30. t_order_item:
  31. actualDataNodes: db${0..1}.t_order_item_${0..1}
  32. # The strategy of binding the rest of the tables in the table is the same as the strategy of the first table
  33. databaseStrategy:
  34. standard:
  35. shardingColumn: user_id
  36. preciseAlgorithmClassName: io.shardingjdbc.core.yaml.fixture.SingleAlgorithm
  37. tableStrategy:
  38. inline:
  39. shardingColumn: order_id
  40. algorithmInlineExpression: t_order_item_${order_id % 2}
  41. bindingTables:
  42. - t_order,t_order_item
  43. # Default database sharding strategy
  44. defaultDatabaseStrategy:
  45. none:
  46. defaultTableStrategy:
  47. complex:
  48. shardingColumns: id, order_id
  49. algorithmClassName: io.shardingjdbc.core.yaml.fixture.MultiAlgorithm
  50. props:
  51. sql.show: true

Read-Write Split

concept

In order to relieve the pressure on the database, the write and read operations are separated into different data sources. The write library is called the master library, and the read library is called the slave library. One master library can be configured with multiple slave libraries.

Supported

  1. Provides a read-write separation configuration with one master and multiple slaves, which can be used independently or with sub-databases and sub-meters.
  2. Independent use of read-write separation to support SQL transparent transmission.
  3. In the same thread and the same database connection, if there is a write operation, subsequent read operations will be read from the main library to ensure data consistency.
  4. Spring namespace.
  5. Hint-based mandatory main library routing.

Unsupported

  1. Data synchronization between the master library and the slave library.
  2. Data inconsistency caused by the data synchronization delay of the master library and the slave library.
  3. Double writing or multiple writing in the main library.

rule configuration

  1. dataSources:
  2. db_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: org.h2.Driver
  4. url: jdbc:h2:mem:db_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  5. username: sa
  6. password:
  7. maxActive: 100
  8. db_slave_0: !!org.apache.commons.dbcp.BasicDataSource
  9. driverClassName: org.h2.Driver
  10. url: jdbc:h2:mem:db_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  11. username: sa
  12. password:
  13. maxActive: 100
  14. db_slave_1: !!org.apache.commons.dbcp.BasicDataSource
  15. driverClassName: org.h2.Driver
  16. url: jdbc:h2:mem:db_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
  17. username: sa
  18. password:
  19. maxActive: 100
  20. masterSlaveRule:
  21. name: db_ms
  22. masterDataSourceName: db_master
  23. slaveDataSourceNames: [db_slave_0, db_slave_1]
  24. configMap:
  25. key1: value1

Create a DataSource through the MasterSlaveDataSourceFactory factory class:

  1. DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);

Orchestration

Configuration Item Explanation

Zookeeper sharding table and database Orchestration Configuration Item Explanation

  1. dataSources: Data sources configuration
  2. shardingRule: Sharding rule configuration
  3. orchestration: Zookeeper Orchestration Configuration
  4. name: Orchestration name
  5. overwrite: Whether to overwrite local configurations with config center configurations; if it can, each initialization should refer to local configurations
  6. zookeeper: Registry center Configuration
  7. namespace: Registry center namespace
  8. serverLists: The list of servers that connect to governance instance, including IP and port number, use commas to separate, such as: host1:2181,host2:2181
  9. baseSleepTimeMilliseconds: The initial millisecond value of the interval to wait for retry
  10. maxSleepTimeMilliseconds: The maximum millisecond value of the interval to wait for retry
  11. maxRetries: The maximum retry count
  12. sessionTimeoutMilliseconds: The session timeout milliseconds
  13. connectionTimeoutMilliseconds: The connecton timeout milliseconds
  14. digest: Permission token to connect to Zookeeper. default no authorization is required

Etcd sharding table and database Orchestration Configuration Item Explanation

  1. dataSources: Data sources configuration
  2. shardingRule: Sharding rule configuration
  3. orchestration: Etcd Orchestration Configuration
  4. name: Orchestration name
  5. overwrite: Whether to overwrite local configurations with config center configurations; if it can, each initialization should refer to local configurations
  6. etcd: Registry center Configuration
  7. serverLists: The list of servers that connect to governance instance, including IP and port number, use commas to separate, such as: http://host1:2379,http://host2:2379
  8. timeToLiveSeconds: Time to live seconds for ephemeral nodes
  9. timeoutMilliseconds: The request timeout milliseconds
  10. maxRetries: The maximum retry count
  11. retryIntervalMilliseconds: The retry interval milliseconds

Sharding table and database Data source construction method

  1. DataSource dataSource = OrchestrationShardingDataSourceFactory.createDataSource(yamlFile);

Read-Write split Data source construction method

  1. DataSource dataSource = OrchestrationMasterSlaveDataSourceFactory.createDataSource(yamlFile);