From a2748aee3489060aa040551c08719259d42826e2 Mon Sep 17 00:00:00 2001 From: jiuru Date: Thu, 14 Jul 2016 09:55:09 +0800 Subject: [PATCH 1/6] @jiuru --- tsharding-client/.gitignore | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tsharding-client/.gitignore diff --git a/tsharding-client/.gitignore b/tsharding-client/.gitignore new file mode 100644 index 0000000..26d8d9a --- /dev/null +++ b/tsharding-client/.gitignore @@ -0,0 +1,35 @@ +# maven ignore +target/ +*.jar +*.war +*.zip +*.tar +*.tar.gz + +# eclipse ignore +.metadata/ +.settings/ +.project +.classpath +.springBeans + +# idea ignore +.idea/ +*.ipr +*.iml +*.iws + +# temp ignore +logs/ +*.log +*.cache +*.diff +*.patch +*.tmp +*.swp + +# system ignore +.DS_Store +Thumbs.db + +/bin/ From b1ea2f32c44d6371fb170a4c55c4562925837cf9 Mon Sep 17 00:00:00 2001 From: jiuru Date: Thu, 14 Jul 2016 10:16:18 +0800 Subject: [PATCH 2/6] @jiuru --- tsharding-client/pom.xml | 55 ++++++++-------------------------------- 1 file changed, 11 insertions(+), 44 deletions(-) diff --git a/tsharding-client/pom.xml b/tsharding-client/pom.xml index 409e178..d1c17d8 100644 --- a/tsharding-client/pom.xml +++ b/tsharding-client/pom.xml @@ -1,31 +1,25 @@ 4.0.0 - - com.mogujie.trade - tsharding - 1.0.0 - + com.mogujie.trade tsharding-client - 1.0.2 + 1.0.0 tsharding client 3.2.8 1.2.2 + 4.0.6.RELEASE + 4.10 + 3.19.0-GA - - com.mogujie.trade - trade.switch - 1.0.2.1111switch - - org.springframework spring-jdbc + ${spring.version} @@ -49,59 +43,32 @@ org.springframework spring-test + ${spring.version} org.springframework spring-core + ${spring.version} org.springframework spring-context + ${spring.version} provided org.javassist javassist - 3.19.0-GA - - - - com.mogujie.tesla - tesla-support - ${tesla.support.version} - pom - - - com.mogujie.tesla - tesla-dal-db-orm - - - - - - com.mogujie.tesla - tesla-dal-db - 1.0.11-SNAPSHOT - - - - com.mogujie.trade - trade.switch - 1.0.2 - - - - com.mogujie.service - trade.service.base - 1.0.6 + ${javassist.version} junit junit + ${junit.version} From 5700f28fa6bb38090a2d5faaacc7648a16af56dc Mon Sep 17 00:00:00 2001 From: jiuru Date: Thu, 14 Jul 2016 12:38:37 +0800 Subject: [PATCH 3/6] @jiuru --- tsharding-client/pom.xml | 71 +++++ .../mogujie/trade/db/DataSourceConfig.java | 72 +++++ .../mogujie/trade/db/DataSourceFactory.java | 13 + .../mogujie/trade/db/DataSourceLookup.java | 37 +++ .../mogujie/trade/db/DataSourceRouting.java | 28 ++ .../trade/db/DataSourceRoutingException.java | 30 ++ .../trade/db/DataSourceRoutingHandler.java | 29 ++ .../mogujie/trade/db/DataSourceScanner.java | 284 ++++++++++++++++++ .../com/mogujie/trade/db/DataSourceType.java | 8 + .../trade/db/DruidDataSourceFactory.java | 49 +++ .../db/EmptyDataSourceRoutingHandler.java | 23 ++ .../mogujie/trade/db/ReadWriteSplitting.java | 15 + .../trade/db/ReadWriteSplittingAdvice.java | 96 ++++++ .../trade/db/ReadWriteSplittingContext.java | 26 ++ .../db/ReadWriteSplittingDataSource.java | 103 +++++++ .../trade/db/ReadWriteSplittingException.java | 30 ++ .../RoutingDataSourceTransactionContext.java | 23 ++ .../RoutingDataSourceTransactionManager.java | 51 ++++ .../tsharding/client/ShardingCaculator.java | 6 +- .../route/TShardingRoutingHandler.java | 8 +- ...ShardingRoutingHandlerForPressureTest.java | 2 +- .../route/orm/MapperResourceEnhancer.java | 14 +- .../route/orm/MapperScannerWithSharding.java | 4 +- .../route/orm/MapperShardingInitializer.java | 5 - .../ReadWriteSplittingContextInitializer.java | 6 +- .../base/TShardingRoutingInvokeFactory.java | 11 +- .../tesla/support/service-loader.xml | 2 +- .../service/tsharding/bean/BaseOrder.java | 36 +++ .../service/tsharding/bean/ShopOrder.java | 6 + .../{route/dal => dao}/ShopOrderDao.java | 6 +- .../{route/dal => dao}/ShopOrderDaoImpl.java | 16 +- .../dal => mapper}/ShopOrderMapper.java | 14 +- .../tsharding/route/ShopOrderDaoTest.java | 30 -- .../tsharding/route/ShopOrderMapperTest.java | 110 ------- .../tsharding/route/dal/BaseOrder.java | 8 - .../tsharding/route/dal/ShopOrder.java | 8 - .../service/tsharding/test/BaseTest.java | 16 + .../service/tsharding/test/TShardingTest.java | 47 +++ .../{ => test}/annotation/SimpleDao.java | 2 +- .../{ => test}/annotation/SimpleDaoImpl.java | 2 +- .../SimpleDaoWithAnotationTest.java | 13 +- .../client/ShardingCaculatorTest.java | 2 +- .../route/GenerateTest.java} | 4 +- .../{ => test}/route/InvokerTest.java | 2 +- .../route/SchemaPropertiesTest.java | 10 +- .../test/route/ShopOrderDaoTest.java | 45 +++ .../test/route/ShopOrderMapperTest.java | 56 ++++ .../{ => test}/route/TestJavaAssit.java | 2 +- .../resources/META-INF/support/datasource.xml | 41 +++ .../{tesla => }/support/service-loader.xml | 0 .../{tesla.properties => app.properties} | 4 +- .../src/test/resources/jdbc.properties | 251 ++++++++-------- .../{logback-test.xml => logback.xml} | 4 +- .../src/test/resources/spring-test.xml | 20 ++ .../resources/sqlmap/shoporder-mapper.xml | 37 +++ 55 files changed, 1474 insertions(+), 364 deletions(-) create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceConfig.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceFactory.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceLookup.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRouting.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingException.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingHandler.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceScanner.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceType.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/DruidDataSourceFactory.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/EmptyDataSourceRoutingHandler.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplitting.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingAdvice.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingContext.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingDataSource.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingException.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionContext.java create mode 100644 tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionManager.java create mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java create mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/ShopOrder.java rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{route/dal => dao}/ShopOrderDao.java (74%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{route/dal => dao}/ShopOrderDaoImpl.java (77%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{route/dal => mapper}/ShopOrderMapper.java (59%) delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderDaoTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderMapperTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/BaseOrder.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrder.java create mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/BaseTest.java create mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{ => test}/annotation/SimpleDao.java (83%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{ => test}/annotation/SimpleDaoImpl.java (86%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{ => test}/annotation/SimpleDaoWithAnotationTest.java (97%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{ => test}/client/ShardingCaculatorTest.java (98%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{route/Test.java => test/route/GenerateTest.java} (96%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{ => test}/route/InvokerTest.java (85%) rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{ => test}/route/SchemaPropertiesTest.java (68%) create mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java create mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java rename tsharding-client/src/test/java/com/mogujie/service/tsharding/{ => test}/route/TestJavaAssit.java (97%) create mode 100644 tsharding-client/src/test/resources/META-INF/support/datasource.xml rename tsharding-client/src/test/resources/META-INF/{tesla => }/support/service-loader.xml (100%) rename tsharding-client/src/test/resources/{tesla.properties => app.properties} (73%) rename tsharding-client/src/test/resources/{logback-test.xml => logback.xml} (83%) create mode 100644 tsharding-client/src/test/resources/spring-test.xml diff --git a/tsharding-client/pom.xml b/tsharding-client/pom.xml index d1c17d8..81d5bdb 100644 --- a/tsharding-client/pom.xml +++ b/tsharding-client/pom.xml @@ -13,6 +13,7 @@ 4.0.6.RELEASE 4.10 3.19.0-GA + 1.2.7 @@ -70,5 +71,75 @@ junit ${junit.version} + + + com.alibaba + fastjson + ${fastjson.version} + + + + org.slf4j + slf4j-api + 1.7.21 + + + com.alibaba + druid + 1.0.18 + + + + mysql + mysql-connector-java + 5.1.38 + + + + org.aspectj + aspectjweaver + 1.8.8 + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.17 + + false + + + + maven-compiler-plugin + + 1.7 + 1.7 + UTF-8 + + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + maven-source-plugin + 2.4 + + + package + + jar-no-fork + + + + + + diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceConfig.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceConfig.java new file mode 100644 index 0000000..fb66b7f --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceConfig.java @@ -0,0 +1,72 @@ +package com.mogujie.trade.db; + +/** + * @author by jiuru on 16/7/14. + */ +public class DataSourceConfig { + + static final int DEFAULT_MIN_POOL_SIZE = 0; + static final int DEFAULT_MAX_POOL_SIZE = 10; + static final int DEFAULT_INI_POOL_SIZE = 0; + + private String url; + + private String username; + + private String password; + + private int minPoolSize; + + private int maxPoolSize; + + private int initialPoolSize; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public int getMinPoolSize() { + return minPoolSize; + } + + public void setMinPoolSize(int minPoolSize) { + this.minPoolSize = minPoolSize; + } + + public int getMaxPoolSize() { + return maxPoolSize; + } + + public void setMaxPoolSize(int maxPoolSize) { + this.maxPoolSize = maxPoolSize; + } + + public int getInitialPoolSize() { + return initialPoolSize; + } + + public void setInitialPoolSize(int initialPoolSize) { + this.initialPoolSize = initialPoolSize; + } + +} \ No newline at end of file diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceFactory.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceFactory.java new file mode 100644 index 0000000..00a893b --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceFactory.java @@ -0,0 +1,13 @@ +package com.mogujie.trade.db; + +import javax.sql.DataSource; +import java.sql.SQLException; + +/** + * @author by jiuru on 16/7/14. + */ +public interface DataSourceFactory { + + T getDataSource(DataSourceConfig config) throws SQLException; + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceLookup.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceLookup.java new file mode 100644 index 0000000..d0212a1 --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceLookup.java @@ -0,0 +1,37 @@ +package com.mogujie.trade.db; + +import java.io.Closeable; +import java.io.IOException; +import java.util.Collections; +import java.util.Map; + +/** + * @author by jiuru on 16/7/14. + */ +public class DataSourceLookup implements Closeable { + + private final Map dataSources; + + public DataSourceLookup(Map dataSources) { + this.dataSources = Collections.unmodifiableMap(dataSources); + } + + /** + * @param name + * @return + */ + public ReadWriteSplittingDataSource get(String name) { + return this.dataSources.get(name); + } + + public Map getMapping() { + return this.dataSources; + } + + @Override + public void close() throws IOException { + for (ReadWriteSplittingDataSource dataSource : dataSources.values()) { + dataSource.close(); + } + } +} \ No newline at end of file diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRouting.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRouting.java new file mode 100644 index 0000000..73025b4 --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRouting.java @@ -0,0 +1,28 @@ +package com.mogujie.trade.db; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author by jiuru on 16/7/14. + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface DataSourceRouting { + /** + * 静态绑定该Mapper对应的数据源 + * + * @return 绑定的数据源的名称 + */ + String value() default ""; + + /** + * 指定运行时路由处理的Handler,动态指定对应的数据源。注意:此方式不能支持事务! + * + * @return + */ + Class handler() default EmptyDataSourceRoutingHandler.class; + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingException.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingException.java new file mode 100644 index 0000000..33895fb --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingException.java @@ -0,0 +1,30 @@ +package com.mogujie.trade.db; + +/** + * @author by jiuru on 16/7/14. + */ +public class DataSourceRoutingException extends RuntimeException { + + private static final long serialVersionUID = 8678631953659495594L; + + public DataSourceRoutingException() { + } + + public DataSourceRoutingException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public DataSourceRoutingException(String message, Throwable cause) { + super(message, cause); + } + + public DataSourceRoutingException(String message) { + super(message); + } + + public DataSourceRoutingException(Throwable cause) { + super(cause); + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingHandler.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingHandler.java new file mode 100644 index 0000000..e6ff461 --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceRoutingHandler.java @@ -0,0 +1,29 @@ +package com.mogujie.trade.db; + +import java.lang.reflect.Method; +import java.util.Collection; + +/** + * @author by jiuru on 16/7/14. + */ +public interface DataSourceRoutingHandler { + + /** + * 根据方法及参数动态路由 + * + * @param method + * 方法 + * @param args + * 参数列表 + * @return 数据源的名称 + */ + String dynamicRoute(Method method, Object[] args); + + /** + * 返回所有可能数据库名称。注:此方法可能在将来版本中废弃,无须做此指定。 + * + * @return 所有可能返回的数据库名称 + */ + Collection values(); + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceScanner.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceScanner.java new file mode 100644 index 0000000..a77e86a --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceScanner.java @@ -0,0 +1,284 @@ +package com.mogujie.trade.db; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.BeanInitializationException; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.ConstructorArgumentValues; +import org.springframework.beans.factory.support.AutowireCandidateQualifier; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; +import org.springframework.beans.factory.support.GenericBeanDefinition; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionException; +import org.springframework.transaction.TransactionStatus; +import org.springframework.util.Assert; + +import javax.sql.DataSource; +import java.io.IOException; +import java.io.InputStream; +import java.sql.SQLException; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * @author by jiuru on 16/7/14. + */ +public class DataSourceScanner implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware { + + private static final String PROPERTY_FILE_NAME = "jdbc.properties"; + + private static final String KEY_SEPARATOR = "."; + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private DataSourceFactory dataSourceFactory; + + private ApplicationContext applicationContext; + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + // do nothing + } + + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { + + final Map dataSources = new HashMap<>(); + + InputStream in = this.getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE_NAME); + if (in != null) { + Properties properties = new Properties(); + try { + properties.load(in); + } catch (IOException e) { + throw new BeanInitializationException("read property file error!", e); + } + try { + Map> dataSourcesMapping = this.getDataSources(properties); + this.registerDataSources(registry, dataSourcesMapping); + int transcactionManagerCount = 0; + String transactionManagerBeanName = null; + for (Map.Entry> entry : dataSourcesMapping.entrySet()) { + final String name = entry.getKey(); + + final DataSource masterDataSource = entry.getValue().get(DataSourceType.master); + + final ReadWriteSplittingDataSource readWriteSplittingDataSource = new ReadWriteSplittingDataSource( + entry.getKey(), entry.getValue().get(DataSourceType.master), entry.getValue().get( + DataSourceType.slave)); + logger.info("init dataSource {}", readWriteSplittingDataSource); + dataSources.put(name, readWriteSplittingDataSource); + + // 若无可写的数据源则跳过创建事务管理器 + if (masterDataSource == null) { + continue; + } + + transactionManagerBeanName = name + "TransactionManager"; + + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); + beanDefinition.setBeanClass(RoutingDataSourceTransactionManager.class); + + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.add("dataSource", readWriteSplittingDataSource); + propertyValues.add("name", name); + beanDefinition.setPropertyValues(propertyValues); + AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(Qualifier.class); + qualifier.setAttribute(AutowireCandidateQualifier.VALUE_KEY, name); + beanDefinition.addQualifier(qualifier); + registry.registerBeanDefinition(transactionManagerBeanName, beanDefinition); + + PlatformTransactionManager transactionManager = this.applicationContext.getBean( + transactionManagerBeanName, PlatformTransactionManager.class); + Assert.notNull(transactionManager, "register BeanDefinition of " + transactionManagerBeanName + + " error!"); + transcactionManagerCount++; + } + + // 兼容只有一个或无TransactionManager的情况 + if (transcactionManagerCount == 1) {// 若只有一个则添加别名,兼容默认情况 + registry.registerAlias(transactionManagerBeanName, "transcationManager"); + } else if (transcactionManagerCount == 0) { + // add an empty transcationManager + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); + beanDefinition.setBeanClass(EmptyTransactionManager.class); + + registry.registerBeanDefinition("transactionManager", beanDefinition); + } + } catch (SQLException e) { + throw new BeanCreationException("initial dataSources error!", e); + } + } + + // register dataSourceLookup + GenericBeanDefinition dataSourceLookupBeanDefinition = new GenericBeanDefinition(); + dataSourceLookupBeanDefinition.setBeanClass(DataSourceLookup.class); + ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues(); + constructorArgumentValues.addIndexedArgumentValue(0, dataSources); + dataSourceLookupBeanDefinition.setConstructorArgumentValues(constructorArgumentValues); + registry.registerBeanDefinition("dataSourceLookup", dataSourceLookupBeanDefinition); + } + + /** + * 根据Properties配置解析得到数据源 + * + * @param properties + * @return + * @throws SQLException + */ + private Map> getDataSources(Properties properties) throws SQLException { + Map> dataSourcesMapping = new HashMap<>(2); + for (Map.Entry entry : properties.entrySet()) { + String[] parts = entry.getKey().toString().trim().split("\\" + KEY_SEPARATOR); + if (parts.length == 3) { + String name = parts[0]; + if (!dataSourcesMapping.containsKey(name)) { + for (DataSourceType dataSourceType : DataSourceType.values()) { + + DataSource ds = this.dataSourceFactory.getDataSource(this.parseDataSourceConfig(name, + dataSourceType, properties)); + Map map = dataSourcesMapping.get(name); + if (map == null) { + map = new EnumMap(DataSourceType.class); + dataSourcesMapping.put(name, map); + } + DataSource preValue = map.put(dataSourceType, ds); + if (preValue != null) { + throw new IllegalArgumentException("dupilicated DataSource of" + name + " " + + dataSourceType); + } + } + + } + } else { + // It's illegal, ignore. + } + } + return dataSourcesMapping; + } + + private DataSourceConfig parseDataSourceConfig(String name, DataSourceType dataSourceType, Properties properties) { + String keyPrefix = name + KEY_SEPARATOR + dataSourceType + KEY_SEPARATOR; + + DataSourceConfig dataSourceConfig = new DataSourceConfig(); + String url = properties.getProperty(keyPrefix + "url"); + Assert.hasText(url, keyPrefix + "url is empty!"); + dataSourceConfig.setUrl(url); + + String username = properties.getProperty(keyPrefix + "username"); + Assert.hasText(username, keyPrefix + "username is empty!"); + dataSourceConfig.setUsername(username); + + String password = properties.getProperty(keyPrefix + "password"); + Assert.hasText(password, keyPrefix + "password is empty!"); + dataSourceConfig.setPassword(password); + + String initialPoolSizeStr = properties.getProperty(keyPrefix + "initialPoolSize"); + int initialPoolSize = initialPoolSizeStr == null ? DataSourceConfig.DEFAULT_INI_POOL_SIZE : Integer + .parseInt(initialPoolSizeStr); + dataSourceConfig.setInitialPoolSize(initialPoolSize); + + String minPoolSizeStr = properties.getProperty(keyPrefix + "minPoolSize"); + int minPoolSize = minPoolSizeStr == null ? DataSourceConfig.DEFAULT_MIN_POOL_SIZE : Integer + .parseInt(minPoolSizeStr); + dataSourceConfig.setMinPoolSize(minPoolSize); + + String maxPoolSizeStr = properties.getProperty(keyPrefix + "maxPoolSize"); + int maxPoolSize = maxPoolSizeStr == null ? DataSourceConfig.DEFAULT_MAX_POOL_SIZE : Integer + .parseInt(maxPoolSizeStr); + dataSourceConfig.setMaxPoolSize(maxPoolSize); + + return dataSourceConfig; + } + + /** + * 将数据源注入到Spring中 + * + * @param registry + * @param dataSourcesMapping + */ + private void registerDataSources(BeanDefinitionRegistry registry, + Map> dataSourcesMapping) { + + for (Map.Entry> entry : dataSourcesMapping.entrySet()) { + final String name = entry.getKey(); + for (Map.Entry subEntry : entry.getValue().entrySet()) { + GenericBeanDefinition dataSourceBeanDefinition = new GenericBeanDefinition(); + dataSourceBeanDefinition.setBeanClass(DataSourceFactoryBean.class); + ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues(); + constructorArgumentValues.addIndexedArgumentValue(0, subEntry.getValue()); + dataSourceBeanDefinition.setConstructorArgumentValues(constructorArgumentValues); + String beanName = name + Character.toUpperCase(subEntry.getKey().name().charAt(0)) + + subEntry.getKey().name().substring(1) + "DataSource"; + registry.registerBeanDefinition(beanName, dataSourceBeanDefinition); + } + } + } + + // --------------------Setters--------------- + + public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { + this.dataSourceFactory = dataSourceFactory; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + public static class EmptyTransactionManager implements PlatformTransactionManager { + + @Override + public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { + throw new UnsupportedOperationException(); + } + + @Override + public void commit(TransactionStatus status) throws TransactionException { + throw new UnsupportedOperationException(); + } + + @Override + public void rollback(TransactionStatus status) throws TransactionException { + throw new UnsupportedOperationException(); + } + + } + + public static class DataSourceFactoryBean implements FactoryBean { + + private final DataSource dataSource; + + public DataSourceFactoryBean(DataSource dataSource) { + Assert.notNull(dataSource); + this.dataSource = dataSource; + } + + @Override + public DataSource getObject() throws Exception { + return this.dataSource; + } + + @Override + public Class getObjectType() { + return this.dataSource.getClass(); + } + + @Override + public boolean isSingleton() { + return true; + } + + } +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceType.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceType.java new file mode 100644 index 0000000..30d1dff --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DataSourceType.java @@ -0,0 +1,8 @@ +package com.mogujie.trade.db; + +/** + * @author by jiuru on 16/7/14. + */ +public enum DataSourceType { + master, slave +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/DruidDataSourceFactory.java b/tsharding-client/src/main/java/com/mogujie/trade/db/DruidDataSourceFactory.java new file mode 100644 index 0000000..6b0e528 --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/DruidDataSourceFactory.java @@ -0,0 +1,49 @@ +package com.mogujie.trade.db; + +import com.alibaba.druid.filter.Filter; +import com.alibaba.druid.pool.DruidDataSource; + +import java.sql.SQLException; +import java.util.Collections; +import java.util.List; + +/** + * @author by jiuru on 16/7/14. + */ +public class DruidDataSourceFactory implements DataSourceFactory { + + private List filters = Collections.emptyList(); + + @Override + public DruidDataSource getDataSource(DataSourceConfig config) throws SQLException { + DruidDataSource dataSource = new DruidDataSource(); + dataSource.setUrl(config.getUrl()); + dataSource.setUsername(config.getUsername()); + dataSource.setPassword(config.getPassword()); + // pool config + dataSource.setInitialSize(config.getInitialPoolSize()); + dataSource.setMinIdle(config.getMinPoolSize()); + dataSource.setMaxActive(config.getMaxPoolSize()); + + // common config + dataSource.setFilters("stat"); + dataSource.setMaxWait(1000); + dataSource.setValidationQuery("SELECT 'x'"); + dataSource.setTestWhileIdle(true); + dataSource.setTestOnBorrow(false); + dataSource.setTestOnReturn(false); + dataSource.setTimeBetweenEvictionRunsMillis(60000); + dataSource.setMinEvictableIdleTimeMillis(120000); + dataSource.setTimeBetweenLogStatsMillis(0); + + dataSource.setProxyFilters(filters); + + dataSource.init(); + return dataSource; + } + + public void setFilters(List filters) { + this.filters = filters; + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/EmptyDataSourceRoutingHandler.java b/tsharding-client/src/main/java/com/mogujie/trade/db/EmptyDataSourceRoutingHandler.java new file mode 100644 index 0000000..6269887 --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/EmptyDataSourceRoutingHandler.java @@ -0,0 +1,23 @@ +package com.mogujie.trade.db; + +import java.lang.reflect.Method; +import java.util.Collection; + +/** + * @author by jiuru on 16/7/14. + */ +public class EmptyDataSourceRoutingHandler implements DataSourceRoutingHandler { + + @Override + public String dynamicRoute(Method method, Object[] args) { + // this will never be called. + throw new InternalError(); + } + + @Override + public Collection values() { + // this will never be called. + throw new InternalError(); + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplitting.java b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplitting.java new file mode 100644 index 0000000..dc462ed --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplitting.java @@ -0,0 +1,15 @@ +package com.mogujie.trade.db; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author by jiuru on 16/7/14. + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ReadWriteSplitting { + DataSourceType value() default DataSourceType.master; +} \ No newline at end of file diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingAdvice.java b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingAdvice.java new file mode 100644 index 0000000..f33a32d --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingAdvice.java @@ -0,0 +1,96 @@ +package com.mogujie.trade.db; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.reflect.MethodSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.transaction.annotation.Transactional; + +import java.lang.reflect.Method; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author by jiuru on 16/7/14. + */ +public class ReadWriteSplittingAdvice { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final Map cache = new ConcurrentHashMap<>(); + + @Around("@annotation(com.mogujie.trade.db.ReadWriteSplitting)") + public Object intercept(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { + Signature signature = proceedingJoinPoint.getSignature(); + DataSourceType dataSourceType = null; + if (signature instanceof MethodSignature) { + // 若已经在事务中 则不做处理 + if (RoutingDataSourceTransactionContext.getCurTransactionDataSource() != null) { + return proceedingJoinPoint.proceed(); + } + + // 若已经设置为Master 则忽略 + if (ReadWriteSplittingContext.isMaster()) { + return proceedingJoinPoint.proceed(); + } + MethodSignature methodSignature = (MethodSignature) signature; + Method method = methodSignature.getMethod(); + dataSourceType = this.getDataSourceType(method); + } else { + // this may not happend. + throw new ReadWriteSplittingException("ReadWriteSplitting annotation should only used on method. "); + } + ReadWriteSplittingContext.set(dataSourceType); + logger.debug("{} {} using dataSourceOf {} ", proceedingJoinPoint.getTarget(), + proceedingJoinPoint.getSignature(), dataSourceType); + try { + return proceedingJoinPoint.proceed(); + } finally { + ReadWriteSplittingContext.clear(); + logger.debug("{} release dataSource of {}", proceedingJoinPoint.getTarget(), dataSourceType); + } + } + + /** + * 获取方法对应的数据源类型 + * + * @param method + * @return + */ + private DataSourceType getDataSourceType(Method method) { + DataSourceType dataSourceType = this.cache.get(method); + if (dataSourceType == null) { + synchronized (method) { + dataSourceType = this.cache.get(method); + if (dataSourceType == null) { + dataSourceType = this.determineDataSourceType(method); + this.cache.put(method, dataSourceType); + } + } + } + return dataSourceType; + } + + private DataSourceType determineDataSourceType(Method method) { + DataSourceType dataSourceType = DataSourceType.slave; + + ReadWriteSplitting readWriteSplitting = method.getAnnotation(ReadWriteSplitting.class); + if (readWriteSplitting != null) { + dataSourceType = readWriteSplitting.value(); + dataSourceType = dataSourceType == null ? DataSourceType.master : dataSourceType; + } else { + // this will not happen + throw new InternalError("method must have the annotation of ReadWriteSplitting! "); + } + + Transactional transcational = method.getAnnotation(Transactional.class); + if (transcational != null) { + throw new ReadWriteSplittingException("ReadWriteSplitting and Transactional can't be used on method " + + method + " at the same time!"); + } + return dataSourceType; + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingContext.java b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingContext.java new file mode 100644 index 0000000..1ba51f3 --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingContext.java @@ -0,0 +1,26 @@ +package com.mogujie.trade.db; + +/** + * @author by jiuru on 16/7/14. + */ +public class ReadWriteSplittingContext { + + private static final ThreadLocal curDataSourceType = new ThreadLocal(); + + public static void set(DataSourceType dataSourceType) { + curDataSourceType.set(dataSourceType); + } + + public static void setMaster() { + curDataSourceType.set(DataSourceType.master); + } + + public static void clear() { + curDataSourceType.remove(); + } + + public static boolean isMaster() { + return DataSourceType.master == curDataSourceType.get(); + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingDataSource.java b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingDataSource.java new file mode 100644 index 0000000..b7243da --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingDataSource.java @@ -0,0 +1,103 @@ +package com.mogujie.trade.db; + +import org.springframework.jdbc.datasource.AbstractDataSource; +import org.springframework.util.Assert; + +import javax.sql.DataSource; +import java.io.Closeable; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * @author by jiuru on 16/7/14. + */ +public class ReadWriteSplittingDataSource extends AbstractDataSource implements DataSource, Closeable { + + private final String name; + + private final DataSource masterDataSource; + + private final DataSource slaveDataSource; + + public ReadWriteSplittingDataSource(String name, DataSource masterDataSource, DataSource slaveDataSource) { + this.name = name; + this.masterDataSource = masterDataSource; + this.slaveDataSource = slaveDataSource; + Assert.isTrue(masterDataSource != slaveDataSource || masterDataSource != null, + "masterDataSource and slaveDataSource can't be both null!"); + } + + @Override + public Connection getConnection() throws SQLException { + return this.determineTargetDataSource().getConnection(); + } + + public DataSource getSlaveDataSource() { + return slaveDataSource; + } + + private DataSource determineTargetDataSource() { + if (slaveDataSource == null) { + return masterDataSource; + } + + if (this.isInTransaction()) { + return masterDataSource; + } + + return ReadWriteSplittingContext.isMaster() ? masterDataSource : slaveDataSource; + } + + private boolean isInTransaction() { + return RoutingDataSourceTransactionContext.getCurTransactionDataSource() != null; + } + + @Override + public Connection getConnection(String username, String password) throws SQLException { + return this.determineTargetDataSource().getConnection(username, password); + } + + public String getName() { + return this.name; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ReadWriteSplittingDataSource ["); + if (name != null) { + builder.append("name=").append(name).append(", "); + } + if (masterDataSource != null) { + builder.append("masterDataSource=").append(masterDataSource).append(", "); + } + if (slaveDataSource != null) { + builder.append("slaveDataSource=").append(slaveDataSource); + } + builder.append("]"); + return builder.toString(); + } + + @Override + public void close() throws IOException { + if (this.masterDataSource != null) { + if (masterDataSource instanceof AutoCloseable) { + try { + ((AutoCloseable) masterDataSource).close(); + } catch (Exception ignore) { + } + } + } + + if (this.slaveDataSource != null) { + if (slaveDataSource instanceof AutoCloseable) { + try { + ((AutoCloseable) slaveDataSource).close(); + } catch (Exception ignore) { + } + } + } + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingException.java b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingException.java new file mode 100644 index 0000000..aee28ad --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/ReadWriteSplittingException.java @@ -0,0 +1,30 @@ +package com.mogujie.trade.db; + +/** + * @author by jiuru on 16/7/14. + */ +public class ReadWriteSplittingException extends RuntimeException { + + private static final long serialVersionUID = 3118672534738018794L; + + public ReadWriteSplittingException() { + } + + public ReadWriteSplittingException(String message) { + super(message); + } + + public ReadWriteSplittingException(Throwable cause) { + super(cause); + } + + public ReadWriteSplittingException(String message, Throwable cause) { + super(message, cause); + } + + public ReadWriteSplittingException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionContext.java b/tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionContext.java new file mode 100644 index 0000000..5c524d6 --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionContext.java @@ -0,0 +1,23 @@ +package com.mogujie.trade.db; + +import javax.sql.DataSource; + +/** + * @author by jiuru on 16/7/14. + */ +public class RoutingDataSourceTransactionContext { + + private static final ThreadLocal curDataSource = new ThreadLocal(); + + public static DataSource getCurTransactionDataSource() { + return curDataSource.get(); + } + + public static void setCurDataSource(DataSource dataSource) { + curDataSource.set(dataSource); + } + + public static void clear() { + curDataSource.remove(); + } +} \ No newline at end of file diff --git a/tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionManager.java b/tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionManager.java new file mode 100644 index 0000000..c23fa7b --- /dev/null +++ b/tsharding-client/src/main/java/com/mogujie/trade/db/RoutingDataSourceTransactionManager.java @@ -0,0 +1,51 @@ +package com.mogujie.trade.db; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; + +import javax.sql.DataSource; + +/** + * @author by jiuru on 16/7/14. + */ +public class RoutingDataSourceTransactionManager extends DataSourceTransactionManager { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private String name; + /** + * + */ + private static final long serialVersionUID = 2532966497797909623L; + + public RoutingDataSourceTransactionManager() { + } + + public RoutingDataSourceTransactionManager(DataSource dataSource) { + super(dataSource); + } + + @Override + protected Object doGetTransaction() { + RoutingDataSourceTransactionContext.setCurDataSource(getDataSource()); + logger.debug("Transaction of {} begin", this.name); + return super.doGetTransaction(); + } + + @Override + protected void doCleanupAfterCompletion(Object transaction) { + super.doCleanupAfterCompletion(transaction); + RoutingDataSourceTransactionContext.clear(); + logger.debug("Transaction of {} ends", this.name); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/client/ShardingCaculator.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/client/ShardingCaculator.java index 49fcc4f..00683c2 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/client/ShardingCaculator.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/client/ShardingCaculator.java @@ -20,7 +20,7 @@ public class ShardingCaculator { */ public static String caculateTableName(Long shardingPara) { if (shardingPara >= 0) { - return "TestTable" + getNumberWithZeroSuffix((shardingPara % 10000) % 512); + return "XDOrder" + getNumberWithZeroSuffix((shardingPara % 10000) % 512); } return null; } @@ -49,9 +49,9 @@ public static String caculateSchemaName(String fieldName, Long shardingPara) { if (shardingPara >= 0) { if ("sellerUserId".equals(fieldName)) { - return "sellertestschema" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64); + return "sellerxdtrade" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64); } else { - return "testschema" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64); + return "xdtrade" + getNumberWithZeroSuffix(((shardingPara % 10000) % 512) / 64); } } return null; diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandler.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandler.java index 767aef0..a3b4e5e 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandler.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandler.java @@ -1,14 +1,10 @@ package 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 com.mogujie.trade.db.DataSourceRoutingHandler; + import java.lang.reflect.Method; -import java.util.Arrays; import java.util.Collection; -import java.util.List; /** diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandlerForPressureTest.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandlerForPressureTest.java index 77f2a38..e390271 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandlerForPressureTest.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/TShardingRoutingHandlerForPressureTest.java @@ -1,7 +1,7 @@ package com.mogujie.trade.tsharding.route; -import com.mogujie.tesla.db.DataSourceRoutingHandler; +import com.mogujie.trade.db.DataSourceRoutingHandler; import java.lang.reflect.Method; import java.util.Collection; diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperResourceEnhancer.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperResourceEnhancer.java index 49ba0e0..780f848 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperResourceEnhancer.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperResourceEnhancer.java @@ -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("TestTable") && !text.contains("TestTable1")){ + if(!text.contains("XDOrder")){ newSqlNodesList.add(node); }else { newSqlNodesList.add(new StaticTextSqlNode(replaceWithShardingTableName(text, tableName, shardingPara))); @@ -88,7 +88,7 @@ public SqlSource enhancedShardingSQL(MappedStatement ms, Configuration configura //sql处理 String sql = (String) sqlField.get(staticSqlSource); - if(!sql.contains("TestTable") && !sql.contains("TestTable1")){ + if(!sql.contains("XDOrder")){ result = sqlSource; }else { sql = replaceWithShardingTableName(sql, tableName, shardingPara); @@ -111,13 +111,9 @@ public SqlSource enhancedShardingSQL(MappedStatement ms, Configuration configura private String replaceWithShardingTableName(String text, String tableName, Long shardingPara){ - if(text.contains(" TestTablePressureTest")){ - return text.replace(" TestTablePressureTest", " TestTablePressureTest" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara)); - }else if(text.contains(" TestTable1PressureTest")){ - return text.replace(" TestTable1PressureTest", " TestTable1PressureTest" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara)); - }else if(text.contains(" TestTable1")){ - return text.replace(" TestTable1", " TestTable1" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara)); + if(text.contains(" XDOrderPressureTest")){ + return text.replace(" XDOrderPressureTest", " XDOrderPressureTest" + ShardingCaculator.getNumberWithZeroSuffix(shardingPara)); } - return text.replace(" TestTable", " " + tableName); + return text.replace(" XDOrder", " " + tableName); } } diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperScannerWithSharding.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperScannerWithSharding.java index 778f7bc..50a0a3e 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperScannerWithSharding.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperScannerWithSharding.java @@ -1,7 +1,7 @@ package com.mogujie.trade.tsharding.route.orm; -import com.mogujie.tesla.db.DataSourceLookup; -import com.mogujie.tesla.db.ReadWriteSplittingDataSource; +import com.mogujie.trade.db.DataSourceLookup; +import com.mogujie.trade.db.ReadWriteSplittingDataSource; import com.mogujie.trade.tsharding.route.orm.base.*; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.session.Configuration; diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperShardingInitializer.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperShardingInitializer.java index 11e8530..e68a17d 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperShardingInitializer.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/MapperShardingInitializer.java @@ -1,6 +1,5 @@ package com.mogujie.trade.tsharding.route.orm; -import com.mogujie.service.xiaodian.trade.base.utils.SentryUtil; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionTemplate; @@ -29,10 +28,6 @@ public class MapperShardingInitializer implements ApplicationContextAware { @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - - //初始化jvm监控信息 - SentryUtil.getTradeOrderInstance().openAutoCollector(); - Map sqlSessionFactories = applicationContext.getBeansOfType(SqlSessionFactory.class); if (sqlSessionFactories.isEmpty()) { return; diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/ReadWriteSplittingContextInitializer.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/ReadWriteSplittingContextInitializer.java index 5e8efca..7cff081 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/ReadWriteSplittingContextInitializer.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/ReadWriteSplittingContextInitializer.java @@ -1,8 +1,8 @@ package com.mogujie.trade.tsharding.route.orm.base; -import com.mogujie.tesla.db.DataSourceType; -import com.mogujie.tesla.db.ReadWriteSplitting; -import com.mogujie.tesla.db.ReadWriteSplittingContext; +import com.mogujie.trade.db.DataSourceType; +import com.mogujie.trade.db.ReadWriteSplitting; +import com.mogujie.trade.db.ReadWriteSplittingContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/TShardingRoutingInvokeFactory.java b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/TShardingRoutingInvokeFactory.java index b42ae7a..ef313ef 100644 --- a/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/TShardingRoutingInvokeFactory.java +++ b/tsharding-client/src/main/java/com/mogujie/trade/tsharding/route/orm/base/TShardingRoutingInvokeFactory.java @@ -1,18 +1,12 @@ package com.mogujie.trade.tsharding.route.orm.base; -import com.alibaba.druid.pool.DruidDataSource; -import com.alibaba.fastjson.JSON; -import com.mogujie.tesla.db.DataSourceLookup; -import com.mogujie.tesla.db.DataSourceRouting; -import com.mogujie.tesla.db.DataSourceRoutingException; -import com.mogujie.tesla.db.ReadWriteSplittingDataSource; -import com.mogujie.trade.moguswitch.util.MoguStableSwitch; +import com.mogujie.trade.db.DataSourceRouting; +import com.mogujie.trade.db.DataSourceRoutingException; import com.mogujie.trade.tsharding.annotation.parameter.ShardingBuyerPara; import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara; import com.mogujie.trade.tsharding.annotation.parameter.ShardingSellerPara; import com.mogujie.trade.tsharding.client.ShardingCaculator; import com.mogujie.trade.tsharding.route.TShardingRoutingHandler; -import com.mogujie.trade.tsharding.route.orm.MapperScannerWithSharding; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.mapper.MapperFactoryBean; import org.slf4j.Logger; @@ -22,7 +16,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.sql.SQLException; import java.util.List; public class TShardingRoutingInvokeFactory implements InvokerFactory> { diff --git a/tsharding-client/src/main/resources/tesla/support/service-loader.xml b/tsharding-client/src/main/resources/tesla/support/service-loader.xml index 976ba0b..2362d3b 100644 --- a/tsharding-client/src/main/resources/tesla/support/service-loader.xml +++ b/tsharding-client/src/main/resources/tesla/support/service-loader.xml @@ -22,5 +22,5 @@ - + \ No newline at end of file diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java new file mode 100644 index 0000000..1925972 --- /dev/null +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java @@ -0,0 +1,36 @@ +package com.mogujie.service.tsharding.bean; + + +public abstract class BaseOrder { + + private Long orderId; + + private Long buyerUserId; + + private Long sellerUserId; + + + public Long getOrderId() { + return orderId; + } + + public void setOrderId(Long orderId) { + this.orderId = orderId; + } + + public Long getBuyerUserId() { + return buyerUserId; + } + + public void setBuyerUserId(Long buyerUserId) { + this.buyerUserId = buyerUserId; + } + + public Long getSellerUserId() { + return sellerUserId; + } + + public void setSellerUserId(Long sellerUserId) { + this.sellerUserId = sellerUserId; + } +} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/ShopOrder.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/ShopOrder.java new file mode 100644 index 0000000..98c5b8c --- /dev/null +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/ShopOrder.java @@ -0,0 +1,6 @@ +package com.mogujie.service.tsharding.bean; + + +public class ShopOrder extends BaseOrder { + +} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderDao.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDao.java similarity index 74% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderDao.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDao.java index 9a0e066..6824272 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderDao.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDao.java @@ -1,14 +1,12 @@ -package com.mogujie.service.tsharding.route.dal; +package com.mogujie.service.tsharding.dao; -import com.mogujie.tesla.db.DataSourceRouting; -import com.mogujie.trade.tsharding.route.TShardingRoutingHandler; +import com.mogujie.service.tsharding.bean.ShopOrder; import java.util.List; /** * @auther qigong on 6/5/15 8:50 PM. */ -@DataSourceRouting(handler = TShardingRoutingHandler.class) public interface ShopOrderDao { /** diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderDaoImpl.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDaoImpl.java similarity index 77% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderDaoImpl.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDaoImpl.java index 8cbcdcf..c4cd092 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderDaoImpl.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDaoImpl.java @@ -1,13 +1,21 @@ -package com.mogujie.service.tsharding.route.dal; +package com.mogujie.service.tsharding.dao; +import com.mogujie.service.tsharding.bean.ShopOrder; +import com.mogujie.service.tsharding.mapper.ShopOrderMapper; import com.mogujie.trade.tsharding.client.ShardingCaculator; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * @auther qigong on 6/5/15 8:52 PM. */ +@Service("shopOrderDao") public class ShopOrderDaoImpl implements ShopOrderDao { @Autowired @@ -24,7 +32,7 @@ public List getShopOrderByShopOrderIdsOfOneBuyer(List listShopO setShopOrderIds.add(iShopOrderId); } } - return shopOrderMapper.getShopOrderByShopOrderIdsOfOneBuyer(new ArrayList(setShopOrderIds)); + return shopOrderMapper.getShopOrderByShopOrderIds(new ArrayList(setShopOrderIds)); } @@ -46,7 +54,7 @@ public List getShopOrderByShopOrderIdsOfMultiBuyer(List listSho List result = new ArrayList<>(); for (Integer tableIndex : shopOrderIdsMap.keySet()) { - result.addAll(shopOrderMapper.getShopOrderByShopOrderIdsOfOneBuyer(shopOrderIdsMap.get(tableIndex))); + result.addAll(shopOrderMapper.getShopOrderByShopOrderIds(shopOrderIdsMap.get(tableIndex))); } return result; } diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderMapper.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java similarity index 59% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderMapper.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java index e29e84f..62e8c30 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrderMapper.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java @@ -1,20 +1,20 @@ -package com.mogujie.service.tsharding.route.dal; +package com.mogujie.service.tsharding.mapper; -import com.mogujie.tesla.db.DataSourceRouting; +import com.mogujie.service.tsharding.bean.ShopOrder; +import com.mogujie.trade.db.DataSourceRouting; import com.mogujie.trade.tsharding.annotation.ShardingExtensionMethod; import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara; import com.mogujie.trade.tsharding.route.TShardingRoutingHandler; import com.mogujie.trade.tsharding.route.orm.MapperResourceEnhancer; -import org.apache.ibatis.annotations.Param; import java.util.List; -@DataSourceRouting(handler=TShardingRoutingHandler.class) -public interface ShopOrderMapper { +@DataSourceRouting(handler = TShardingRoutingHandler.class) +public interface ShopOrderMapper { @ShardingExtensionMethod(type = MapperResourceEnhancer.class, method = "enhancedShardingSQL") - public ShopOrder getShopOrderByShopOrderId(@ShardingOrderPara("orderId") Long shopOrderId); + public ShopOrder getShopOrderByShopOrderId(@ShardingOrderPara Long shopOrderId); @ShardingExtensionMethod(type = MapperResourceEnhancer.class, method = "enhancedShardingSQL") - public List getShopOrderByShopOrderIdsOfOneBuyer(@ShardingOrderPara("orderId") List xdShopOrderIds); + public List getShopOrderByShopOrderIds(@ShardingOrderPara List shopOrderIds); } \ No newline at end of file diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderDaoTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderDaoTest.java deleted file mode 100644 index cb4d363..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderDaoTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.mogujie.service.tsharding.route; - -import com.mogujie.service.tsharding.route.dal.ShopOrder; -import com.mogujie.service.tsharding.route.dal.ShopOrderDao; -import org.junit.Assert; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.ArrayList; -import java.util.List; - -/** - * @auther qigong on 6/5/15 8:55 PM. - */ -public class ShopOrderDaoTest { - - @Autowired - private ShopOrderDao shopOrderDao; - - @Test - public void testGetShopOrderByShopOrderIdsOfOneBuyer() { - - List listShopOrderIds = new ArrayList<>(); - listShopOrderIds.add(3968484880824L); - listShopOrderIds.add(3968484880824L); - - List shopOrders = shopOrderDao.getShopOrderByShopOrderIdsOfOneBuyer(listShopOrderIds); - Assert.assertTrue(shopOrders.size() == 2); - } -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderMapperTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderMapperTest.java deleted file mode 100644 index e83c004..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/ShopOrderMapperTest.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.mogujie.service.tsharding.route; - -import com.mogujie.service.tsharding.route.dal.ShopOrder; -import com.mogujie.service.tsharding.route.dal.ShopOrderMapper; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import java.util.Arrays; -import java.util.List; - -public class ShopOrderMapperTest { - - private final Logger logger = LoggerFactory.getLogger(getClass()); - - private ConfigurableApplicationContext applicationContext; - - @Autowired - private ShopOrderMapper shopOrderMapper; - - @Before - public void setUp() throws Exception { - this.applicationContext = new ClassPathXmlApplicationContext("META-INF/tesla/support/service-loader.xml", - "META-INF/tesla/support/datasource.xml"); - this.shopOrderMapper = applicationContext.getBean(ShopOrderMapper.class); - } - - @After - public void tearDown() throws Exception { - this.applicationContext.close(); - } - - - @Test - public void testXdOrderSharding() { - - ShopOrder shopOrder = null; - try { - shopOrder = shopOrderMapper.getShopOrderByShopOrderId(3950627820824L); - logger.debug("{}", shopOrder); - Assert.assertNotNull(shopOrder); - - shopOrder = shopOrderMapper.getShopOrderByShopOrderId(3950627820002L); - logger.debug("{}", shopOrder); - Assert.assertNull(shopOrder); - -// List shopOrders = shopOrderMapper.getShopOrderByShopOrderIdsOfOneBuyer(Arrays.asList(new Long[]{3950627820824L,3950631130824L})); -// logger.debug("{}", shopOrders); -// Assert.assertEquals(2, shopOrders.size()); - - -// Assert.assertEquals(new Integer(0), xdShopOrder.getVisible()); -// xdShopOrder.setVisible(1); -// xdShopOrderMapperPart1.updateShopOrderByShopOrderIdAndStatus(xdShopOrder.getOrderId(),xdShopOrder, xdShopOrder.getStatus()); -// xdShopOrder = xdShopOrderMapper.getShopOrderByShopOrderId(3950627820824L); -// logger.debug("after update visible:{}", xdShopOrders); -// Assert.assertEquals(new Integer(1), xdShopOrder.getVisible()); -// -// xdShopOrder.setVisible(0); -// xdShopOrderMapperPart1.updateShopOrderByShopOrderIdAndStatus(xdShopOrder.getOrderId(),xdShopOrder, xdShopOrder.getStatus()); -// xdShopOrder = xdShopOrderMapper.getShopOrderByShopOrderId(3950627820824L); -// Assert.assertEquals(new Integer(0), xdShopOrder.getVisible()); - -// xdShopOrders = xdShopOrderMapperPart1.getShopOrdersByPayOrderIdsOfOneBuyer(Arrays.asList(new Long[]{3950627810824L, 3950631120824L})); -// logger.debug("{}", xdShopOrders); -// Assert.assertEquals(2, xdShopOrders.size()); -// -// Long payOrderId = 3971291360824L; -// XdShopOrder record = new XdShopOrder(); -// record.setParentOrderId(payOrderId); -// record.setStatus(1 /*BusinessConstants.ORDER_STATUS_CANCELED*/); -// Long now = (System.currentTimeMillis() / 1000); -// record.setCancelTime(now); -// record.setCancelReason("ddd"); -// record.setExpiredTime(1L); -// record.setUpdated(1L); -// xdShopOrderMapper.batchUpdateShopOrderByShopOrderIdsAndStatusOfOneBuyer(Arrays.asList(new Long[]{3971291380824L, 3971291370824L}), record, record.getStatus()); -// xdShopOrder = xdShopOrderMapper.getShopOrderByShopOrderId(3971291380824L); -// logger.debug("{}", xdShopOrder); -// Assert.assertEquals(now, xdShopOrder.getCancelTime()); -// -// -// //updateShipExpenseByShopOrderId -// xdShopOrder.setOrderId(3981357789949L); -// xdShopOrder.setShipExpense(1200L); -// xdShopOrderMapper.updateShopOrderByShopOrderId(xdShopOrder.getOrderId(), xdShopOrder); -// xdShopOrder = xdShopOrderMapper.getShopOrderByShopOrderId(3981357789949L); -// logger.debug("{}", xdShopOrder); -// Assert.assertEquals(new Long(1200), xdShopOrder.getShipExpense()); -// -// -// //扫单表 -// xdShopOrders = xdShopOrderMapper.getOrdersByUpdatedTime(181,1430740105L, 1430833654L); -// logger.debug("{}", xdShopOrders); -// Assert.assertTrue(xdShopOrders.size() > 1); - - } catch (Exception e) { - logger.error("test error", e); - } - - } - - -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/BaseOrder.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/BaseOrder.java deleted file mode 100644 index 9565387..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/BaseOrder.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.mogujie.service.tsharding.route.dal; - -/** - * @author xueyan on 15/1/19. - */ -public abstract class BaseOrder { - -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrder.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrder.java deleted file mode 100644 index d3e4cbb..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/dal/ShopOrder.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.mogujie.service.tsharding.route.dal; - -/** - * @author xueyan on 15/1/19. - */ -public class ShopOrder extends BaseOrder { - -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/BaseTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/BaseTest.java new file mode 100644 index 0000000..766bed8 --- /dev/null +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/BaseTest.java @@ -0,0 +1,16 @@ +package com.mogujie.service.tsharding.test; + +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author by jiuru on 16/7/14. + */ + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({"classpath:spring-test.xml"}) +public abstract class BaseTest { + + +} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java new file mode 100644 index 0000000..bbfae9a --- /dev/null +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java @@ -0,0 +1,47 @@ +package com.mogujie.service.tsharding.test; + +import com.mogujie.service.tsharding.bean.ShopOrder; +import com.mogujie.service.tsharding.dao.ShopOrderDao; +import com.mogujie.service.tsharding.mapper.ShopOrderMapper; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author by jiuru on 16/7/14. + */ +public class TShardingTest extends BaseTest { + + private final Logger logger = LoggerFactory.getLogger(TShardingTest.class); + + @Autowired + private ShopOrderDao shopOrderDao; + + + @Autowired + private ShopOrderMapper shopOrderMapper; + + @Test + public void testGetShopOrderByShopOrderIdsOfOneBuyer() { + List orderIds = new ArrayList<>(); + orderIds.add(111L); + List orders = shopOrderDao.getShopOrderByShopOrderIdsOfOneBuyer(orderIds); + System.out.println(); + } + + + @Test + public void testGetShopOrderByShopOrderIds() { + List orderIds = new ArrayList<>(); + orderIds.add(50000280834672L); +// orderIds.add(70000204797232L); + List orders = shopOrderMapper.getShopOrderByShopOrderIds(orderIds); + System.out.println(); + } + + +} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDao.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDao.java similarity index 83% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDao.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDao.java index 5c14abe..751f79a 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDao.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDao.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.annotation; +package com.mogujie.service.tsharding.test.annotation; import com.mogujie.trade.tsharding.annotation.parameter.ShardingBuyerPara; import org.apache.ibatis.annotations.Param; diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDaoImpl.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoImpl.java similarity index 86% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDaoImpl.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoImpl.java index 10e2196..1935b24 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDaoImpl.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoImpl.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.annotation; +package com.mogujie.service.tsharding.test.annotation; import com.mogujie.trade.tsharding.annotation.parameter.ShardingBuyerPara; import org.apache.ibatis.annotations.Param; diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDaoWithAnotationTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoWithAnotationTest.java similarity index 97% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDaoWithAnotationTest.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoWithAnotationTest.java index 9eb9985..ce5e35e 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/annotation/SimpleDaoWithAnotationTest.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoWithAnotationTest.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.annotation; +package com.mogujie.service.tsharding.test.annotation; import com.alibaba.fastjson.JSON; import com.mogujie.trade.tsharding.annotation.parameter.ShardingBuyerPara; @@ -19,17 +19,12 @@ public class SimpleDaoWithAnotationTest { public static void main(String[] args) { Class z = SimpleDao.class; try { - InvocationHandler handler = new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("proxy method"); - - System.out.println(JSON.toJSONString(method.getParameterTypes())); - Annotation[][] an = method.getParameterAnnotations(); - if (an.length > 0) { for (int i = 0; i < an.length; i++) { System.out.print("i::" + i + ";"); @@ -46,23 +41,17 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl } } } - - return method.invoke(SimpleDaoImpl.class.newInstance(), new Object[]{"abc", "cde", 4191574207234L}); } }; Proxy proxy = (Proxy) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{SimpleDao.class}, handler); - Method m = z.getMethod("abc", new Class[]{String.class, String.class, Long.class}); - try { handler.invoke(proxy, m, new Object[]{"abc", "cde", 4191574207234L}); } catch (Throwable throwable) { throwable.printStackTrace(); } - - } catch (Exception e) { e.printStackTrace(); } diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/client/ShardingCaculatorTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/client/ShardingCaculatorTest.java similarity index 98% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/client/ShardingCaculatorTest.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/client/ShardingCaculatorTest.java index 394c19f..f81bc4e 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/client/ShardingCaculatorTest.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/client/ShardingCaculatorTest.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.client; +package com.mogujie.service.tsharding.test.client; import com.mogujie.trade.tsharding.client.ShardingCaculator; import org.junit.Assert; diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/Test.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/GenerateTest.java similarity index 96% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/route/Test.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/GenerateTest.java index 16a376e..8bab612 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/Test.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/GenerateTest.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.route; +package com.mogujie.service.tsharding.test.route; import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara; import com.mogujie.trade.tsharding.route.orm.MapperAnnotationEnhancer; @@ -13,7 +13,7 @@ /** * @author qigong on 15/9/5 下午12:14. */ -public class Test { +public class GenerateTest { public static void main(String[] args) { diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/InvokerTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/InvokerTest.java similarity index 85% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/route/InvokerTest.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/InvokerTest.java index 969f386..d2b106b 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/InvokerTest.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/InvokerTest.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.route; +package com.mogujie.service.tsharding.test.route; import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara; import com.mogujie.trade.tsharding.route.orm.base.Invocation; diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/SchemaPropertiesTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/SchemaPropertiesTest.java similarity index 68% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/route/SchemaPropertiesTest.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/SchemaPropertiesTest.java index 8295299..a40bbf3 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/SchemaPropertiesTest.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/SchemaPropertiesTest.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.route; +package com.mogujie.service.tsharding.test.route; import com.mogujie.trade.tsharding.client.ShardingCaculator; @@ -11,10 +11,10 @@ public static void main(String[] args) { String[] initConfs = new String[]{ "# testschemapressuretest master", - "testschemapressuretest.master.url=jdbc:mysql://10.11.4.110/testschemapressuretest", + "testschemapressuretest.master.url=jdbc:mysql://10.13.4.23/xdtrade", "testschemapressuretest.master.port=3306", - "testschemapressuretest.master.username=pigeontest", - "testschemapressuretest.master.password=3ff143dd2bbd399a3af6349f1b46f2e2", + "testschemapressuretest.master.username=test", + "testschemapressuretest.master.password=test123", "testschemapressuretest.master.minPoolSize=1", "testschemapressuretest.master.maxPoolSize=24", "testschemapressuretest.master.initialPoolSize=1" @@ -22,7 +22,7 @@ public static void main(String[] args) { for (long i = 0; i < 8; i++) { for (String conf : initConfs) { - conf = conf.replace("testschemapressuretest", "testschemapressuretest" + ShardingCaculator.getNumberWithZeroSuffix(i)); + conf = conf.replace("xdtrade", "xdtrade" + ShardingCaculator.getNumberWithZeroSuffix(i)); conf = conf.replace("sharding0", "sharding" + i/4); System.out.println(conf); } diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java new file mode 100644 index 0000000..d36136f --- /dev/null +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java @@ -0,0 +1,45 @@ +package com.mogujie.service.tsharding.test.route; + +import com.mogujie.service.tsharding.bean.ShopOrder; +import com.mogujie.service.tsharding.dao.ShopOrderDao; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.util.ArrayList; +import java.util.List; + +/** + * @auther qigong on 6/5/15 8:55 PM. + */ +public class ShopOrderDaoTest { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private ConfigurableApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + this.applicationContext = new ClassPathXmlApplicationContext("META-INF/support/service-loader.xml", + "META-INF/support/datasource.xml"); + this.shopOrderDao = applicationContext.getBean(ShopOrderDao.class); + } + + + private ShopOrderDao shopOrderDao; + + @Test + public void testGetShopOrderByShopOrderIdsOfOneBuyer() { + + List listShopOrderIds = new ArrayList<>(); + listShopOrderIds.add(3968484880824L); + listShopOrderIds.add(3968484880824L); + + List shopOrders = shopOrderDao.getShopOrderByShopOrderIdsOfOneBuyer(listShopOrderIds); + Assert.assertTrue(shopOrders.size() == 2); + } +} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java new file mode 100644 index 0000000..1c11541 --- /dev/null +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java @@ -0,0 +1,56 @@ +package com.mogujie.service.tsharding.test.route; + +import com.mogujie.service.tsharding.bean.ShopOrder; +import com.mogujie.service.tsharding.mapper.ShopOrderMapper; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class ShopOrderMapperTest { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private ConfigurableApplicationContext applicationContext; + + @Autowired + private ShopOrderMapper shopOrderMapper; + + @Before + public void setUp() throws Exception { + this.applicationContext = new ClassPathXmlApplicationContext("META-INF/support/service-loader.xml", + "META-INF/support/datasource.xml"); + this.shopOrderMapper = applicationContext.getBean(ShopOrderMapper.class); + } + + @After + public void tearDown() throws Exception { + this.applicationContext.close(); + } + + + @Test + public void testXdOrderSharding() { + + ShopOrder shopOrder = null; + try { + shopOrder = shopOrderMapper.getShopOrderByShopOrderId(3950627820824L); + logger.debug("{}", shopOrder); + Assert.assertNotNull(shopOrder); + + shopOrder = shopOrderMapper.getShopOrderByShopOrderId(3950627820002L); + logger.debug("{}", shopOrder); + Assert.assertNull(shopOrder); + } catch (Exception e) { + logger.error("test error: {}", e.getMessage(), e); + } + + } + + +} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/TestJavaAssit.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/TestJavaAssit.java similarity index 97% rename from tsharding-client/src/test/java/com/mogujie/service/tsharding/route/TestJavaAssit.java rename to tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/TestJavaAssit.java index f8b567a..df35a25 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/route/TestJavaAssit.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/TestJavaAssit.java @@ -1,4 +1,4 @@ -package com.mogujie.service.tsharding.route; +package com.mogujie.service.tsharding.test.route; import javassist.CannotCompileException; import javassist.ClassPool; diff --git a/tsharding-client/src/test/resources/META-INF/support/datasource.xml b/tsharding-client/src/test/resources/META-INF/support/datasource.xml new file mode 100644 index 0000000..11981a2 --- /dev/null +++ b/tsharding-client/src/test/resources/META-INF/support/datasource.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tsharding-client/src/test/resources/META-INF/tesla/support/service-loader.xml b/tsharding-client/src/test/resources/META-INF/support/service-loader.xml similarity index 100% rename from tsharding-client/src/test/resources/META-INF/tesla/support/service-loader.xml rename to tsharding-client/src/test/resources/META-INF/support/service-loader.xml diff --git a/tsharding-client/src/test/resources/tesla.properties b/tsharding-client/src/test/resources/app.properties similarity index 73% rename from tsharding-client/src/test/resources/tesla.properties rename to tsharding-client/src/test/resources/app.properties index 670a3d2..1b5a08c 100644 --- a/tsharding-client/src/test/resources/tesla.properties +++ b/tsharding-client/src/test/resources/app.properties @@ -15,5 +15,5 @@ tesla.monitorTask.timeSpan = 60000 tesla.network.readIdleTimeout = 0 tesla.network.compressThreshold = 1024 -tesla.orm.mapperPacakge=com.mogujie.service.tsharding.route.dal -tsharding.needEnhancedClasses=com.mogujie.service.tsharding.route.dal.ShopOrderMapper \ No newline at end of file +tesla.orm.mapperPacakge=com.mogujie.service.tsharding.mapper +tsharding.needEnhancedClasses=com.mogujie.service.tsharding.mapper.ShopOrderMapper \ No newline at end of file diff --git a/tsharding-client/src/test/resources/jdbc.properties b/tsharding-client/src/test/resources/jdbc.properties index 7c08398..17f64cb 100644 --- a/tsharding-client/src/test/resources/jdbc.properties +++ b/tsharding-client/src/test/resources/jdbc.properties @@ -1,137 +1,136 @@ -# testschema0000 master -testschema0000.master.url=jdbc:mysql://127.0.0.1/testschema0000 -testschema0000.master.username=root -testschema0000.master.password=root123 -testschema0000.master.minPoolSize=0 -testschema0000.master.maxPoolSize=10 -testschema0000.master.initialPoolSize=0 -# testschema0000 slave -testschema0000.slave.url=jdbc:mysql://127.0.0.1/testschema0000 -testschema0000.slave.username=root -testschema0000.slave.password=root123 -testschema0000.slave.minPoolSize=1 -testschema0000.slave.maxPoolSize=24 -testschema0000.slave.initialPoolSize=1 +# 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 -# testschema0001 master -testschema0001.master.url=jdbc:mysql://127.0.0.1/testschema0001 -testschema0001.master.username=root -testschema0001.master.password=root123 -testschema0001.master.minPoolSize=0 -testschema0001.master.maxPoolSize=10 -testschema0001.master.initialPoolSize=0 -# testschema0001 slave -testschema0001.slave.url=jdbc:mysql://127.0.0.1/testschema0001 -testschema0001.slave.username=root -testschema0001.slave.password=root123 -testschema0001.slave.minPoolSize=1 -testschema0001.slave.maxPoolSize=24 -testschema0001.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 -# testschema0002 master -testschema0002.master.url=jdbc:mysql://127.0.0.1/testschema0002 -testschema0002.master.username=root -testschema0002.master.password=root123 -testschema0002.master.minPoolSize=0 -testschema0002.master.maxPoolSize=10 -testschema0002.master.initialPoolSize=0 -# testschema0002 slave -testschema0002.slave.url=jdbc:mysql://127.0.0.1/testschema0002 -testschema0002.slave.username=root -testschema0002.slave.password=root123 -testschema0002.slave.minPoolSize=1 -testschema0002.slave.maxPoolSize=24 -testschema0002.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 -# testschema0003 master -testschema0003.master.url=jdbc:mysql://127.0.0.1/testschema0003 +# 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 -testschema0003.master.username=root -testschema0003.master.password=root123 -testschema0003.master.minPoolSize=0 -testschema0003.master.maxPoolSize=10 -testschema0003.master.initialPoolSize=0 -# testschema0003 slave -testschema0003.slave.url=jdbc:mysql://127.0.0.1/testschema0003 -testschema0003.slave.username=root -testschema0003.slave.password=root123 -testschema0003.slave.minPoolSize=1 -testschema0003.slave.maxPoolSize=24 -testschema0003.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 -# testschema0004 master -testschema0004.master.url=jdbc:mysql://127.0.0.1/testschema0004 -testschema0004.master.username=root -testschema0004.master.password=root123 -testschema0004.master.minPoolSize=0 -testschema0004.master.maxPoolSize=10 -testschema0004.master.initialPoolSize=0 -# testschema0004 slave -testschema0004.slave.url=jdbc:mysql://127.0.0.1/testschema0004 -testschema0004.slave.username=root -testschema0004.slave.password=root123 -testschema0004.slave.minPoolSize=1 -testschema0004.slave.maxPoolSize=24 -testschema0004.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 -# testschema0005 master -testschema0005.master.url=jdbc:mysql://127.0.0.1/testschema0005 -testschema0005.master.username=root -testschema0005.master.password=root123 -testschema0005.master.minPoolSize=0 -testschema0005.master.maxPoolSize=10 -testschema0005.master.initialPoolSize=0 -# testschema0005 slave -testschema0005.slave.url=jdbc:mysql://127.0.0.1/testschema0005 -testschema0005.slave.username=root -testschema0005.slave.password=root123 -testschema0005.slave.minPoolSize=1 -testschema0005.slave.maxPoolSize=24 -testschema0005.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 -# testschema0006 master -testschema0006.master.url=jdbc:mysql://127.0.0.1/testschema0006 -testschema0006.master.username=root -testschema0006.master.password=root123 -testschema0006.master.minPoolSize=0 -testschema0006.master.maxPoolSize=10 -testschema0006.master.initialPoolSize=0 -# testschema0006 slave -testschema0006.slave.url=jdbc:mysql://127.0.0.1/testschema0006 -testschema0006.slave.username=root -testschema0006.slave.password=root123 -testschema0006.slave.minPoolSize=1 -testschema0006.slave.maxPoolSize=24 -testschema0006.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 -# testschema0007 master -testschema0007.master.url=jdbc:mysql://127.0.0.1/testschema0007 -testschema0007.master.username=root -testschema0007.master.password=root123 -testschema0007.master.minPoolSize=0 -testschema0007.master.maxPoolSize=10 -testschema0007.master.initialPoolSize=0 -# testschema0007 slave -testschema0007.slave.url=jdbc:mysql://127.0.0.1/testschema0007 -testschema0007.slave.username=root -testschema0007.slave.password=root123 -testschema0007.slave.minPoolSize=1 -testschema0007.slave.maxPoolSize=24 -testschema0007.slave.initialPoolSize=1 - -# testschema单库 master -testschema.master.url=jdbc:mysql://127.0.0.1/trade -testschema.master.username=root -testschema.master.password=root123 -testschema.master.minPoolSize=0 -testschema.master.maxPoolSize=10 -testschema.master.initialPoolSize=0 +# xdtrade单库 master +xdtrade.master.url=jdbc:mysql://10.13.4.23/trade +xdtrade.master.username=test +xdtrade.master.password=test123 +xdtrade.master.minPoolSize=0 +xdtrade.master.maxPoolSize=10 +xdtrade.master.initialPoolSize=0 # rootjie slave -testschema.slave.url=jdbc:mysql://127.0.0.1/trade -testschema.slave.username=root -testschema.slave.password=root123 -testschema.slave.minPoolSize=1 -testschema.slave.maxPoolSize=24 -testschema.slave.initialPoolSize=1 +xdtrade.slave.url=jdbc:mysql://10.13.4.23/trade +xdtrade.slave.username=test +xdtrade.slave.password=test123 +xdtrade.slave.minPoolSize=1 +xdtrade.slave.maxPoolSize=24 +xdtrade.slave.initialPoolSize=1 diff --git a/tsharding-client/src/test/resources/logback-test.xml b/tsharding-client/src/test/resources/logback.xml similarity index 83% rename from tsharding-client/src/test/resources/logback-test.xml rename to tsharding-client/src/test/resources/logback.xml index ef8fb9f..e337ac2 100644 --- a/tsharding-client/src/test/resources/logback-test.xml +++ b/tsharding-client/src/test/resources/logback.xml @@ -1,8 +1,8 @@ - + - + diff --git a/tsharding-client/src/test/resources/spring-test.xml b/tsharding-client/src/test/resources/spring-test.xml new file mode 100644 index 0000000..f78aff3 --- /dev/null +++ b/tsharding-client/src/test/resources/spring-test.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml b/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml index 14256f4..f0404d9 100644 --- a/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml +++ b/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml @@ -2,3 +2,40 @@ + + + + + orderId, + buyerUserId, + sellerUserId + + + + + + + + \ No newline at end of file From ee5a81fdb47b03ebb4df05db1fe5a0711c48e741 Mon Sep 17 00:00:00 2001 From: jiuru Date: Thu, 14 Jul 2016 13:16:03 +0800 Subject: [PATCH 4/6] @jiuru --- tsharding-client/README.md | 15 +---- .../service/tsharding/bean/BaseOrder.java | 9 +++ .../tsharding/mapper/ShopOrderMapper.java | 6 ++ .../service/tsharding/test/TShardingTest.java | 20 +++--- .../tsharding/test/annotation/SimpleDao.java | 13 ---- .../test/annotation/SimpleDaoImpl.java | 16 ----- .../SimpleDaoWithAnotationTest.java | 60 ------------------ .../tsharding/test/route/GenerateTest.java | 61 ------------------- .../tsharding/test/route/InvokerTest.java | 13 ---- .../test/route/SchemaPropertiesTest.java | 31 ---------- .../test/route/ShopOrderDaoTest.java | 45 -------------- .../test/route/ShopOrderMapperTest.java | 56 ----------------- .../tsharding/test/route/TestJavaAssit.java | 61 ------------------- .../META-INF/support/service-loader.xml | 2 +- .../src/test/resources/app.properties | 2 +- .../resources/sqlmap/shoporder-mapper.xml | 21 +++++++ 16 files changed, 51 insertions(+), 380 deletions(-) delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDao.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoImpl.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoWithAnotationTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/GenerateTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/InvokerTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/SchemaPropertiesTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java delete mode 100644 tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/TestJavaAssit.java diff --git a/tsharding-client/README.md b/tsharding-client/README.md index cce5b61..0f506a9 100644 --- a/tsharding-client/README.md +++ b/tsharding-client/README.md @@ -1,17 +1,6 @@ 交易分库分表组件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支持。 +#### +1. diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java index 1925972..5fe8360 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/bean/BaseOrder.java @@ -9,6 +9,15 @@ public abstract class BaseOrder { private Long sellerUserId; + private Long shipTime; + + public Long getShipTime() { + return shipTime; + } + + public void setShipTime(Long shipTime) { + this.shipTime = shipTime; + } public Long getOrderId() { return orderId; diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java index 62e8c30..0eab19b 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/mapper/ShopOrderMapper.java @@ -6,15 +6,21 @@ import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara; import com.mogujie.trade.tsharding.route.TShardingRoutingHandler; import com.mogujie.trade.tsharding.route.orm.MapperResourceEnhancer; +import org.apache.ibatis.annotations.Param; import java.util.List; @DataSourceRouting(handler = TShardingRoutingHandler.class) public interface ShopOrderMapper { + @ShardingExtensionMethod(type = MapperResourceEnhancer.class, method = "enhancedShardingSQL") public ShopOrder getShopOrderByShopOrderId(@ShardingOrderPara Long shopOrderId); @ShardingExtensionMethod(type = MapperResourceEnhancer.class, method = "enhancedShardingSQL") public List getShopOrderByShopOrderIds(@ShardingOrderPara List shopOrderIds); + + @ShardingExtensionMethod(type = MapperResourceEnhancer.class, method = "enhancedShardingSQL") + int batchUpdateShopOrderByShopOrderIds(@ShardingOrderPara @Param("shopOrderIds") List shopOrderIds, @Param("shopOrder") ShopOrder shopOrder); + } \ No newline at end of file diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java index bbfae9a..f82f8f4 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java @@ -7,6 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.Assert; import java.util.ArrayList; import java.util.List; @@ -25,22 +26,23 @@ public class TShardingTest extends BaseTest { @Autowired private ShopOrderMapper shopOrderMapper; + @Test - public void testGetShopOrderByShopOrderIdsOfOneBuyer() { + public void testGetShopOrderByShopOrderIds() { List orderIds = new ArrayList<>(); - orderIds.add(111L); - List orders = shopOrderDao.getShopOrderByShopOrderIdsOfOneBuyer(orderIds); - System.out.println(); + orderIds.add(50000280834672L); + List orders = shopOrderMapper.getShopOrderByShopOrderIds(orderIds); + Assert.isTrue(orders.get(0).getOrderId().equals(50000280834672L)); } - @Test - public void testGetShopOrderByShopOrderIds() { + public void testUpdateShopOrder() { List orderIds = new ArrayList<>(); orderIds.add(50000280834672L); -// orderIds.add(70000204797232L); - List orders = shopOrderMapper.getShopOrderByShopOrderIds(orderIds); - System.out.println(); + ShopOrder shopOrder = new ShopOrder(); + shopOrder.setShipTime(12345678L); + int rows = shopOrderMapper.batchUpdateShopOrderByShopOrderIds(orderIds, shopOrder); + Assert.isTrue(rows == 1); } diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDao.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDao.java deleted file mode 100644 index 751f79a..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDao.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mogujie.service.tsharding.test.annotation; - -import com.mogujie.trade.tsharding.annotation.parameter.ShardingBuyerPara; -import org.apache.ibatis.annotations.Param; - -/** - * @auther qigong on 5/29/15 11:13 AM. - */ -public interface SimpleDao { - - public void abc(String a,String b,@ShardingBuyerPara @Param("test") Long id); - -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoImpl.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoImpl.java deleted file mode 100644 index 1935b24..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoImpl.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.mogujie.service.tsharding.test.annotation; - -import com.mogujie.trade.tsharding.annotation.parameter.ShardingBuyerPara; -import org.apache.ibatis.annotations.Param; - -/** - * @auther qigong on 5/29/15 2:24 PM. - */ -public class SimpleDaoImpl implements SimpleDao { - - - public void abc(String a,String b,@ShardingBuyerPara @Param("xxx") Long id){ - - System.out.println("real method"); - } -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoWithAnotationTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoWithAnotationTest.java deleted file mode 100644 index ce5e35e..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/annotation/SimpleDaoWithAnotationTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.mogujie.service.tsharding.test.annotation; - -import com.alibaba.fastjson.JSON; -import com.mogujie.trade.tsharding.annotation.parameter.ShardingBuyerPara; -import org.apache.ibatis.annotations.Param; - -import java.lang.annotation.Annotation; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - -/** - * @auther qigong on 5/29/15 12:46 PM. - */ -public class SimpleDaoWithAnotationTest { - - private static ShardingBuyerPara a; - - public static void main(String[] args) { - Class z = SimpleDao.class; - try { - InvocationHandler handler = new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - System.out.println("proxy method"); - System.out.println(JSON.toJSONString(method.getParameterTypes())); - Annotation[][] an = method.getParameterAnnotations(); - if (an.length > 0) { - for (int i = 0; i < an.length; i++) { - System.out.print("i::" + i + ";"); - System.out.println(JSON.toJSONString(an[i].length)); - for (int j = 0; j < an[i].length; j++) { - System.out.println("j::" + j); - if(j==0) { - a = (ShardingBuyerPara) an[i][j]; - System.out.println("sharding参数"+args[i]); - }else { - Param b = (Param) an[i][j]; - System.out.println(b.value()); - } - } - } - } - return method.invoke(SimpleDaoImpl.class.newInstance(), new Object[]{"abc", "cde", 4191574207234L}); - } - }; - Proxy proxy = (Proxy) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{SimpleDao.class}, handler); - - Method m = z.getMethod("abc", new Class[]{String.class, String.class, Long.class}); - try { - handler.invoke(proxy, m, new Object[]{"abc", "cde", 4191574207234L}); - } catch (Throwable throwable) { - throwable.printStackTrace(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - } -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/GenerateTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/GenerateTest.java deleted file mode 100644 index 8bab612..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/GenerateTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.mogujie.service.tsharding.test.route; - -import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara; -import com.mogujie.trade.tsharding.route.orm.MapperAnnotationEnhancer; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtMethod; -import javassist.bytecode.ClassFile; -import javassist.bytecode.ConstPool; - -import java.io.IOException; - -/** - * @author qigong on 15/9/5 下午12:14. - */ -public class GenerateTest { - - public static void main(String[] args) { - - ClassPool cp = ClassPool.getDefault(); - try { - CtClass cc = cp.makeInterface("com.mogujie.service.tsharding.route.Tester"); - - ClassPool pool = ClassPool.getDefault(); - - Class originClass = Class.forName("com.mogujie.service.tsharding.route.InvokerTest"); - - - CtClass c = pool.get("com.mogujie.service.tsharding.route.InvokerTest"); - CtMethod ctMethod = c.getDeclaredMethods()[0]; - CtMethod cm = new CtMethod(ctMethod.getReturnType(), ctMethod.getName(), ctMethod.getParameterTypes(), cc); - ClassFile ccFile = cc.getClassFile(); - ConstPool constPool = ccFile.getConstPool(); - cm.getMethodInfo().addAttribute(MapperAnnotationEnhancer.duplicateParameterAnnotationsAttribute(constPool, originClass.getDeclaredMethods()[0])); - - cc.addMethod(cm); - Class newClass = cc.toClass(); - - java.lang.annotation.Annotation[][] annotations = newClass.getDeclaredMethods()[0].getParameterAnnotations(); - if (annotations.length > 0) { - for (int i = 0; i < annotations.length; i++) { - for (int j = 0; j < annotations[i].length; j++) { - if (annotations[i][j] instanceof ShardingOrderPara) { - Long shardingPara = 0L; - System.out.println("comin again!"); - break; - } - } - } - } - - try { - cc.writeFile("."); - } catch (IOException e) { - e.printStackTrace(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/InvokerTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/InvokerTest.java deleted file mode 100644 index d2b106b..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/InvokerTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mogujie.service.tsharding.test.route; - -import com.mogujie.trade.tsharding.annotation.parameter.ShardingOrderPara; -import com.mogujie.trade.tsharding.route.orm.base.Invocation; - -/** - * @author qigong - * - */ -public interface InvokerTest { - - Object invoke(@ShardingOrderPara("test") Invocation invocation) throws Throwable; -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/SchemaPropertiesTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/SchemaPropertiesTest.java deleted file mode 100644 index a40bbf3..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/SchemaPropertiesTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.mogujie.service.tsharding.test.route; - -import com.mogujie.trade.tsharding.client.ShardingCaculator; - -/** - * @author qigong on 15/9/7 下午6:16. - */ -public class SchemaPropertiesTest { - - public static void main(String[] args) { - - String[] initConfs = new String[]{ - "# testschemapressuretest master", - "testschemapressuretest.master.url=jdbc:mysql://10.13.4.23/xdtrade", - "testschemapressuretest.master.port=3306", - "testschemapressuretest.master.username=test", - "testschemapressuretest.master.password=test123", - "testschemapressuretest.master.minPoolSize=1", - "testschemapressuretest.master.maxPoolSize=24", - "testschemapressuretest.master.initialPoolSize=1" - }; - - for (long i = 0; i < 8; i++) { - for (String conf : initConfs) { - conf = conf.replace("xdtrade", "xdtrade" + ShardingCaculator.getNumberWithZeroSuffix(i)); - conf = conf.replace("sharding0", "sharding" + i/4); - System.out.println(conf); - } - } - } -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java deleted file mode 100644 index d36136f..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderDaoTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mogujie.service.tsharding.test.route; - -import com.mogujie.service.tsharding.bean.ShopOrder; -import com.mogujie.service.tsharding.dao.ShopOrderDao; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import java.util.ArrayList; -import java.util.List; - -/** - * @auther qigong on 6/5/15 8:55 PM. - */ -public class ShopOrderDaoTest { - - private final Logger logger = LoggerFactory.getLogger(getClass()); - - private ConfigurableApplicationContext applicationContext; - - @Before - public void setUp() throws Exception { - this.applicationContext = new ClassPathXmlApplicationContext("META-INF/support/service-loader.xml", - "META-INF/support/datasource.xml"); - this.shopOrderDao = applicationContext.getBean(ShopOrderDao.class); - } - - - private ShopOrderDao shopOrderDao; - - @Test - public void testGetShopOrderByShopOrderIdsOfOneBuyer() { - - List listShopOrderIds = new ArrayList<>(); - listShopOrderIds.add(3968484880824L); - listShopOrderIds.add(3968484880824L); - - List shopOrders = shopOrderDao.getShopOrderByShopOrderIdsOfOneBuyer(listShopOrderIds); - Assert.assertTrue(shopOrders.size() == 2); - } -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java deleted file mode 100644 index 1c11541..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/ShopOrderMapperTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.mogujie.service.tsharding.test.route; - -import com.mogujie.service.tsharding.bean.ShopOrder; -import com.mogujie.service.tsharding.mapper.ShopOrderMapper; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class ShopOrderMapperTest { - - private final Logger logger = LoggerFactory.getLogger(getClass()); - - private ConfigurableApplicationContext applicationContext; - - @Autowired - private ShopOrderMapper shopOrderMapper; - - @Before - public void setUp() throws Exception { - this.applicationContext = new ClassPathXmlApplicationContext("META-INF/support/service-loader.xml", - "META-INF/support/datasource.xml"); - this.shopOrderMapper = applicationContext.getBean(ShopOrderMapper.class); - } - - @After - public void tearDown() throws Exception { - this.applicationContext.close(); - } - - - @Test - public void testXdOrderSharding() { - - ShopOrder shopOrder = null; - try { - shopOrder = shopOrderMapper.getShopOrderByShopOrderId(3950627820824L); - logger.debug("{}", shopOrder); - Assert.assertNotNull(shopOrder); - - shopOrder = shopOrderMapper.getShopOrderByShopOrderId(3950627820002L); - logger.debug("{}", shopOrder); - Assert.assertNull(shopOrder); - } catch (Exception e) { - logger.error("test error: {}", e.getMessage(), e); - } - - } - - -} diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/TestJavaAssit.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/TestJavaAssit.java deleted file mode 100644 index df35a25..0000000 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/route/TestJavaAssit.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.mogujie.service.tsharding.test.route; - -import javassist.CannotCompileException; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtMethod; -import javassist.bytecode.AnnotationsAttribute; -import javassist.bytecode.ClassFile; -import javassist.bytecode.ConstPool; -import javassist.bytecode.annotation.Annotation; -import javassist.bytecode.annotation.IntegerMemberValue; - -import java.io.IOException; -import java.lang.reflect.Method; - -/** - * @author qigong on 15/9/5 下午12:14. - */ -public class TestJavaAssit { - - public static void main(String[] args){ - - ClassPool cp = ClassPool.getDefault(); - try { - CtClass cc = cp.makeClass("com.mogujie.service.tsharding.route.Tester1"); - CtMethod cm = new CtMethod(CtClass.intType, "add", new CtClass[] { - CtClass.intType, CtClass.intType }, cc); - - - ClassFile ccFile = cc.getClassFile(); - ConstPool constpool = ccFile.getConstPool(); - - AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); - Annotation annot = new Annotation("com.mogujie.trade.tsharding.annotation.parameter.ShardingTablePara", constpool); - annot.addMemberValue("value", new IntegerMemberValue(ccFile.getConstPool(), 0)); - - cm.getMethodInfo().addAttribute(attr); - -// - cm.setBody("return $1 + $2;"); - cm.insertAfter("for(int i=0;i<$args.length;i++)" - + "{System.out.println(\"args[\"+i+\"]=\"+$args[i]);}"); - cc.addMethod(cm); - Class newClass = cc.toClass(); - Object o = newClass.newInstance(); - Method m = newClass.getDeclaredMethod("add", int.class, int.class); - System.out.println("Result:" + m.invoke(o, 1, 2)); - try { - cc.writeFile("."); - } catch (IOException e) { - e.printStackTrace(); - } - } catch (CannotCompileException e) { - e.printStackTrace(); - } catch (SecurityException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/tsharding-client/src/test/resources/META-INF/support/service-loader.xml b/tsharding-client/src/test/resources/META-INF/support/service-loader.xml index 976ba0b..bf8bf47 100644 --- a/tsharding-client/src/test/resources/META-INF/support/service-loader.xml +++ b/tsharding-client/src/test/resources/META-INF/support/service-loader.xml @@ -8,7 +8,7 @@ http://www.springframework.org/schema/context/spring-context-4.0.xsd"> - + diff --git a/tsharding-client/src/test/resources/app.properties b/tsharding-client/src/test/resources/app.properties index 1b5a08c..2578861 100644 --- a/tsharding-client/src/test/resources/app.properties +++ b/tsharding-client/src/test/resources/app.properties @@ -15,5 +15,5 @@ tesla.monitorTask.timeSpan = 60000 tesla.network.readIdleTimeout = 0 tesla.network.compressThreshold = 1024 -tesla.orm.mapperPacakge=com.mogujie.service.tsharding.mapper +tsharding.orm.mapperPacakge=com.mogujie.service.tsharding.mapper tsharding.needEnhancedClasses=com.mogujie.service.tsharding.mapper.ShopOrderMapper \ No newline at end of file diff --git a/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml b/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml index f0404d9..79e0575 100644 --- a/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml +++ b/tsharding-client/src/test/resources/sqlmap/shoporder-mapper.xml @@ -5,6 +5,15 @@ + + + + + shipTime=#{shopOrder.shipTime}, + + + + orderId, buyerUserId, @@ -38,4 +47,16 @@ limit 500 + + + UPDATE XDOrder + ,updated = unix_timestamp(now()) + where orderId in + + #{shopOrderId} + + AND level = 1 + limit 500 + + \ No newline at end of file From 514d6fdd1088881a1a7fb58f810b34a1a9ee2522 Mon Sep 17 00:00:00 2001 From: jiuru Date: Thu, 14 Jul 2016 14:21:21 +0800 Subject: [PATCH 5/6] @jiuru update jar version --- tsharding-client/pom.xml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tsharding-client/pom.xml b/tsharding-client/pom.xml index 81d5bdb..138f8a1 100644 --- a/tsharding-client/pom.xml +++ b/tsharding-client/pom.xml @@ -14,6 +14,10 @@ 4.10 3.19.0-GA 1.2.7 + 1.7.21 + 1.0.18 + 5.1.38 + 1.8.8 @@ -81,24 +85,24 @@ org.slf4j slf4j-api - 1.7.21 + ${slf4j-api.version} com.alibaba druid - 1.0.18 + ${druid.version} mysql mysql-connector-java - 5.1.38 + ${mysql.version} org.aspectj aspectjweaver - 1.8.8 + ${aspectj.version} From 6bf3f639f2101d73f665480e54605e63625228e6 Mon Sep 17 00:00:00 2001 From: jiuru Date: Thu, 14 Jul 2016 14:29:58 +0800 Subject: [PATCH 6/6] @jiuru add readme --- tsharding-client/README.md | 5 +++- .../service/tsharding/dao/ShopOrderDao.java | 11 +------- .../tsharding/dao/ShopOrderDaoImpl.java | 27 +------------------ .../service/tsharding/test/TShardingTest.java | 8 ++++++ 4 files changed, 14 insertions(+), 37 deletions(-) diff --git a/tsharding-client/README.md b/tsharding-client/README.md index 0f506a9..2bbcb00 100644 --- a/tsharding-client/README.md +++ b/tsharding-client/README.md @@ -3,4 +3,7 @@ #### -1. +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 + diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDao.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDao.java index 6824272..494a12b 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDao.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDao.java @@ -15,15 +15,6 @@ public interface ShopOrderDao { * @param listShopOrderIds 店铺级订单ID集合 * @return List */ - List getShopOrderByShopOrderIdsOfOneBuyer(List listShopOrderIds); - - /** - * 根据店铺级订单ID获取订单信息(多个买家) - * - * @param listShopOrderIds 店铺级订单ID集合 - * @return List - */ - List getShopOrderByShopOrderIdsOfMultiBuyer(List listShopOrderIds); - + List getShopOrderByShopOrderIds(List listShopOrderIds); } diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDaoImpl.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDaoImpl.java index c4cd092..c9f5a92 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDaoImpl.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/dao/ShopOrderDaoImpl.java @@ -2,12 +2,10 @@ import com.mogujie.service.tsharding.bean.ShopOrder; import com.mogujie.service.tsharding.mapper.ShopOrderMapper; -import com.mogujie.trade.tsharding.client.ShardingCaculator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -22,7 +20,7 @@ public class ShopOrderDaoImpl implements ShopOrderDao { private ShopOrderMapper shopOrderMapper; @Override - public List getShopOrderByShopOrderIdsOfOneBuyer(List listShopOrderIds) { + public List getShopOrderByShopOrderIds(List listShopOrderIds) { if (listShopOrderIds == null || listShopOrderIds.size() == 0) { return null; } @@ -35,27 +33,4 @@ public List getShopOrderByShopOrderIdsOfOneBuyer(List listShopO return shopOrderMapper.getShopOrderByShopOrderIds(new ArrayList(setShopOrderIds)); } - - @Override - public List getShopOrderByShopOrderIdsOfMultiBuyer(List listShopOrderIds) { - if (listShopOrderIds == null || listShopOrderIds.size() == 0) { - return null; - } - HashMap> shopOrderIdsMap = new HashMap(); - for (Long shopOrderId : listShopOrderIds) { - Integer tableIndex = ShardingCaculator.caculateTableIndex(shopOrderId); - List orderIds = shopOrderIdsMap.get(tableIndex); - if (orderIds == null) { - orderIds = new ArrayList<>(); - } - orderIds.add(shopOrderId); - shopOrderIdsMap.put(tableIndex, orderIds); - } - List result = new ArrayList<>(); - - for (Integer tableIndex : shopOrderIdsMap.keySet()) { - result.addAll(shopOrderMapper.getShopOrderByShopOrderIds(shopOrderIdsMap.get(tableIndex))); - } - return result; - } } diff --git a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java index f82f8f4..8f092a3 100644 --- a/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java +++ b/tsharding-client/src/test/java/com/mogujie/service/tsharding/test/TShardingTest.java @@ -27,6 +27,14 @@ public class TShardingTest extends BaseTest { private ShopOrderMapper shopOrderMapper; + @Test + public void testGetShopOrderByShopOrderIdsDao() { + List orderIds = new ArrayList<>(); + orderIds.add(50000280834672L); + List orders = shopOrderDao.getShopOrderByShopOrderIds(orderIds); + Assert.isTrue(orders.get(0).getOrderId().equals(50000280834672L)); + } + @Test public void testGetShopOrderByShopOrderIds() { List orderIds = new ArrayList<>();