Shadow DB

Background

If you want to use the ShardingSphere Shadow DB feature in the Spring Boot environment, please refer to the following configuration.

Parameters

  1. spring.shardingsphere.datasource.names= # Omit the data source configuration, please refer to the usage
  2. spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.production-data-source-name= # Production data source name
  3. spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.shadow-data-source-name= # Shadow data source name
  4. spring.shardingsphere.rules.shadow.tables.<table-name>.data-source-names= # Shadow table location shadow data source names (multiple values are separated by ",")
  5. spring.shardingsphere.rules.shadow.tables.<table-name>.shadow-algorithm-names= # Shadow table location shadow algorithm names (multiple values are separated by ",")
  6. spring.shardingsphere.rules.shadow.defaultShadowAlgorithmName= # Default shadow algorithm name, optional item.
  7. spring.shardingsphere.rules.shadow.shadow-algorithms.<shadow-algorithm-name>.type= # Shadow algorithm type
  8. spring.shardingsphere.rules.shadow.shadow-algorithms.<shadow-algorithm-name>.props.xxx= # Shadow algorithm property configuration

For details, see list of built-in shadow algorithms

Procedure

  1. Configure the shadow library rules in the SpringBoot file, including configuration items such as data sources, shadow rules, and global properties.
  2. Start the SpringBoot program, the configuration will be loaded automatically, and the ShardingSphereDataSource will be initialized.

Sample

  1. spring.shardingsphere.datasource.names=ds,shadow-ds
  2. spring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://localhost:3306/ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  3. spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource
  4. spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.jdbc.Driver
  5. spring.shardingsphere.datasource.ds.username=root
  6. spring.shardingsphere.datasource.ds.password=
  7. spring.shardingsphere.datasource.shadow-ds.jdbc-url=jdbc:mysql://localhost:3306/shadow_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
  8. spring.shardingsphere.datasource.shadow-ds.type=com.zaxxer.hikari.HikariDataSource
  9. spring.shardingsphere.datasource.shadow-ds.driver-class-name=com.mysql.jdbc.Driver
  10. spring.shardingsphere.datasource.shadow-ds.username=root
  11. spring.shardingsphere.datasource.shadow-ds.password=
  12. spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.production-data-source-name=ds
  13. spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.shadow-data-source-name=shadow-ds
  14. spring.shardingsphere.rules.shadow.tables.t_user.data-source-names=shadow-data-source
  15. spring.shardingsphere.rules.shadow.tables.t_user.shadow-algorithm-names=user-id-insert-match-algorithm,simple-hint-algorithm
  16. spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.type=VALUE_MATCH
  17. spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.props.operation=insert
  18. spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.props.column=user_id
  19. spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.props.value=1
  20. spring.shardingsphere.rules.shadow.shadow-algorithms.simple-hint-algorithm.type=SIMPLE_HINT
  21. spring.shardingsphere.rules.shadow.shadow-algorithms.simple-hint-algorithm.props.shadow=true
  22. spring.shardingsphere.rules.shadow.shadow-algorithms.simple-hint-algorithm.props.foo=bar