Skip to content

Commit

Permalink
for #85 use datasource to manage master slave
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 30, 2016
1 parent 23db9ab commit 1303d55
Show file tree
Hide file tree
Showing 32 changed files with 374 additions and 340 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

* 分片策略灵活,可支持`=``BETWEEN``IN`等多维度分片,也可支持多分片键共用。
* `SQL`解析功能完善,支持聚合,分组,排序,`Limit``OR`等查询,并且支持`Binding Table`以及笛卡尔积的表查询。
* 支持柔性事务(目前仅最大努力送达型)。
* 支持读写分离。

`Sharding-JDBC`配置多样:

Expand Down Expand Up @@ -123,21 +125,24 @@

## 规则配置
`Sharding-JDBC`的分库分表通过规则配置描述,请简单浏览配置全貌:

```java
ShardingRule shardingRule = ShardingRule.builder()
ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(tableRules)
.databaseShardingStrategy(new DatabaseShardingStrategy("sharding_column_1", new XXXShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("sharding_column_2", new XXXShardingAlgorithm())))
.tableRules(tableRuleList)
.databaseShardingStrategy(new DatabaseShardingStrategy("sharding_column", new XXXShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("sharding_column", new XXXShardingAlgorithm())))
.build();
```

规则配置包括数据源配置、表规则配置、分库策略和分表策略组成。这只是最简单的配置方式,实际使用可更加灵活,如:多分片键,分片策略直接和`tableRule`绑定等。

