数据分片

背景信息

数据分片 Java API 规则配置允许用户直接通过编写 Java 代码的方式,完成 ShardingSphereDataSource 对象的创建,Java API 的配置方式非常灵活,不需要依赖额外的 jar 包就能够集成各种类型的业务系统。

参数解释

配置入口

类名称:org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration

可配置属性:

名称数据类型说明默认值
tables (+)Collection<ShardingTableRuleConfiguration>分片表规则列表-
autoTables (+)Collection<ShardingAutoTableRuleConfiguration>自动分片表规则列表-
bindingTableGroups ()Collection<String>绑定表规则列表
broadcastTables ()Collection<String>广播表规则列表
defaultDatabaseShardingStrategy (?)ShardingStrategyConfiguration默认分库策略不分片
defaultTableShardingStrategy (?)ShardingStrategyConfiguration默认分表策略不分片
defaultKeyGenerateStrategy (?)KeyGeneratorConfiguration默认自增列生成器配置雪花算法
defaultAuditStrategy (?)ShardingAuditStrategyConfiguration默认分片审计策略配置强制分片键
defaultShardingColumn (?)String默认分片列名称
shardingAlgorithms (+)Map<String, AlgorithmConfiguration>分片算法名称和配置
keyGenerators (?)Map<String, AlgorithmConfiguration>自增列生成算法名称和配置
auditors (?)Map<String, AlgorithmConfiguration>分片审计算法名称和配置

分片表配置

类名称:org.apache.shardingsphere.sharding.api.config.ShardingTableRuleConfiguration

可配置属性:

名称数据类型说明默认值
logicTableString分片逻辑表名称-
actualDataNodes (?)String由数据源名 + 表名组成,以小数点分隔。
多个表以逗号分隔,支持行表达式
使用已知数据源与逻辑表名称生成数据节点,用于广播表或只分库不分表且所有库的表结构完全一致的情况
databaseShardingStrategy (?)ShardingStrategyConfiguration分库策略使用默认分库策略
tableShardingStrategy (?)ShardingStrategyConfiguration分表策略使用默认分表策略
keyGenerateStrategy (?)KeyGeneratorConfiguration自增列生成器使用默认自增主键生成器
auditStrategy (?)ShardingAuditStrategyConfiguration分片审计策略使用默认分片审计策略

自动分片表配置

类名称:org.apache.shardingsphere.sharding.api.config.ShardingAutoTableRuleConfiguration

可配置属性:

名称数据类型说明默认值
logicTableString分片逻辑表名称-
actualDataSources (?)String数据源名称,多个数据源以逗号分隔使用全部配置的数据源
shardingStrategy (?)ShardingStrategyConfiguration分片策略使用默认分片策略
keyGenerateStrategy (?)KeyGeneratorConfiguration自增列生成器使用默认自增主键生成器
auditStrategy (?)ShardingAuditStrategyConfiguration分片审计策略使用默认分片审计策略

分片策略配置

标准分片策略配置

类名称:org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration

可配置属性:

名称数据类型说明
shardingColumnString分片列名称
shardingAlgorithmNameString分片算法名称

复合分片策略配置

类名称:org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration

可配置属性:

名称数据类型说明
shardingColumnsString分片列名称,多个列以逗号分隔
shardingAlgorithmNameString分片算法名称

Hint 分片策略配置

类名称:org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration

可配置属性:

名称数据类型说明
shardingAlgorithmNameString分片算法名称

不分片策略配置

类名称:org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration

可配置属性:无

算法类型的详情,请参见内置分片算法列表

分布式序列策略配置

类名称:org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration

可配置属性:

名称数据类型说明
columnString分布式序列列名称
keyGeneratorNameString分布式序列算法名称

算法类型的详情,请参见内置分布式序列算法列表

分片审计策略配置

类名称:org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration

可配置属性:

名称数据类型说明
auditorNamesCollection<String>分片审计算法名称
allowHintDisableBoolean是否禁用分片审计hint

算法类型的详情,请参见内置分片审计列算法列表

操作步骤

  1. 创建真实数据源映射关系,key 为数据源逻辑名称,value 为 DataSource 对象;
  2. 创建分片规则对象 ShardingRuleConfiguration,并初始化对象中的分片表对象 ShardingTableRuleConfiguration、绑定表集合、广播表集合,以及数据分片所依赖的分库策略和分表策略等参数;
  3. 调用 ShardingSphereDataSourceFactory 对象的 createDataSource 方法,创建 ShardingSphereDataSource。

配置示例

  1. public final class ShardingDatabasesAndTablesConfigurationPrecise implements ExampleConfiguration {
  2. @Override
  3. public DataSource getDataSource() throws SQLException {
  4. return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(createShardingRuleConfiguration()), new Properties());
  5. }
  6. private ShardingRuleConfiguration createShardingRuleConfiguration() {
  7. ShardingRuleConfiguration result = new ShardingRuleConfiguration();
  8. result.getTables().add(getOrderTableRuleConfiguration());
  9. result.getTables().add(getOrderItemTableRuleConfiguration());
  10. result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order, t_order_item"));
  11. result.getBroadcastTables().add("t_address");
  12. result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "inline"));
  13. result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "standard_test_tbl"));
  14. Properties props = new Properties();
  15. props.setProperty("algorithm-expression", "demo_ds_${user_id % 2}");
  16. result.getShardingAlgorithms().put("inline", new AlgorithmConfiguration("INLINE", props));
  17. result.getShardingAlgorithms().put("standard_test_tbl", new AlgorithmConfiguration("STANDARD_TEST_TBL", new Properties()));
  18. result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
  19. result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
  20. return result;
  21. }
  22. private ShardingTableRuleConfiguration getOrderTableRuleConfiguration() {
  23. ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}");
  24. result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
  25. result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), true));
  26. return result;
  27. }
  28. private ShardingTableRuleConfiguration getOrderItemTableRuleConfiguration() {
  29. ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}");
  30. result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_item_id", "snowflake"));
  31. return result;
  32. }
  33. private Map<String, DataSource> createDataSourceMap() {
  34. Map<String, DataSource> result = new HashMap<>();
  35. result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
  36. result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
  37. return result;
  38. }
  39. }

相关参考