Java配置

配置示例

数据分片

  1. DataSource getShardingDataSource() throws SQLException {
  2. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  3. shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
  4. shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
  5. shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
  6. shardingRuleConfig.getBroadcastTables().add("t_config");
  7. shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
  8. shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new ModuloShardingTableAlgorithm()));
  9. return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig);
  10. }
  11. TableRuleConfiguration getOrderTableRuleConfiguration() {
  12. TableRuleConfiguration result = new TableRuleConfiguration();
  13. result.setLogicTable("t_order");
  14. result.setActualDataNodes("ds${0..1}.t_order${0..1}");
  15. result.setKeyGeneratorColumnName("order_id");
  16. return result;
  17. }
  18. TableRuleConfiguration getOrderItemTableRuleConfiguration() {
  19. TableRuleConfiguration result = new TableRuleConfiguration();
  20. result.setLogicTable("t_order_item");
  21. result.setActualDataNodes("ds${0..1}.t_order_item${0..1}");
  22. return result;
  23. }
  24. Map<String, DataSource> createDataSourceMap() {
  25. Map<String, DataSource> result = new HashMap<>();
  26. result.put("ds0", DataSourceUtil.createDataSource("ds0"));
  27. result.put("ds1", DataSourceUtil.createDataSource("ds1"));
  28. return result;
  29. }

读写分离

  1. DataSource getMasterSlaveDataSource() throws SQLException {
  2. MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration();
  3. masterSlaveRuleConfig.setName("ds_master_slave");
  4. masterSlaveRuleConfig.setMasterDataSourceName("ds_master");
  5. masterSlaveRuleConfig.setSlaveDataSourceNames(Arrays.asList("ds_slave0", "ds_slave1"));
  6. return MasterSlaveDataSourceFactory.createDataSource(createDataSourceMap(), masterSlaveRuleConfig, new LinkedHashMap<String, Object>(), new Properties());
  7. }
  8. Map<String, DataSource> createDataSourceMap() {
  9. Map<String, DataSource> result = new HashMap<>();
  10. result.put("ds_master", DataSourceUtil.createDataSource("ds_master"));
  11. result.put("ds_slave0", DataSourceUtil.createDataSource("ds_slave0"));
  12. result.put("ds_slave1", DataSourceUtil.createDataSource("ds_slave1"));
  13. return result;
  14. }

