混合规则

混合配置的规则项之间的叠加使用是通过数据源名称和表名称关联的。

如果前一个规则是面向数据源聚合的,下一个规则在配置数据源时,则需要使用前一个规则配置的聚合后的逻辑数据源名称; 同理,如果前一个规则是面向表聚合的,下一个规则在配置表时,则需要使用前一个规则配置的聚合后的逻辑表名称。

配置项说明

  1. /* 数据源配置 */
  2. HikariDataSource writeDataSource0 = new HikariDataSource();
  3. writeDataSource0.setDriverClassName("com.mysql.jdbc.Driver");
  4. writeDataSource0.setJdbcUrl("jdbc:mysql://localhost:3306/db0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8");
  5. writeDataSource0.setUsername("root");
  6. writeDataSource0.setPassword("");
  7. HikariDataSource writeDataSource1 = new HikariDataSource();
  8. // ...忽略其他数据库配置项
  9. HikariDataSource read0OfwriteDataSource0 = new HikariDataSource();
  10. // ...忽略其他数据库配置项
  11. HikariDataSource read1OfwriteDataSource0 = new HikariDataSource();
  12. // ...忽略其他数据库配置项
  13. HikariDataSource read0OfwriteDataSource1 = new HikariDataSource();
  14. // ...忽略其他数据库配置项
  15. HikariDataSource read1OfwriteDataSource1 = new HikariDataSource();
  16. // ...忽略其他数据库配置项
  17. Map<String, DataSource> datasourceMaps = new HashMap<>(6);
  18. datasourceMaps.put("write_ds0", writeDataSource0);
  19. datasourceMaps.put("write_ds0_read0", read0OfwriteDataSource0);
  20. datasourceMaps.put("write_ds0_read1", read1OfwriteDataSource0);
  21. datasourceMaps.put("write_ds1", writeDataSource1);
  22. datasourceMaps.put("write_ds1_read0", read0OfwriteDataSource1);
  23. datasourceMaps.put("write_ds1_read1", read1OfwriteDataSource1);
  24. /* 分片规则配置 */
  25. // 表达式 ds_${0..1} 枚举值表示的是主从配置的逻辑数据源名称列表
  26. ShardingTableRuleConfiguration tOrderRuleConfiguration = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${[0, 1]}");
  27. tOrderRuleConfiguration.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
  28. tOrderRuleConfiguration.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "tOrderInlineShardingAlgorithm"));
  29. Properties tOrderShardingInlineProps = new Properties();
  30. tOrderShardingInlineProps.setProperty("algorithm-expression", "t_order_${order_id % 2}");
  31. tOrderRuleConfiguration.getShardingAlgorithms().putIfAbsent("tOrderInlineShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE",tOrderShardingInlineProps));
  32. ShardingTableRuleConfiguration tOrderItemRuleConfiguration = new ShardingTableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${[0, 1]}");
  33. tOrderItemRuleConfiguration.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_item_id", "snowflake"));
  34. tOrderRuleConfiguration.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_item_id", "tOrderItemInlineShardingAlgorithm"));
  35. Properties tOrderItemShardingInlineProps = new Properties();
  36. tOrderItemShardingInlineProps.setProperty("algorithm-expression", "t_order_item_${order_item_id % 2}");
  37. tOrderRuleConfiguration.getShardingAlgorithms().putIfAbsent("tOrderItemInlineShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE",tOrderItemShardingInlineProps));
  38. ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
  39. shardingRuleConfiguration.getTables().add(tOrderRuleConfiguration);
  40. shardingRuleConfiguration.getTables().add(tOrderItemRuleConfiguration);
  41. shardingRuleConfiguration.getBindingTableGroups().add("t_order, t_order_item");
  42. shardingRuleConfiguration.getBroadcastTables().add("t_bank");
  43. // 默认分库策略
  44. shardingRuleConfiguration.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "default_db_strategy_inline"));
  45. Properties defaultDatabaseStrategyInlineProps = new Properties();
  46. defaultDatabaseStrategyInlineProps.setProperty("algorithm-expression", "ds_${user_id % 2}");
  47. shardingRuleConfiguration.getShardingAlgorithms().put("default_db_strategy_inline", new ShardingSphereAlgorithmConfiguration("INLINE", defaultDatabaseStrategyInlineProps));
  48. // 分布式序列算法配置
  49. Properties snowflakeProperties = new Properties();
  50. snowflakeProperties.setProperty("worker-id", "123");
  51. shardingRuleConfiguration.getKeyGenerators().put("snowflake", new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", snowflakeProperties));
  52. /* 数据加密规则配置 */
  53. Properties encryptProperties = new Properties();
  54. encryptProperties.setProperty("aes-key-value", "123456");
  55. EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("user_name", "user_name", "", "user_name_plain", "name_encryptor");
  56. EncryptColumnRuleConfiguration columnConfigTest = new EncryptColumnRuleConfiguration("pwd", "pwd", "assisted_query_pwd", "", "pwd_encryptor");
  57. EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_user", Arrays.asList(columnConfigAes, columnConfigTest));
  58. Map<String, ShardingSphereAlgorithmConfiguration> encryptAlgorithmConfigs = new LinkedHashMap<>(2, 1);
  59. encryptAlgorithmConfigs.put("name_encryptor", new ShardingSphereAlgorithmConfiguration("AES", encryptProperties));
  60. encryptAlgorithmConfigs.put("pwd_encryptor", new ShardingSphereAlgorithmConfiguration("assistedTest", encryptProperties));
  61. EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), encryptAlgorithmConfigs);
  62. /* 读写分离规则配置 */
  63. ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfiguration1 = new ReadwriteSplittingDataSourceRuleConfiguration("ds_0", "write_ds0", Arrays.asList("write_ds0_read0", "write_ds0_read1"), "roundRobin");
  64. ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfiguration2 = new ReadwriteSplittingDataSourceRuleConfiguration("ds_1", "write_ds0", Arrays.asList("write_ds1_read0", "write_ds1_read0"), "roundRobin");
  65. //负载均衡算法
  66. Map<String, ShardingSphereAlgorithmConfiguration> loadBalanceMaps = new HashMap<>(1);
  67. loadBalanceMaps.put("roundRobin", new ShardingSphereAlgorithmConfiguration("ROUND_ROBIN", new Properties()));
  68. ReadwriteSplittingRuleConfiguration readWriteSplittingyRuleConfiguration = new ReadwriteSplittingRuleConfiguration(Arrays.asList(dataSourceConfiguration1, dataSourceConfiguration2), loadBalanceMaps);
  69. /* 其他配置 */
  70. Properties otherProperties = new Properties();
  71. otherProperties.setProperty("sql-show", "true");
  72. /* shardingDataSource 就是最终被ORM框架或其他jdbc框架引用的数据源名称 */
  73. DataSource shardingDataSource = ShardingSphereDataSourceFactory.createDataSource(datasourceMaps, Arrays.asList(shardingRuleConfiguration, readWriteSplittingyRuleConfiguration, encryptRuleConfiguration), otherProperties);