>详细的规则配置请参考[用户指南](http://dangdangdotcom.github.io/sharding-jdbc/post/user_guide/)
## 使用原生JDBC接口
通过`ShardingDataSourceFactory`工厂和规则配置对象获取`ShardingDataSource``ShardingDataSource`实现自`JDBC`的标准接口`DataSource`。然后可通过`DataSource`选择使用原生`JDBC`开发,或者使用`JPA`, `MyBatis``ORM`工具。
`JDBC`原生实现为例:

```java
DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
Expand All @@ -150,15 +155,14 @@ try (
while(rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getInt(2));
System.out.println(rs.getInt(3));
}
}
}
```

## 使用Spring命名空间配置
```xml

```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@
<value>t_order_3</value>
</list>
</constructor-arg>
<constructor-arg index="3">
<null />
</constructor-arg>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="6" ref="databaseShardingStrategy"/>
<constructor-arg index="7" ref="tableShardingStrategy"/>
<constructor-arg index="5" ref="databaseShardingStrategy"/>
<constructor-arg index="6" ref="tableShardingStrategy"/>
</bean>

<bean id="orderItemTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
Expand All @@ -50,15 +47,12 @@
<value>t_order_item_3</value>
</list>
</constructor-arg>
<constructor-arg index="3">
<null />
</constructor-arg>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="6" ref="databaseShardingStrategy"/>
<constructor-arg index="7" ref="tableShardingStrategy"/>
<constructor-arg index="5" ref="databaseShardingStrategy"/>
<constructor-arg index="6" ref="tableShardingStrategy"/>
</bean>

<bean id="databaseShardingStrategy" class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy">
Expand Down Expand Up @@ -92,9 +86,6 @@
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="5">
<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 @@ -27,19 +27,16 @@
<value>t_order_3</value>
</list>
</constructor-arg>
<constructor-arg index="3">
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<null />
</constructor-arg>
<constructor-arg index="6">
<null />
</constructor-arg>
<constructor-arg index="7">
<null />
</constructor-arg>
</bean>

<bean id="orderItemTableRule" class="com.dangdang.ddframe.rdb.sharding.api.rule.TableRule">
Expand All @@ -53,19 +50,16 @@
<value>t_order_item_3</value>
</list>
</constructor-arg>
<constructor-arg index="3">
<constructor-arg index="3" ref="dataSourceRule"/>
<constructor-arg index="4">
<null />
</constructor-arg>
<constructor-arg index="4" ref="dataSourceRule"/>
<constructor-arg index="5">
<null />
</constructor-arg>
<constructor-arg index="6">
<null />
</constructor-arg>
<constructor-arg index="7">
<null />
</constructor-arg>
</bean>

<bean id="databaseShardingStrategy" class="com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy">
Expand Down Expand Up @@ -95,9 +89,6 @@
</constructor-arg>
<constructor-arg index="3" ref="databaseShardingStrategy"/>
<constructor-arg index="4" ref="tableShardingStrategy"/>
<constructor-arg index="5">
<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
@@ -0,0 +1,47 @@
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package com.dangdang.ddframe.rdb.sharding.api;

import com.dangdang.ddframe.rdb.sharding.jdbc.MasterSlaveDataSource;
import com.google.common.collect.Lists;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.sql.DataSource;

/**
* 读写分离数据源工厂.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MasterSlaveDataSourceFactory {

/**
* 创建读写分离数据源.
*
* @param logicDataSourceName 逻辑数据源名称
* @param masterDataSource 主节点数据源
* @param slaveDataSource 从节点数据源
* @param otherSlaveDataSources 其他从节点数据源
* @return 读写分离数据源
*/
public static DataSource createDataSource(final String logicDataSourceName, final DataSource masterDataSource, final DataSource slaveDataSource, final DataSource... otherSlaveDataSources) {
return new MasterSlaveDataSource(logicDataSourceName, masterDataSource, Lists.asList(slaveDataSource, otherSlaveDataSources));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

/**
* 分片数据源工厂.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingDataSourceFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,15 @@
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -54,9 +50,6 @@ public final class ShardingRule {

private final TableShardingStrategy tableShardingStrategy;

@Getter(AccessLevel.NONE)
private final Map<String, MasterSlaveRule> masterSlaveRuleMap;

/**
* 全属性构造器.
*
Expand All @@ -69,7 +62,7 @@ public final class ShardingRule {
@Deprecated
public ShardingRule(
final DataSourceRule dataSourceRule, final Collection<TableRule> tableRules, final Collection<BindingTableRule> bindingTableRules,
final DatabaseShardingStrategy databaseShardingStrategy, final TableShardingStrategy tableShardingStrategy, final Collection<MasterSlaveRule> masterSlaveRules) {
final DatabaseShardingStrategy databaseShardingStrategy, final TableShardingStrategy tableShardingStrategy) {
Preconditions.checkNotNull(dataSourceRule);
Preconditions.checkNotNull(tableRules);
this.dataSourceRule = dataSourceRule;
Expand All @@ -79,13 +72,6 @@ public ShardingRule(
Collections.<String>emptyList(), new NoneDatabaseShardingAlgorithm()) : databaseShardingStrategy;
this.tableShardingStrategy = null == tableShardingStrategy ? new TableShardingStrategy(
Collections.<String>emptyList(), new NoneTableShardingAlgorithm()) : tableShardingStrategy;
masterSlaveRuleMap = Maps.uniqueIndex(null == masterSlaveRules ? Collections.<MasterSlaveRule>emptyList() : masterSlaveRules, new Function<MasterSlaveRule, String>() {

@Override
public String apply(final MasterSlaveRule input) {
return input.getLogicDataSource();
}
});
}

/**
Expand Down Expand Up @@ -226,16 +212,6 @@ public Collection<String> getAllShardingColumns() {
return result;
}

/**
* 根据逻辑数据源名称获取读写分离配置.
*
* @param logicDataSource 逻辑数据源名称
* @return 读写分离配置
*/
public Optional<MasterSlaveRule> findMasterSlaveRule(final String logicDataSource) {
return Optional.fromNullable(masterSlaveRuleMap.get(logicDataSource));
}

/**
* 分片规则配置对象构建器.
*/
Expand All @@ -252,8 +228,6 @@ public static class ShardingRuleBuilder {

private TableShardingStrategy tableShardingStrategy;

private Collection<MasterSlaveRule> masterSlaveRules;

/**
* 构建数据源配置规则.
*
Expand Down Expand Up @@ -309,24 +283,13 @@ public ShardingRuleBuilder tableShardingStrategy(final TableShardingStrategy tab
return this;
}

/**
* 构建数据源分片规则.
*
* @param masterSlaveRules 读写分离策略集合
* @return 分片规则配置对象构建器
*/
public ShardingRuleBuilder masterSlaveRules(final Collection<MasterSlaveRule> masterSlaveRules) {
this.masterSlaveRules = masterSlaveRules;
return this;
}

/**
* 构建分片规则配置对象.
*
* @return 分片规则配置对象
*/
public ShardingRule build() {
return new ShardingRule(dataSourceRule, tableRules, bindingTableRules, databaseShardingStrategy, tableShardingStrategy, masterSlaveRules);
return new ShardingRule(dataSourceRule, tableRules, bindingTableRules, databaseShardingStrategy, tableShardingStrategy);
}
}
}
Loading

0 comments on commit 1303d55

Please sign in to comment.