forked from baihui212/tsharding
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
3,407 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,25 @@ | ||
# tsharding | ||
TSharding is the simple sharding component used in mogujie trade platform. | ||
![alt text](https://raw.githubusercontent.com/juxuechen/ShareKit/cut-for-baby/share.jpg) | ||
###TSharding is the simple sharding component used in mogujie trade platform. | ||
###分库分表业界方案 | ||
![alt text](https://github.com/baihui212/intro/raw/master/pics/tsharding-select.png) | ||
|
||
###分库分表TSharding | ||
#####TSharding组件目标 | ||
*很少的资源投入即可开发完成 | ||
*支持交易订单表的Sharding需求,分库又分表 | ||
*支持数据源路由 | ||
*支持事务 | ||
*支持结果集合并 | ||
*支持读写分离 | ||
|
||
#####TSharding Resources Abstract | ||
![alt text](https://github.com/baihui212/intro/raw/master/pics/tsharding-abstract.png) | ||
|
||
#####TSharding Resources Classes | ||
![alt text](https://github.com/baihui212/intro/raw/master/pics/tsharding-classes.png) | ||
|
||
#####TSharding组件接入过程: | ||
*引入TSharding JAR包 | ||
*配置所有分库的JDBC连接信息 | ||
*Mybatis Mapper方法参数增加ShardingOrderPara/ShardingBuyerPara/ShardingSellerPara注解 | ||
*批量查询增加结果集合并逻辑 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
交易分库分表组件TSharding | ||
|
||
1.0.0-SNAPSHOT | ||
|
||
##2015-09-08 | ||
##版本1.0.0-SNAPSHOT | ||
new features: | ||
com.mogujie.trade.tsharding.route.orm.MapperEnhancer#enhanceMapperClass //增加注解信息和注解内容,以支持mybatis mapper类的动态绑定 | ||
|
||
##2015-09-18 | ||
##版本1.0.0 | ||
已经支持交易订单分库分表完整需求,灰度完成100%发布上线。发布tsharding稳定版. | ||
|
||
##版本1.0.1.1 | ||
优化orm元数据增强扩展时的内存消耗 | ||
##版本1.0.2 | ||
增加XDItemOrderEx表sharding支持。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.mogujie.trade</groupId> | ||
<artifactId>tsharding</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
|
||
<artifactId>tsharding-client</artifactId> | ||
<version>1.0.2</version> | ||
<name>tsharding client</name> | ||
|
||
<properties> | ||
<mybatis.version>3.2.8</mybatis.version> | ||
<mybatis-spring.version>1.2.2</mybatis-spring.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.mogujie.trade</groupId> | ||
<artifactId>trade.switch</artifactId> | ||
<version>1.0.2.1111switch</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis</artifactId> | ||
<version>${mybatis.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis-spring</artifactId> | ||
<version>${mybatis-spring.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax.persistence</groupId> | ||
<artifactId>persistence-api</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.javassist</groupId> | ||
<artifactId>javassist</artifactId> | ||
<version>3.19.0-GA</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.mogujie.tesla</groupId> | ||
<artifactId>tesla-support</artifactId> | ||
<version>${tesla.support.version}</version> | ||
<type>pom</type> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.mogujie.tesla</groupId> | ||
<artifactId>tesla-dal-db-orm</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.mogujie.tesla</groupId> | ||
<artifactId>tesla-dal-db</artifactId> | ||
<version>1.0.11-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.mogujie.trade</groupId> | ||
<artifactId>trade.switch</artifactId> | ||
<version>1.0.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.mogujie.service</groupId> | ||
<artifactId>trade.service.base</artifactId> | ||
<version>1.0.6</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
20 changes: 20 additions & 0 deletions
20
...-client/src/main/java/com/mogujie/trade/tsharding/annotation/ShardingExtensionMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mogujie.trade.tsharding.annotation; | ||
|
||
import com.mogujie.trade.tsharding.route.orm.MapperResourceEnhancer; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* 需要sharding扩展的dao层方法 | ||
* @auther qigong on 6/4/15 11:02 AM. | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ShardingExtensionMethod { | ||
Class<?> type() default MapperResourceEnhancer.class; | ||
|
||
String method() default "enhancedShardingSQL"; | ||
} |
21 changes: 21 additions & 0 deletions
21
...ent/src/main/java/com/mogujie/trade/tsharding/annotation/parameter/ShardingBuyerPara.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mogujie.trade.tsharding.annotation.parameter; | ||
|
||
/** | ||
* sharding参数注解 | ||
* @auther qigong on 5/28/15 1:00 PM. | ||
*/ | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
/** | ||
* sharding计算用的Para buyerUserId | ||
*/ | ||
public @interface ShardingBuyerPara { | ||
String value() default "buyerUserId"; | ||
} |
21 changes: 21 additions & 0 deletions
21
...ent/src/main/java/com/mogujie/trade/tsharding/annotation/parameter/ShardingOrderPara.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mogujie.trade.tsharding.annotation.parameter; | ||
|
||
/** | ||
* sharding参数注解 | ||
* @auther qigong on 5/28/15 1:00 PM. | ||
*/ | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
/** | ||
* sharding计算用的Para orderId或parentOrderId或itemOrderId或shopOrderId或payOrderId | ||
*/ | ||
public @interface ShardingOrderPara { | ||
String value() default "orderId"; | ||
} |
21 changes: 21 additions & 0 deletions
21
...nt/src/main/java/com/mogujie/trade/tsharding/annotation/parameter/ShardingSellerPara.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.mogujie.trade.tsharding.annotation.parameter; | ||
|
||
/** | ||
* sharding参数注解 | ||
* @auther qigong on 5/28/15 1:00 PM. | ||
*/ | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
/** | ||
* sharding计算用的Para sellerUserId | ||
*/ | ||
public @interface ShardingSellerPara { | ||
String value() default "sellerUserId"; | ||
} |
119 changes: 119 additions & 0 deletions
119
tsharding-client/src/main/java/com/mogujie/trade/tsharding/client/ShardingCaculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.mogujie.trade.tsharding.client; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* 分片计算器 | ||
* | ||
* @auther qigong on 5/28/15 1:06 PM. | ||
*/ | ||
public class ShardingCaculator { | ||
|
||
/** | ||
* 根据分片参数值计算分表名 | ||
* | ||
* @param shardingPara | ||
* @return 分表名0xxx | ||
*/ | ||
public static String caculateTableName(Long shardingPara) { | ||
if (shardingPara >= 0) { | ||
return "TestTable" + getNumberWithZeroSuffix((shardingPara % 10000) % 512); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 根据分片参数值计算分表名 | ||
* | ||
* @param shardingPara | ||
* @return 分表名0xxx | ||
*/ | ||
public static Integer caculateTableIndex(Long shardingPara) { | ||
if (shardingPara >= 0) { | ||
return new Long(shardingPara % 10000 % 512).intValue(); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* 根据分片参数值计算分库名(逻辑库) | ||
* | ||
* @param shardingPara | ||
* @return 分库名000x | ||
*/ | ||
public static String caculateSchemaName(String fieldName, Long shardingPara) { | ||
if (shardingPara >= 0) { | ||
|
||
if ("sellerUserId".equals(fieldName)) { | ||
return "sellertestschema" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64); | ||
} else { | ||
return "testschema" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 根据分片参数值计算数据源名 | ||
* | ||
* @param shardingPara | ||
* @return DatasourceName 见数据源配置文件 | ||
*/ | ||
public static String caculateDatasourceName(String fieldName, Long shardingPara) { | ||
if (shardingPara >= 0) { | ||
if ("sellerUserId".equals(fieldName)) { | ||
return "seller_ds_" + ((shardingPara % 10000) % 512) / 256; | ||
} else { | ||
return "buyer_ds_" + ((shardingPara % 10000) % 512) / 256; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public static String getNumberWithZeroSuffix(long number) { | ||
if (number >= 100) { | ||
return "0" + number; | ||
} else if (number >= 10) { | ||
return "00" + number; | ||
} else if (number >= 0) { | ||
return "000" + number; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 按订单号批量查询:跨表查,先按分表做分组 | ||
* | ||
* @param listShopOrderIds | ||
* @return tableNo -> orderIds | ||
*/ | ||
public static Map<Integer, List<Long>> getTableNoAndOrderIdsMap(List<Long> listShopOrderIds) { | ||
|
||
HashMap<Integer, List<Long>> shopOrderIdsMap = new HashMap(); | ||
if (listShopOrderIds == null || listShopOrderIds.size() == 0) { | ||
return shopOrderIdsMap; | ||
} | ||
for (Long shopOrderId : listShopOrderIds) { | ||
Integer tableNo = ShardingCaculator.caculateTableIndex(shopOrderId); | ||
List<Long> orderIds = shopOrderIdsMap.get(tableNo); | ||
if (orderIds == null) { | ||
orderIds = new ArrayList<>(); | ||
} | ||
orderIds.add(shopOrderId); | ||
shopOrderIdsMap.put(tableNo, orderIds); | ||
} | ||
return shopOrderIdsMap; | ||
} | ||
|
||
public static void main(String args[]) { | ||
System.out.println(caculateTableName(6000004386417L)); | ||
System.out.println(caculateSchemaName("buyerUserId", 6000004386417L)); | ||
|
||
System.out.println(caculateTableName(35586213L)); | ||
System.out.println(caculateSchemaName("sellerUserId", 35586213L)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...rding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
ackage com.mogujie.trade.tsharding.route; | ||
|
||
import com.mogujie.tesla.db.DataSourceRoutingHandler; | ||
import com.mogujie.trade.tsharding.annotation.parameter.*; | ||
import com.mogujie.trade.tsharding.client.ShardingCaculator; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* @author qigong 06/05/2015 | ||
*/ | ||
public class TShardingRoutingHandler implements DataSourceRoutingHandler { | ||
|
||
@Override | ||
public String dynamicRoute(Method method, Object[] args) { | ||
//route逻辑见TShardingRoutingInvokeFactory | ||
return "testschema"; | ||
} | ||
|
||
@Override | ||
public Collection<String> values() { | ||
//暂未使用 | ||
return null; | ||
} | ||
} |
Oops, something went wrong.