Skip to content

Commit

Permalink
fix #78
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 20, 2016
1 parent afc62b7 commit a366df1
Show file tree
Hide file tree
Showing 34 changed files with 543 additions and 299 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,12 @@
## 规则配置
`Sharding-JDBC`的分库分表通过规则配置描述,请简单浏览配置全貌:
```java
ShardingRule shardingRule = new ShardingRule(
dataSourceRule,
Arrays.asList(tableRule),
new DatabaseShardingStrategy("sharding_column_1", new XXXShardingAlgorithm()),
new TableShardingStrategy("sharding_column_2", new XXXShardingAlgorithm()));
ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(tableRules)
.databaseShardingStrategy(new DatabaseShardingStrategy("sharding_column_1", new XXXShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("sharding_column_2", new XXXShardingAlgorithm())))
.build();
```
规则配置包括数据源配置、表规则配置、分库策略和分表策略组成。这只是最简单的配置方式,实际使用可更加灵活,如:多分片键,分片策略直接和`tableRule`绑定等。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.router.strategy.MultipleKeysShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.router.strategy.SingleKeyShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.MultipleKeysDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;
Expand All @@ -38,6 +34,10 @@
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.algorithm.ClosureTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.config.common.internal.parser.InlineParser;
import com.dangdang.ddframe.rdb.sharding.router.strategy.MultipleKeysShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.router.strategy.ShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.router.strategy.SingleKeyShardingAlgorithm;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -83,9 +83,9 @@ public ShardingRuleBuilder(final String logRoot, final ShardingRuleConfig shardi
public ShardingRule build() {
DataSourceRule dataSourceRule = buildDataSourceRule();
Collection<TableRule> tableRules = buildTableRules(dataSourceRule);
return new ShardingRule(dataSourceRule, tableRules, buildBindingTableRules(tableRules),
buildShardingStrategy(shardingRuleConfig.getDefaultDatabaseStrategy(), DatabaseShardingStrategy.class),
buildShardingStrategy(shardingRuleConfig.getDefaultTableStrategy(), TableShardingStrategy.class));
return ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(tableRules).bindingTableRules(buildBindingTableRules(tableRules))
.databaseShardingStrategy(buildShardingStrategy(shardingRuleConfig.getDefaultDatabaseStrategy(), DatabaseShardingStrategy.class))
.tableShardingStrategy(buildShardingStrategy(shardingRuleConfig.getDefaultTableStrategy(), TableShardingStrategy.class)).build();
}

private DataSourceRule buildDataSourceRule() {
Expand All @@ -96,17 +96,13 @@ private DataSourceRule buildDataSourceRule() {
private Collection<TableRule> buildTableRules(final DataSourceRule dataSourceRule) {
Collection<TableRule> result = new ArrayList<>(shardingRuleConfig.getTables().size());
for (Entry<String, TableRuleConfig> each : shardingRuleConfig.getTables().entrySet()) {
TableRule tableRule;
if (null == each.getValue().getActualTables()) {
tableRule = new TableRule(each.getKey(), dataSourceRule,
buildShardingStrategy(each.getValue().getDatabaseStrategy(), DatabaseShardingStrategy.class),
buildShardingStrategy(each.getValue().getTableStrategy(), TableShardingStrategy.class));
} else {
tableRule = new TableRule(each.getKey(), new InlineParser(each.getValue().getActualTables()).evaluate(), dataSourceRule,
buildShardingStrategy(each.getValue().getDatabaseStrategy(), DatabaseShardingStrategy.class),
buildShardingStrategy(each.getValue().getTableStrategy(), TableShardingStrategy.class));
TableRule.TableRuleBuilder tableRuleBuilder = TableRule.builder(each.getKey()).dataSourceRule(dataSourceRule).dynamic(each.getValue().isDynamic())
.databaseShardingStrategy(buildShardingStrategy(each.getValue().getDatabaseStrategy(), DatabaseShardingStrategy.class))
.tableShardingStrategy(buildShardingStrategy(each.getValue().getTableStrategy(), TableShardingStrategy.class));
if (null != each.getValue().getActualTables()) {
tableRuleBuilder.actualTables(new InlineParser(each.getValue().getActualTables()).evaluate());
}
result.add(tableRule);
result.add(tableRuleBuilder.build());
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
@Setter
public class TableRuleConfig {

private boolean dynamic;

private String actualTables;

private StrategyConfig databaseStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@
InlineParserTest.class
})
public class AllTests {

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import javax.sql.DataSource;

import com.dangdang.ddframe.rdb.sharding.api.rule.DynamicDataNode;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
Expand All @@ -33,7 +34,9 @@

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

@Slf4j
public class ShardingRuleBuilderTest {
Expand All @@ -58,6 +61,35 @@ public void testMin() {
assertThat(shardingRule.getTableRules().size(), is(1));
}

@Test
public void testDynamic() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
Map<String, DataSource> dsMap = new HashMap<>();
dsMap.put("ds", new BasicDataSource());
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(ShardingRuleBuilderTest.class.getResourceAsStream("/config/config-dynamic.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder("config-dynamic.yaml", dsMap, config).build();
int i = 0;
for (TableRule each : shardingRule.getTableRules()) {
i++;
assertThat(each.getActualTables().size(), is(2));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db0")));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db1")));
switch (i) {
case 1:
assertThat(each.getLogicTable(), is("config"));
break;
case 2:
assertThat(each.getLogicTable(), is("t_order"));
break;
case 3:
assertThat(each.getLogicTable(), is("t_order_item"));
break;
default:
fail();
}
}
}

@Test(expected = IllegalArgumentException.class)
public void testClassNotFound() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
dataSource:
db0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
db1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100

tables:
config:
dynamic: true

t_order:
dynamic: true
databaseStrategy: &db001
shardingColumns: order_id
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.common.internal.fixture.SingleAlgorithm
tableStrategy: &table001
shardingColumns: id
algorithmExpression: t_order_${id.longValue() % 2}

t_order_item:
dynamic: true
#绑定表中其余的表的策略与第一张表的策略相同
databaseStrategy: *db001
tableStrategy: *table001

#默认数据库分片策略
defaultDatabaseStrategy:
shardingColumns: order_id, user_id
algorithmExpression: t_order_${id.longValue() % 2}
defaultTableStrategy:
shardingColumns: id, order_id
algorithmClassName: com.dangdang.ddframe.rdb.sharding.config.common.internal.fixture.MultiAlgorithm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public final class ShardingJdbcDataSourceBeanDefinitionParserTag {

public static final String LOGIC_TABLES_ATTR = "logic-tables";

public static final String DYNAMIC_TABLE_ATTR = "dynamic";

public static final String ACTUAL_TABLES_ATTR = "actual-tables";

public static final String DATABASE_STRATEGY_ATTR = "database-strategy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ private Map<String, BeanDefinition> parseTableRulesConfig(final Element element)

private BeanDefinition parseTableRuleConfig(final Element tableElement) {
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(TableRuleConfig.class);
String dynamic = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DYNAMIC_TABLE_ATTR);
if (!Strings.isNullOrEmpty(dynamic)) {
factory.addPropertyValue("dynamic", dynamic);
}
String actualTables = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.ACTUAL_TABLES_ATTR);
if (!Strings.isNullOrEmpty(actualTables)) {
factory.addPropertyValue("actualTables", actualTables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<xsd:element name="table-rule">
<xsd:complexType>
<xsd:attribute name="logic-table" type="xsd:string" use="required" />
<xsd:attribute name="dynamic" type="xsd:string" use="optional" />
<xsd:attribute name="actual-tables" type="xsd:string" use="optional" />
<xsd:attribute name="database-strategy" type="xsd:string" use="optional" />
<xsd:attribute name="table-strategy" type="xsd:string" use="optional" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<rdb:data-source id="shardingDataSource">
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1">
<rdb:table-rules>
<rdb:table-rule logic-table="t_order" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/>
<rdb:table-rule logic-table="t_order_item" database-strategy="databaseStrategy" table-strategy="orderItemTableStrategy"/>
<rdb:table-rule logic-table="t_order" dynamic="true" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/>
<rdb:table-rule logic-table="t_order_item" dynamic="true" database-strategy="databaseStrategy" table-strategy="orderItemTableStrategy"/>
</rdb:table-rules>
</rdb:sharding-rule>
</rdb:data-source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,34 @@

<bean id="orderTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
<constructor-arg value="t_order" index="0"/>
<constructor-arg index="1">
<constructor-arg value="false" index="1"/>
<constructor-arg index="2">
<list>
<value>t_order_0</value>
<value>t_order_1</value>
<value>t_order_2</value>
<value>t_order_3</value>
</list>
</constructor-arg>
<constructor-arg index="2" ref="dataSourceRule"/>
<constructor-arg index="3" ref="databaseShardingStrategy"/>
<constructor-arg index="4" ref="tableShardingStrategy"/>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4" ref="databaseShardingStrategy"/>
<constructor-arg index="5" ref="tableShardingStrategy"/>
</bean>

<bean id="orderItemTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
<constructor-arg value="t_order_item" index="0"/>
<constructor-arg index="1">
<constructor-arg value="false" index="1"/>
<constructor-arg index="2">
<list>
<value>t_order_item_0</value>
<value>t_order_item_1</value>
<value>t_order_item_2</value>
<value>t_order_item_3</value>
</list>
</constructor-arg>
<constructor-arg index="2" ref="dataSourceRule"/>
<constructor-arg index="3" ref="databaseShardingStrategy"/>
<constructor-arg index="4" ref="tableShardingStrategy"/>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4" ref="databaseShardingStrategy"/>
<constructor-arg index="5" ref="tableShardingStrategy"/>
</bean>

<bean id="databaseShardingStrategy" class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy">
Expand All @@ -68,6 +70,15 @@
<ref bean="orderItemTableRule"/>
</list>
</constructor-arg>
<constructor-arg index="2">
<null />
</constructor-arg>
<constructor-arg index="3">
<null />
</constructor-arg>
<constructor-arg index="4">
<null />
</constructor-arg>
</bean>

<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory" factory-method="createDataSource">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,42 @@

<bean id="orderTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
<constructor-arg value="t_order" index="0"/>
<constructor-arg index="1">
<constructor-arg value="false" index="1"/>
<constructor-arg index="2">
<list>
<value>t_order_0</value>
<value>t_order_1</value>
<value>t_order_2</value>
<value>t_order_3</value>
</list>
</constructor-arg>
<constructor-arg index="2" ref="dataSourceRule"/>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="5">
<null />
</constructor-arg>
</bean>

<bean id="orderItemTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
<constructor-arg value="t_order_item" index="0"/>
<constructor-arg index="1">
<constructor-arg value="false" index="1"/>
<constructor-arg index="2">
<list>
<value>t_order_item_0</value>
<value>t_order_item_1</value>
<value>t_order_item_2</value>
<value>t_order_item_3</value>
</list>
</constructor-arg>
<constructor-arg index="2" ref="dataSourceRule"/>
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="5">
<null />
</constructor-arg>
</bean>

<bean id="databaseShardingStrategy" class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy">
Expand All @@ -63,8 +77,11 @@
<ref bean="orderItemTableRule"/>
</list>
</constructor-arg>
<constructor-arg index="2" ref="databaseShardingStrategy"/>
<constructor-arg index="3" ref="tableShardingStrategy"/>
<constructor-arg index="2">
<null />
</constructor-arg>
<constructor-arg index="3" ref="databaseShardingStrategy"/>
<constructor-arg index="4" ref="tableShardingStrategy"/>
</bean>

<bean id="shardingDataSource" class="com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory" factory-method="createDataSource">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* 已废弃, 请使用ShardingDataSourceFactory创建数据源. 未来版本中将删除此类.
* </p>
*
* @deprecated
* @author zhangliang
*/
@Deprecated
Expand Down
Loading

0 comments on commit a366df1

Please sign in to comment.