Skip to content

Commit

Permalink
Merge pull request baihui212#8 from threezhang/jiuru
Browse files Browse the repository at this point in the history
@jiuru thanks for refine readme
  • Loading branch information
baihui212 authored Jul 14, 2016
2 parents 35c5406 + 274af60 commit fec8a61
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 141 deletions.
41 changes: 36 additions & 5 deletions tsharding-client/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
交易分库分表组件TSharding
## 交易分库分表组件TSharding


### 关键类
* 1.测试用例入口 com.mogujie.service.tsharding.test#TShardingTest

####
1.测试用例入口 com.mogujie.service.tsharding.test#TShardingTest
2.默认走Master库的前缀命名 com.mogujie.trade.tsharding.route.orm.base.ReadWriteSplittingContextInitializer.DEFAULT_WRITE_METHOD_NAMES
3.SQL增强 com.mogujie.trade.tsharding.route.orm.MapperResourceEnhancer.enhancedShardingSQL
* 2.默认走Master库的前缀命名 com.mogujie.trade.tsharding.route.orm.base.ReadWriteSplittingContextInitializer.DEFAULT_WRITE_METHOD_NAMES

* 3.SQL增强 com.mogujie.trade.tsharding.route.orm.MapperResourceEnhancer.enhancedShardingSQL


### 测试用例

跑测试用例之前先建库建表结构;
理论上是8个库,512张表,每个库64张表.

如果仅仅是跑测试用例,执行下面的sql就可以跑通:

create database trade0000;
create database trade0001;
create database trade0002;
create database trade0003;
create database trade0004;
create database trade0005;
create database trade0006;
create database trade0007;
create database trade;
use trade0001;

CREATE TABLE `TradeOrder0064` (
`orderId` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单ID',
`buyerUserId` bigint(20) unsigned NOT NULL COMMENT '买家的userId',
`sellerUserId` bigint(20) unsigned NOT NULL COMMENT '卖家的userId',
`shipTime` int(11) unsigned DEFAULT '0' COMMENT '发货时间',
PRIMARY KEY (`orderId`)
) ENGINE=InnoDB AUTO_INCREMENT=10000 DEFAULT CHARSET=utf8mb4 COMMENT='订单信息表';

