混合规则

背景信息

ShardingSphere 涵盖了很多功能,例如,分库分片、读写分离、数据加密等。这些功能用户可以单独进行使用,也可以配合一起使用,下面是基于 JAVA API 的配置示例。

配置示例

  1. // 分片配置
  2. private ShardingRuleConfiguration createShardingRuleConfiguration() {
  3. ShardingRuleConfiguration result = new ShardingRuleConfiguration();
  4. result.getTables().add(getOrderTableRuleConfiguration());
  5. result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "inline"));
  6. result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "standard_test_tbl"));
  7. Properties props = new Properties();
  8. props.setProperty("algorithm-expression", "demo_ds_${user_id % 2}");
  9. result.getShardingAlgorithms().put("inline", new AlgorithmConfiguration("INLINE", props));
  10. result.getShardingAlgorithms().put("standard_test_tbl", new AlgorithmConfiguration("STANDARD_TEST_TBL", new Properties()));
  11. result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
  12. return result;
  13. }
  14. private ShardingTableRuleConfiguration getOrderTableRuleConfiguration() {
  15. ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}");
  16. result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
  17. return result;
  18. }
  19. // 读写分离配置
  20. private static ReadwriteSplittingRuleConfiguration createReadwriteSplittingConfiguration() {
  21. ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfiguration1 = new ReadwriteSplittingDataSourceRuleConfiguration("replica_ds_0", Arrays.asList("readwrite_ds_0"), true), "");
  22. ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfiguration2 = new ReadwriteSplittingDataSourceRuleConfiguration("replica_ds_1", Arrays.asList("readwrite_ds_1"), true), "");
  23. Collection<ReadwriteSplittingDataSourceRuleConfiguration> dataSources = new LinkedList<>();
  24. dataSources.add(dataSourceRuleConfiguration1);
  25. dataSources.add(dataSourceRuleConfiguration2);
  26. return new ReadwriteSplittingRuleConfiguration(dataSources, Collections.emptyMap());
  27. }
  28. // 数据加密配置
  29. private static EncryptRuleConfiguration createEncryptRuleConfiguration() {
  30. Properties props = new Properties();
  31. props.setProperty("aes-key-value", "123456");
  32. EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("username", new EncryptColumnItemRuleConfiguration("username", "name_encryptor"));
  33. EncryptColumnRuleConfiguration columnConfigTest = new EncryptColumnRuleConfiguration("pwd", new EncryptColumnItemRuleConfiguration("pwd", "pwd_encryptor"));
  34. columnConfigTest.setAssistedQuery(new EncryptColumnItemRuleConfiguration("assisted_query_pwd", "pwd_encryptor"));
  35. columnConfigTest.setLikeQuery(new EncryptColumnItemRuleConfiguration("like_pwd", "like_encryptor"));
  36. EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_user", Arrays.asList(columnConfigAes, columnConfigTest));
  37. Map<String, AlgorithmConfiguration> encryptAlgorithmConfigs = new HashMap<>();
  38. encryptAlgorithmConfigs.put("name_encryptor", new AlgorithmConfiguration("AES", props));
  39. encryptAlgorithmConfigs.put("pwd_encryptor", new AlgorithmConfiguration("assistedTest", props));
  40. encryptAlgorithmConfigs.put("like_encryptor", new AlgorithmConfiguration("CHAR_DIGEST_LIKE", new Properties()));
  41. return new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), encryptAlgorithmConfigs);
  42. }