数据分片 + 读写分离

  1. DataSource getDataSource() throws SQLException {
  2. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  3. shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
  4. shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
  5. shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
  6. shardingRuleConfig.getBroadcastTables().add("t_config");
  7. shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new ModuloShardingDatabaseAlgorithm()));
  8. shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new ModuloShardingTableAlgorithm()));
  9. shardingRuleConfig.setMasterSlaveRuleConfigs(getMasterSlaveRuleConfigurations());
  10. return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new HashMap<String, Object>(), new Properties());
  11. }
  12. TableRuleConfiguration getOrderTableRuleConfiguration() {
  13. TableRuleConfiguration result = new TableRuleConfiguration();
  14. result.setLogicTable("t_order");
  15. result.setActualDataNodes("ds_${0..1}.t_order_${[0, 1]}");
  16. result.setKeyGeneratorColumnName("order_id");
  17. return result;
  18. }
  19. TableRuleConfiguration getOrderItemTableRuleConfiguration() {
  20. TableRuleConfiguration result = new TableRuleConfiguration();
  21. result.setLogicTable("t_order_item");
  22. result.setActualDataNodes("ds_${0..1}.t_order_item_${[0, 1]}");
  23. return result;
  24. }
  25. List<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() {
  26. MasterSlaveRuleConfiguration masterSlaveRuleConfig1 = new MasterSlaveRuleConfiguration("ds_0", "demo_ds_master_0", Arrays.asList("demo_ds_master_0_slave_0", "demo_ds_master_0_slave_1"));
  27. MasterSlaveRuleConfiguration masterSlaveRuleConfig2 = new MasterSlaveRuleConfiguration("ds_1", "demo_ds_master_1", Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1"));
  28. return Lists.newArrayList(masterSlaveRuleConfig1, masterSlaveRuleConfig2);
  29. }
  30. Map<String, DataSource> createDataSourceMap() {
  31. final Map<String, DataSource> result = new HashMap<>();
  32. result.put("demo_ds_master_0", DataSourceUtil.createDataSource("demo_ds_master_0"));
  33. result.put("demo_ds_master_0_slave_0", DataSourceUtil.createDataSource("demo_ds_master_0_slave_0"));
  34. result.put("demo_ds_master_0_slave_1", DataSourceUtil.createDataSource("demo_ds_master_0_slave_1"));
  35. result.put("demo_ds_master_1", DataSourceUtil.createDataSource("demo_ds_master_1"));
  36. result.put("demo_ds_master_1_slave_0", DataSourceUtil.createDataSource("demo_ds_master_1_slave_0"));
  37. result.put("demo_ds_master_1_slave_1", DataSourceUtil.createDataSource("demo_ds_master_1_slave_1"));
  38. return result;
  39. }

数据治理

  1. DataSource getDataSource() throws SQLException {
  2. return OrchestrationShardingDataSourceFactory.createDataSource(
  3. createDataSourceMap(), createShardingRuleConfig(), new HashMap<String, Object>(), new Properties(),
  4. new OrchestrationConfiguration("orchestration-sharding-data-source", getRegistryCenterConfiguration(), false));
  5. }
  6. private RegistryCenterConfiguration getRegistryCenterConfiguration() {
  7. RegistryCenterConfiguration regConfig = new RegistryCenterConfiguration();
  8. regConfig.setServerLists("localhost:2181");
  9. regConfig.setNamespace("sharding-sphere-orchestration");
  10. return regConfig;
  11. }

配置项说明

数据分片

ShardingDataSourceFactory

数据分片的数据源创建工厂。

名称数据类型说明
dataSourceMapMap<String, DataSource>数据源配置
shardingRuleConfigShardingRuleConfiguration数据分片配置规则
configMap (?)Map<String, Object>用户自定义配置
props (?)Properties属性配置

ShardingRuleConfiguration

分片规则配置对象。

名称数据类型说明
tableRuleConfigsCollection<TableRuleConfiguration>分片规则列表
bindingTableGroups (?)Collection<String>绑定表规则列表
broadcastTables (?)Collection<String>广播表规则列表
defaultDataSourceName (?)String未配置分片规则的表将通过默认数据源定位
defaultDatabaseShardingStrategyConfig (?)ShardingStrategyConfiguration默认分库策略
defaultTableShardingStrategyConfig (?)ShardingStrategyConfiguration默认分表策略
defaultKeyGenerator (?)KeyGenerator默认自增列值生成器,缺省使用io.shardingsphere.core.keygen.DefaultKeyGenerator
masterSlaveRuleConfigs (?)Collection<MasterSlaveRuleConfiguration>读写分离规则,缺省表示不使用读写分离

TableRuleConfiguration

表分片规则配置对象。

名称数据类型说明
logicTableString逻辑表名称
actualDataNodes (?)String由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点。用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
databaseShardingStrategyConfig (?)ShardingStrategyConfiguration分库策略,缺省表示使用默认分库策略
tableShardingStrategyConfig (?)ShardingStrategyConfiguration分表策略,缺省表示使用默认分表策略
logicIndex (?)String逻辑索引名称,对于分表的Oracle/PostgreSQL数据库中DROP INDEX XXX语句,需要通过配置逻辑索引名称定位所执行SQL的真实分表
keyGeneratorColumnName (?)String自增列名称,缺省表示不使用自增主键生成器
keyGenerator (?)KeyGenerator自增列值生成器,缺省表示使用默认自增主键生成器

StandardShardingStrategyConfiguration

ShardingStrategyConfiguration的实现类,用于单分片键的标准分片场景。

名称数据类型说明
shardingColumnString分片列名称
preciseShardingAlgorithmPreciseShardingAlgorithm精确分片算法,用于=和IN
rangeShardingAlgorithm (?)RangeShardingAlgorithm范围分片算法,用于BETWEEN

ComplexShardingStrategyConfiguration

ShardingStrategyConfiguration的实现类,用于多分片键的复合分片场景。

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

InlineShardingStrategyConfiguration

ShardingStrategyConfiguration的实现类,用于配置行表达式分片策略。

名称数据类型说明
shardingColumnString分片列名称
algorithmExpressionString分片算法行表达式,需符合groovy语法,详情请参考行表达式

HintShardingStrategyConfiguration

ShardingStrategyConfiguration的实现类,用于配置Hint方式分片策略。

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

NoneShardingStrategyConfiguration

ShardingStrategyConfiguration的实现类,用于配置不分片的策略。

PropertiesConstant

属性配置项,可以为以下属性。

名称数据类型说明
sql.show (?)boolean是否开启SQL显示,默认值: false
executor.size (?)int工作线程数量,默认值: CPU核数
max.connections.size.per.query (?)int每个物理数据库为每次查询分配的最大连接数量。默认值: 1
check.table.metadata.enabled (?)boolean是否在启动时检查分表元数据一致性,默认值: false

configMap

用户自定义配置。

读写分离

MasterSlaveDataSourceFactory

读写分离的数据源创建工厂。

名称数据类型说明
dataSourceMapMap<String, DataSource>数据源与其名称的映射
masterSlaveRuleConfigMasterSlaveRuleConfiguration读写分离规则
configMap (?)Map<String, Object>用户自定义配置
props (?)Properties属性配置

MasterSlaveRuleConfiguration

读写分离规则配置对象。

名称数据类型说明
nameString读写分离数据源名称
masterDataSourceNameString主库数据源名称
slaveDataSourceNamesCollection<String>从库数据源名称列表
loadBalanceAlgorithm (?)MasterSlaveLoadBalanceAlgorithm从库负载均衡算法

configMap

用户自定义配置。

PropertiesConstant

属性配置项,可以为以下属性。

名称数据类型说明
sql.show (?)boolean是否打印SQL解析和改写日志,默认值: false
executor.size (?)int用于SQL执行的工作线程数量,为零则表示无限制。默认值: 0
max.connections.size.per.query (?)int每个物理数据库为每次查询分配的最大连接数量。默认值: 1
check.table.metadata.enabled (?)boolean是否在启动时检查分表元数据一致性,默认值: false

数据治理

OrchestrationShardingDataSourceFactory

数据分片 + 数据治理的数据源工厂。

名称数据类型说明
dataSourceMapMap<String, DataSource>同ShardingDataSourceFactory
shardingRuleConfigShardingRuleConfiguration同ShardingDataSourceFactory
configMap (?)Map<String, Object>同ShardingDataSourceFactory
props (?)Properties同ShardingDataSourceFactory
orchestrationConfigOrchestrationConfiguration数据治理规则配置

OrchestrationMasterSlaveDataSourceFactory

读写分离 + 数据治理的数据源工厂。

名称数据类型说明
dataSourceMapMap<String, DataSource>同MasterSlaveDataSourceFactory
masterSlaveRuleConfigMasterSlaveRuleConfiguration同MasterSlaveDataSourceFactory
configMap (?)Map<String, Object>同MasterSlaveDataSourceFactory
props (?)Properties同ShardingDataSourceFactory
orchestrationConfigOrchestrationConfiguration数据治理规则配置

OrchestrationConfiguration

数据治理规则配置对象。

名称数据类型说明
nameString数据治理实例名称
overwriteboolean本地配置是否覆盖注册中心配置,如果可覆盖,每次启动都以本地配置为准
regCenterConfigRegistryCenterConfiguration注册中心配置

RegistryCenterConfiguration

用于配置注册中心。

名称数据类型说明
serverListsString连接注册中心服务器的列表。包括IP地址和端口号。多个地址用逗号分隔。如: host1:2181,host2:2181
namespace (?)String注册中心的命名空间
digest (?)String连接注册中心的权限令牌。缺省为不需要权限验证
operationTimeoutMilliseconds (?)int操作超时的毫秒数,默认500毫秒
maxRetries (?)int连接失败后的最大重试次数,默认3次
retryIntervalMilliseconds (?)int重试间隔毫秒数,默认500毫秒
timeToLiveSeconds (?)int临时节点存活秒数,默认60秒