INSERT INTO `TradeOrder0064` (`orderId`, `buyerUserId`, `sellerUserId`, `shipTime`)
VALUES
(50000280834672, 1234567, 2345678, 12345678);
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ShardingCaculator {
*/
public static String caculateTableName(Long shardingPara) {
if (shardingPara >= 0) {
return "XDOrder" + getNumberWithZeroSuffix((shardingPara % 10000) % 512);
return "TradeOrder" + getNumberWithZeroSuffix((shardingPara % 10000) % 512);
}
return null;
}
Expand Down Expand Up @@ -49,9 +49,9 @@ public static String caculateSchemaName(String fieldName, Long shardingPara) {
if (shardingPara >= 0) {

if ("sellerUserId".equals(fieldName)) {
return "sellerxdtrade" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64);
return "sellertrade" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64);
} else {
return "xdtrade" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64);
return "trade" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public SqlSource enhancedShardingSQL(MappedStatement ms, Configuration configura
if (node instanceof StaticTextSqlNode) {
StaticTextSqlNode textSqlNode = (StaticTextSqlNode) node;
String text = (String) textField.get(textSqlNode);
if(!text.contains("XDOrder")){
if(!text.contains("TradeOrder")){
newSqlNodesList.add(node);
}else {
newSqlNodesList.add(new StaticTextSqlNode(replaceWithShardingTableName(text, tableName, shardingPara)));
Expand Down Expand Up @@ -88,7 +88,7 @@ public SqlSource enhancedShardingSQL(MappedStatement ms, Configuration configura
//sql处理
String sql = (String) sqlField.get(staticSqlSource);

if(!sql.contains("XDOrder")){
if(!sql.contains("TradeOrder")){
result = sqlSource;
}else {
sql = replaceWithShardingTableName(sql, tableName, shardingPara);
Expand All @@ -111,9 +111,9 @@ public SqlSource enhancedShardingSQL(MappedStatement ms, Configuration configura


private String replaceWithShardingTableName(String text, String tableName, Long shardingPara){
if(text.contains(" XDOrderPressureTest")){
return text.replace(" XDOrderPressureTest", " XDOrderPressureTest" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara));
if(text.contains(" TradeOrderPressureTest")){
return text.replace(" TradeOrderPressureTest", " TradeOrderPressureTest" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara));
}
return text.replace(" XDOrder", " " + tableName);
return text.replace(" TradeOrder", " " + tableName);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

Expand Down
232 changes: 116 additions & 116 deletions tsharding-client/src/test/resources/jdbc.properties
Original file line number Diff line number Diff line change
@@ -1,134 +1,134 @@
# xdtrade0000 master
xdtrade0000.master.url=jdbc:mysql://10.13.4.23/xdtrade0000
xdtrade0000.master.username=test
xdtrade0000.master.password=test123
xdtrade0000.master.minPoolSize=0
xdtrade0000.master.maxPoolSize=10
xdtrade0000.master.initialPoolSize=0
# xdtrade0000 slave
xdtrade0000.slave.url=jdbc:mysql://10.13.4.23/xdtrade0000
xdtrade0000.slave.username=test
xdtrade0000.slave.password=test123
xdtrade0000.slave.minPoolSize=1
xdtrade0000.slave.maxPoolSize=24
xdtrade0000.slave.initialPoolSize=1
# trade0000 master
trade0000.master.url=jdbc:mysql://127.0.0.1/trade0000
trade0000.master.username=test
trade0000.master.password=test
trade0000.master.minPoolSize=0
trade0000.master.maxPoolSize=10
trade0000.master.initialPoolSize=0
# trade0000 slave
trade0000.slave.url=jdbc:mysql://127.0.0.1/trade0000
trade0000.slave.username=test
trade0000.slave.password=test
trade0000.slave.minPoolSize=1
trade0000.slave.maxPoolSize=24
trade0000.slave.initialPoolSize=1

# xdtrade0001 master
xdtrade0001.master.url=jdbc:mysql://10.13.4.23/xdtrade0001
xdtrade0001.master.username=test
xdtrade0001.master.password=test123
xdtrade0001.master.minPoolSize=0
xdtrade0001.master.maxPoolSize=10
xdtrade0001.master.initialPoolSize=0
# xdtrade0001 slave
xdtrade0001.slave.url=jdbc:mysql://10.13.4.23/xdtrade0001
xdtrade0001.slave.username=test
xdtrade0001.slave.password=test123
xdtrade0001.slave.minPoolSize=1
xdtrade0001.slave.maxPoolSize=24
xdtrade0001.slave.initialPoolSize=1
# trade0001 master
trade0001.master.url=jdbc:mysql://127.0.0.1/trade0001
trade0001.master.username=test
trade0001.master.password=test
trade0001.master.minPoolSize=0
trade0001.master.maxPoolSize=10
trade0001.master.initialPoolSize=0
# trade0001 slave
trade0001.slave.url=jdbc:mysql://127.0.0.1/trade0001
trade0001.slave.username=test
trade0001.slave.password=test
trade0001.slave.minPoolSize=1
trade0001.slave.maxPoolSize=24
trade0001.slave.initialPoolSize=1

# xdtrade0002 master
xdtrade0002.master.url=jdbc:mysql://10.13.4.23/xdtrade0002
xdtrade0002.master.username=test
xdtrade0002.master.password=test123
xdtrade0002.master.minPoolSize=0
xdtrade0002.master.maxPoolSize=10
xdtrade0002.master.initialPoolSize=0
# xdtrade0002 slave
xdtrade0002.slave.url=jdbc:mysql://10.13.4.23/xdtrade0002
xdtrade0002.slave.username=test
xdtrade0002.slave.password=test123
xdtrade0002.slave.minPoolSize=1
xdtrade0002.slave.maxPoolSize=24
xdtrade0002.slave.initialPoolSize=1
# trade0002 master
trade0002.master.url=jdbc:mysql://127.0.0.1/trade0002
trade0002.master.username=test
trade0002.master.password=test
trade0002.master.minPoolSize=0
trade0002.master.maxPoolSize=10
trade0002.master.initialPoolSize=0
# trade0002 slave
trade0002.slave.url=jdbc:mysql://127.0.0.1/trade0002
trade0002.slave.username=test
trade0002.slave.password=test
trade0002.slave.minPoolSize=1
trade0002.slave.maxPoolSize=24
trade0002.slave.initialPoolSize=1

# xdtrade0003 master
xdtrade0003.master.url=jdbc:mysql://10.13.4.23/xdtrade0003
xdtrade0003.master.username=test
xdtrade0003.master.password=test123
xdtrade0003.master.minPoolSize=0
xdtrade0003.master.maxPoolSize=10
xdtrade0003.master.initialPoolSize=0
# xdtrade0003 slave
xdtrade0003.slave.url=jdbc:mysql://10.13.4.23/xdtrade0003
xdtrade0003.slave.username=test
xdtrade0003.slave.password=test123
xdtrade0003.slave.minPoolSize=1
xdtrade0003.slave.maxPoolSize=24
xdtrade0003.slave.initialPoolSize=1
# trade0003 master
trade0003.master.url=jdbc:mysql://127.0.0.1/trade0003
trade0003.master.username=test
trade0003.master.password=test
trade0003.master.minPoolSize=0
trade0003.master.maxPoolSize=10
trade0003.master.initialPoolSize=0
# trade0003 slave
trade0003.slave.url=jdbc:mysql://127.0.0.1/trade0003
trade0003.slave.username=test
trade0003.slave.password=test
trade0003.slave.minPoolSize=1
trade0003.slave.maxPoolSize=24
trade0003.slave.initialPoolSize=1

# xdtrade0004 master
xdtrade0004.master.url=jdbc:mysql://10.13.4.23/xdtrade0004
xdtrade0004.master.username=test
xdtrade0004.master.password=test123
xdtrade0004.master.minPoolSize=0
xdtrade0004.master.maxPoolSize=10
xdtrade0004.master.initialPoolSize=0
# xdtrade0004 slave
xdtrade0004.slave.url=jdbc:mysql://10.13.4.23/xdtrade0004
xdtrade0004.slave.username=test
xdtrade0004.slave.password=test123
xdtrade0004.slave.minPoolSize=1
xdtrade0004.slave.maxPoolSize=24
xdtrade0004.slave.initialPoolSize=1
# trade0004 master
trade0004.master.url=jdbc:mysql://127.0.0.1/trade0004
trade0004.master.username=test
trade0004.master.password=test
trade0004.master.minPoolSize=0
trade0004.master.maxPoolSize=10
trade0004.master.initialPoolSize=0
# trade0004 slave
trade0004.slave.url=jdbc:mysql://127.0.0.1/trade0004
trade0004.slave.username=test
trade0004.slave.password=test
trade0004.slave.minPoolSize=1
trade0004.slave.maxPoolSize=24
trade0004.slave.initialPoolSize=1

# xdtrade0005 master
xdtrade0005.master.url=jdbc:mysql://10.13.4.23/xdtrade0005
xdtrade0005.master.username=test
xdtrade0005.master.password=test123
xdtrade0005.master.minPoolSize=0
xdtrade0005.master.maxPoolSize=10
xdtrade0005.master.initialPoolSize=0
# xdtrade0005 slave
xdtrade0005.slave.url=jdbc:mysql://10.13.4.23/xdtrade0005
xdtrade0005.slave.username=test
xdtrade0005.slave.password=test123
xdtrade0005.slave.minPoolSize=1
xdtrade0005.slave.maxPoolSize=24
xdtrade0005.slave.initialPoolSize=1
# trade0005 master
trade0005.master.url=jdbc:mysql://127.0.0.1/trade0005
trade0005.master.username=test
trade0005.master.password=test
trade0005.master.minPoolSize=0
trade0005.master.maxPoolSize=10
trade0005.master.initialPoolSize=0
# trade0005 slave
trade0005.slave.url=jdbc:mysql://127.0.0.1/trade0005
trade0005.slave.username=test
trade0005.slave.password=test
trade0005.slave.minPoolSize=1
trade0005.slave.maxPoolSize=24
trade0005.slave.initialPoolSize=1

# xdtrade0006 master
xdtrade0006.master.url=jdbc:mysql://10.13.4.23/xdtrade0006
xdtrade0006.master.username=test
xdtrade0006.master.password=test123
xdtrade0006.master.minPoolSize=0
xdtrade0006.master.maxPoolSize=10
xdtrade0006.master.initialPoolSize=0
# xdtrade0006 slave
xdtrade0006.slave.url=jdbc:mysql://10.13.4.23/xdtrade0006
xdtrade0006.slave.username=test
xdtrade0006.slave.password=test123
xdtrade0006.slave.minPoolSize=1
xdtrade0006.slave.maxPoolSize=24
xdtrade0006.slave.initialPoolSize=1
# trade0006 master
trade0006.master.url=jdbc:mysql://127.0.0.1/trade0006
trade0006.master.username=test
trade0006.master.password=test
trade0006.master.minPoolSize=0
trade0006.master.maxPoolSize=10
trade0006.master.initialPoolSize=0
# trade0006 slave
trade0006.slave.url=jdbc:mysql://127.0.0.1/trade0006
trade0006.slave.username=test
trade0006.slave.password=test
trade0006.slave.minPoolSize=1
trade0006.slave.maxPoolSize=24
trade0006.slave.initialPoolSize=1

# xdtrade0007 master
xdtrade0007.master.url=jdbc:mysql://10.13.4.23/xdtrade0007
xdtrade0007.master.username=test
xdtrade0007.master.password=test123
xdtrade0007.master.minPoolSize=0
xdtrade0007.master.maxPoolSize=10
xdtrade0007.master.initialPoolSize=0
# xdtrade0007 slave
xdtrade0007.slave.url=jdbc:mysql://10.13.4.23/xdtrade0007
xdtrade0007.slave.username=test
xdtrade0007.slave.password=test123
xdtrade0007.slave.minPoolSize=1
xdtrade0007.slave.maxPoolSize=24
xdtrade0007.slave.initialPoolSize=1
# trade0007 master
trade0007.master.url=jdbc:mysql://127.0.0.1/trade0007
trade0007.master.username=test
trade0007.master.password=test
trade0007.master.minPoolSize=0
trade0007.master.maxPoolSize=10
trade0007.master.initialPoolSize=0
# trade0007 slave
trade0007.slave.url=jdbc:mysql://127.0.0.1/trade0007
trade0007.slave.username=test
trade0007.slave.password=test
trade0007.slave.minPoolSize=1
trade0007.slave.maxPoolSize=24
trade0007.slave.initialPoolSize=1

# xdtrade单库 master
xdtrade.master.url=jdbc:mysql://10.13.4.23/trade
xdtrade.master.url=jdbc:mysql://127.0.0.1/trade
xdtrade.master.username=test
xdtrade.master.password=test123
xdtrade.master.password=test
xdtrade.master.minPoolSize=0
xdtrade.master.maxPoolSize=10
xdtrade.master.initialPoolSize=0
# rootjie slave
xdtrade.slave.url=jdbc:mysql://10.13.4.23/trade
xdtrade.slave.url=jdbc:mysql://127.0.0.1/trade
xdtrade.slave.username=test
xdtrade.slave.password=test123
xdtrade.slave.password=test
xdtrade.slave.minPoolSize=1
xdtrade.slave.maxPoolSize=24
xdtrade.slave.initialPoolSize=1
Expand Down
Loading

0 comments on commit fec8a61

Please sign in to comment.