本文整理汇总了Java中com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule类的典型用法代码示例。如果您正苦于以下问题:Java ShardingRule类的具体用法?Java ShardingRule怎么用?Java ShardingRule使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ShardingRule类属于com.dangdang.ddframe.rdb.sharding.api.rule包,在下文中一共展示了ShardingRule类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getShardingDataSource
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; //导入依赖的package包/类
private static ShardingDataSource getShardingDataSource() {
DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList(
"db0.t_order_0", "db0.t_order_1",
"db1.t_order_0", "db1.t_order_1"
)).dataSourceRule(dataSourceRule).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList(
"db0.t_order_item_0", "db0.t_order_item_1",
"db1.t_order_item_0", "db1.t_order_item_1"
)).dataSourceRule(dataSourceRule).build();
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule))
.bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
.databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())).build();
return new ShardingDataSource(shardingRule);
}
示例2: ShardingDataSource
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; //导入依赖的package包/类
@Bean(name = "shardingDataSource")
@Primary
public DataSource ShardingDataSource() {
log.info("-------------------- ShardingDataSource init ---------------------");
// 表规则可以指定每张表在数据源中的分布情况
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1"))
.dataSourceRule(dataSourceRule()).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item")
.actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule())
.build();
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule())
.tableRules(Arrays.asList(orderTableRule, orderItemTableRule))
// 绑定表代表一组表,这组表的逻辑表与实际表之间的映射关系是相同的。比如t_order与t_order_item就是这样一组绑定表关系,它们的分库与分表策略是完全相同的,那么可以使用它们的表规则将它们配置成绑定表
.bindingTableRules(Collections
.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
.databaseShardingStrategy(
new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm()))
// 多分片键值
// .tableShardingStrategy(new TableShardingStrategy(Arrays.asList("order_id","user_id"), new
// MultipleKeysModuloTableShardingAlgorithm()))
.build();
return new ShardingDataSource(shardingRule);
}
示例3: getShardingDataSource
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; //导入依赖的package包/类
public static ShardingDataSource getShardingDataSource(DataSourceRule dataSourceRule) {
// 表规则可以指定每张表在数据源中的分布情况
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1"))
.dataSourceRule(dataSourceRule).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item")
.actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule).build();
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule)
.tableRules(Arrays.asList(orderTableRule, orderItemTableRule))
// 绑定表代表一组表,这组表的逻辑表与实际表之间的映射关系是相同的。比如t_order与t_order_item就是这样一组绑定表关系,它们的分库与分表策略是完全相同的,那么可以使用它们的表规则将它们配置成绑定表
.bindingTableRules(Collections
.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
.databaseShardingStrategy(
new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm()))
//多分片键值
// .tableShardingStrategy(new TableShardingStrategy(Arrays.asList("order_id","user_id"), new MultipleKeysModuloTableShardingAlgorithm()))
.build();
return new ShardingDataSource(shardingRule);
}
示例4: shardingDataSource
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; //导入依赖的package包/类
@Bean
@Primary
public DataSource shardingDataSource() throws Exception {
// masterSlaveDataSource Map
Map<String,DataSource> dataSourceMap = getMasterSlaveDataSourceMap();
// dataSourceRule
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
//tableRuleList
List<TableRule> tableRuleList = new ArrayList<TableRule>(tableRuleBuilderList.size());
//shardingRule
//shardingRuleBuilder由业务系统自行构建分库分表规则
tableRuleBuilderList.forEach(tableRuleBuilder -> {
tableRuleList.add(tableRuleBuilder.dataSourceRule(dataSourceRule).build());
});
//shardingRule
ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(tableRuleList)
.databaseShardingStrategy(databaseShardingStrategy)
.tableShardingStrategy(tableShardingStrategy).build();
return new ShardingDataSource(shardingRule);
}