影子库

背景信息

如果您只想使用 Java API 方式配置使用 ShardingSphere 影子库功能请参考以下配置。

参数解释

配置入口

类名称:org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration

可配置属性:

名称数据类型说明
dataSourcesMap<String, ShadowDataSourceConfiguration>影子数据源映射名称和配置
tablesMap<String, ShadowTableConfiguration>影子表名称和配置
shadowAlgorithmsMap<String, AlgorithmConfiguration>影子算法名称和配置
defaultShadowAlgorithmNameString默认影子算法名称

影子数据源配置

类名称:org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration

可配置属性:

名称数据类型说明
productionDataSourceNameString生产数据源名称
shadowDataSourceNameString影子数据源名称

影子表配置

类名称:org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration

可配置属性:

名称数据类型说明
dataSourceNamesCollection<String>影子表关联影子数据源映射名称列表
shadowAlgorithmNamesCollection<String>影子表关联影子算法名称列表

影子算法配置

类名称:org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration

可配置属性:

名称数据类型说明
typeString影子算法类型
propsProperties影子算法配置

算法类型的详情,请参见内置影子算法列表

操作步骤

  1. 创建生产和影子数据源。
  2. 配置影子规则
  • 配置影子数据源
  • 配置影子表
  • 配置影子算法

配置示例

  1. public final class ShadowConfiguration {
  2. @Override
  3. public DataSource getDataSource() throws SQLException {
  4. Map<String, DataSource> dataSourceMap = createDataSourceMap();
  5. return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, createRuleConfigurations(), createShardingSphereProps());
  6. }
  7. private Map<String, DataSource> createDataSourceMap() {
  8. Map<String, DataSource> result = new LinkedHashMap<>();
  9. result.put("ds", DataSourceUtil.createDataSource("demo_ds"));
  10. result.put("ds_shadow", DataSourceUtil.createDataSource("shadow_demo_ds"));
  11. return result;
  12. }
  13. private Collection<RuleConfiguration> createRuleConfigurations() {
  14. Collection<RuleConfiguration> result = new LinkedList<>();
  15. ShadowRuleConfiguration shadowRule = new ShadowRuleConfiguration();
  16. shadowRule.setDataSources(createShadowDataSources());
  17. shadowRule.setTables(createShadowTables());
  18. shadowRule.setShadowAlgorithms(createShadowAlgorithmConfigurations());
  19. result.add(shadowRule);
  20. return result;
  21. }
  22. private Map<String, ShadowDataSourceConfiguration> createShadowDataSources() {
  23. Map<String, ShadowDataSourceConfiguration> result = new LinkedHashMap<>();
  24. result.put("shadow-data-source", new ShadowDataSourceConfiguration("ds", "ds_shadow"));
  25. return result;
  26. }
  27. private Map<String, ShadowTableConfiguration> createShadowTables() {
  28. Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
  29. result.put("t_user", new ShadowTableConfiguration(Collections.singletonList("shadow-data-source"), createShadowAlgorithmNames()));
  30. return result;
  31. }
  32. private Collection<String> createShadowAlgorithmNames() {
  33. Collection<String> result = new LinkedList<>();
  34. result.add("user-id-insert-match-algorithm");
  35. result.add("simple-hint-algorithm");
  36. return result;
  37. }
  38. private Map<String, AlgorithmConfiguration> createShadowAlgorithmConfigurations() {
  39. Map<String, AlgorithmConfiguration> result = new LinkedHashMap<>();
  40. Properties userIdInsertProps = new Properties();
  41. userIdInsertProps.setProperty("operation", "insert");
  42. userIdInsertProps.setProperty("column", "user_type");
  43. userIdInsertProps.setProperty("value", "1");
  44. result.put("user-id-insert-match-algorithm", new AlgorithmConfiguration("VALUE_MATCH", userIdInsertProps));
  45. return result;
  46. }
  47. }

相关参考

影子库的特性描述