Commit 158429f62a6abc3168efb89d55280d8f7797a54a

Authored by 王彬
1 parent 7de08bb0

sharding 5.0

build.gradle
... ... @@ -23,7 +23,7 @@ dependencies {
23 23 compile('org.springframework:spring-jdbc:5.1.9.RELEASE')
24 24 compile('mysql:mysql-connector-java:5.1.47')
25 25 compile('com.alibaba:druid:1.2.4')
26   - compile("org.apache.shardingsphere:sharding-jdbc-spring-boot-starter:4.1.1")
  26 + compile("org.apache.shardingsphere:shardingsphere-sharding-spring-boot-starter:5.0.0-alpha")
27 27 }
28 28  
29 29 repositories {
... ...
src/main/java/com/taover/repository/jdbctemplate/JdbcTemplateBroadcastImpl.java
... ... @@ -4,7 +4,7 @@ import java.util.Collection;
4 4 import java.util.List;
5 5 import java.util.Map;
6 6  
7   -import org.apache.shardingsphere.api.hint.HintManager;
  7 +import org.apache.shardingsphere.infra.hint.HintManager;
8 8 import org.springframework.dao.DataAccessException;
9 9 import org.springframework.jdbc.core.BatchPreparedStatementSetter;
10 10 import org.springframework.jdbc.core.CallableStatementCallback;
... ...
src/main/java/com/taover/repository/jdbctemplate/JdbcTemplateWrapperTenantImpl.java
... ... @@ -4,7 +4,7 @@ import java.util.Collection;
4 4 import java.util.List;
5 5 import java.util.Map;
6 6  
7   -import org.apache.shardingsphere.api.hint.HintManager;
  7 +import org.apache.shardingsphere.infra.hint.HintManager;
8 8 import org.springframework.dao.DataAccessException;
9 9 import org.springframework.jdbc.core.BatchPreparedStatementSetter;
10 10 import org.springframework.jdbc.core.CallableStatementCallback;
... ...
src/main/java/com/taover/repository/shardingsphere/ShardingDatabaseAlgorithmHint.java
... ... @@ -2,15 +2,17 @@ package com.taover.repository.shardingsphere;
2 2  
3 3 import java.util.Collection;
4 4 import java.util.HashSet;
  5 +import java.util.Properties;
5 6 import java.util.Set;
6 7  
7 8 import org.apache.commons.logging.Log;
8 9 import org.apache.commons.logging.LogFactory;
9   -import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
10   -import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;
  10 +import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm;
  11 +import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;
11 12  
12 13 public class ShardingDatabaseAlgorithmHint implements HintShardingAlgorithm<String> {
13 14 Log log = LogFactory.getLog(ShardingDatabaseAlgorithmHint.class);
  15 + Properties prop = null;
14 16  
15 17 public ShardingDatabaseAlgorithmHint() { }
16 18  
... ... @@ -28,4 +30,24 @@ public class ShardingDatabaseAlgorithmHint implements HintShardingAlgorithm&lt;Stri
28 30 }
29 31 return result;
30 32 }
  33 +
  34 + @Override
  35 + public String getType() {
  36 + return "DATABASE-HINT";
  37 + }
  38 +
  39 + @Override
  40 + public Properties getProps() {
  41 + return this.prop;
  42 + }
  43 +
  44 + @Override
  45 + public void setProps(Properties props) {
  46 + this.prop = props;
  47 + }
  48 +
  49 + @Override
  50 + public void init() {
  51 + System.out.print("ShardingDatabaseAlgorithmHint:init");
  52 + }
31 53 }
... ...
src/main/java/com/taover/repository/shardingsphere/ShardingKeyGeneratorImpl.java
... ... @@ -21,8 +21,8 @@ import java.util.Calendar;
21 21 import java.util.List;
22 22 import java.util.Properties;
23 23  
24   -import org.apache.shardingsphere.core.strategy.keygen.TimeService;
25   -import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator;
  24 +import org.apache.shardingsphere.sharding.algorithm.keygen.TimeService;
  25 +import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
26 26  
27 27 import com.google.common.base.Preconditions;
28 28 import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfiguration;
... ... @@ -49,7 +49,7 @@ import com.taover.repository.autoconfigure.ShardingSphereKeyGeneratorConfigurati
49 49 * Call @{@code SnowflakeShardingKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds} to set max tolerate time difference milliseconds, default value is 0.
50 50 * </p>
51 51 */
52   -public final class ShardingKeyGeneratorImpl implements ShardingKeyGenerator, ShardingKeyGeneratorExt {
  52 +public final class ShardingKeyGeneratorImpl implements KeyGenerateAlgorithm, ShardingKeyGeneratorExt {
53 53  
54 54 public static final long EPOCH;
55 55  
... ... @@ -183,16 +183,6 @@ public final class ShardingKeyGeneratorImpl implements ShardingKeyGenerator, Sha
183 183 return data;
184 184 }
185 185  
186   - @Override
187   - public Properties getProperties() {
188   - return this.properties;
189   - }
190   -
191   - @Override
192   - public void setProperties(Properties properties) {
193   - this.properties = properties;
194   - }
195   -
196 186 public static void setTimeService(TimeService timeService) {
197 187 ShardingKeyGeneratorImpl.timeService = timeService;
198 188 }
... ... @@ -209,4 +199,19 @@ public final class ShardingKeyGeneratorImpl implements ShardingKeyGenerator, Sha
209 199 properties.setProperty("max.vibration.offset", config.getMaxVibrationOffset());
210 200 }
211 201 }
  202 +
  203 + @Override
  204 + public Properties getProps() {
  205 + return this.properties;
  206 + }
  207 +
  208 + @Override
  209 + public void setProps(Properties props) {
  210 + this.properties = props;
  211 + }
  212 +
  213 + @Override
  214 + public void init() {
  215 + System.out.println("ShardingKeyGeneratorImpl:init");
  216 + }
212 217 }
... ...
src/main/java/com/taover/repository/shardingsphere/ShardingTableAlgorithmHint.java
... ... @@ -2,16 +2,19 @@ package com.taover.repository.shardingsphere;
2 2  
3 3 import java.util.Collection;
4 4 import java.util.HashSet;
  5 +import java.util.Properties;
5 6 import java.util.Set;
6 7  
7 8 import org.apache.commons.logging.Log;
8 9 import org.apache.commons.logging.LogFactory;
9   -import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;
10   -import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;
  10 +import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm;
  11 +import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;
11 12  
12 13 public class ShardingTableAlgorithmHint implements HintShardingAlgorithm<String> {
13 14 Log log = LogFactory.getLog(ShardingTableAlgorithmHint.class);
14 15  
  16 + Properties prop = null;
  17 +
15 18 public ShardingTableAlgorithmHint() { }
16 19  
17 20 @Override
... ... @@ -28,4 +31,24 @@ public class ShardingTableAlgorithmHint implements HintShardingAlgorithm&lt;String&gt;
28 31 }
29 32 return result;
30 33 }
  34 +
  35 + @Override
  36 + public String getType() {
  37 + return "TABLE-HINT";
  38 + }
  39 +
  40 + @Override
  41 + public Properties getProps() {
  42 + return this.prop;
  43 + }
  44 +
  45 + @Override
  46 + public void setProps(Properties props) {
  47 + this.prop = props;
  48 + }
  49 +
  50 + @Override
  51 + public void init() {
  52 + System.out.println("ShardingTableAlgorithmHint:init");
  53 + }
31 54 }
... ...
src/test/java/com/taover/repository/test/TestAutoconfigure.java
1 1 package com.taover.repository.test;
2 2  
  3 +import java.util.List;
  4 +import java.util.Map;
  5 +
3 6 import org.springframework.boot.SpringApplication;
4 7 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 8 import org.springframework.context.ConfigurableApplicationContext;
6 9 import org.springframework.scheduling.annotation.EnableScheduling;
7 10  
8   -import com.taover.repository.jdbctemplate.JdbcTemplateBroadcast;
9 11 import com.taover.repository.jdbctemplate.JdbcTemplateWrapperTenant;
10 12  
11 13 @SpringBootApplication
... ... @@ -14,13 +16,37 @@ public class TestAutoconfigure {
14 16 public static void main(String args[]) {
15 17 ConfigurableApplicationContext context = SpringApplication.run(TestAutoconfigure.class, args);
16 18  
17   - JdbcTemplateBroadcast jdbcTemplateB = context.getBean(JdbcTemplateBroadcast.class);
18   - System.out.println(jdbcTemplateB.queryForObject("select id from wxorder_order order by id desc limit 5 ", String.class, new String[]{"wxorder_order"}));
  19 +// JdbcTemplateBroadcast jdbcTemplateB = context.getBean(JdbcTemplateBroadcast.class);
  20 +// System.out.println(jdbcTemplateB.queryForObject("select id from wxorder_order order by id desc limit 5 ", String.class, new String[]{"wxorder_order"}));
19 21  
20 22 // JdbcTemplateWrapperTenant jdbcTemplate = context.getBean(JdbcTemplateWrapperTenant.class);
21 23 // System.out.println(jdbcTemplate.queryForObject("select id from wxorder_order limit 1", Long.class, 1L));
22 24 // System.out.println(jdbcTemplate.queryForList("select * from wxorder_order limit 1", 1L));
23 25 // System.out.println(jdbcTemplate.queryForList("select * from wxorder_order limit 1", 2L));
24 26 // System.out.println(jdbcTemplate.queryForList("select * from wxorder_order_express limit 1", 1L));
  27 +
  28 + String sql = " SELECT wo.customer_network_name as customerNetworkName,wo.order_sn AS orderSn, wog.`goods_name` AS goodsName, wog.`sku_name` AS skuName, wog.`goods_number` AS saleNumber,"
  29 + // woe.order_goods_id woe.order_goods_origin_id
  30 + + " (SELECT sum(woe.`goods_number`) from `wxorder_order_express` as woe WHERE woe.order_goods_id = wog.id and woe.tenant_id=16)as deliveryGoodsNumber, "
  31 + + " ww.name as wareName, ww.manager_nickname as wareManager, "
  32 + + " wc.name as channelName, "
  33 + + " wog.goods_base_sku_price as costSinglePrice, "
  34 + + " wc.manager_nickname as channelManager,"
  35 + + " (wog.money_paid-wog.refund_money) AS shouldReceiveMoney, "
  36 + + " wog.refund_money AS channelRefund, "
  37 + + " wog.ware_refund_money AS wareRefund, "
  38 + + " (wog.goods_base_sku_price*wog.goods_number+wog.ware_shipping_price) AS cost, "
  39 + + " wog.channel_goods_price as channelGoodsPrice, "
  40 + + " wog.money_paid as moneyPaid, "
  41 + + " wo.create_time as orderTime "
  42 + + " FROM `wxorder_order` wo "
  43 + + " INNER JOIN `wxorder_order_goods_origin` wog ON wo.id = wog.`order_id` and wo.tenant_id=16 and wog.tenant_id=16 "
  44 + + " INNER JOIN wxorder_channel as wc on wc.id = wo.channel_id "
  45 + + " INNER JOIN wxorder_ware as ww on ww.id=wo.ware_id "
  46 + + " INNER JOIN wxorder_channel_goods as wcg on wcg.id = wog.channel_goods_id"
  47 + + " limit 10 ";
  48 + JdbcTemplateWrapperTenant jdbcTemplate = context.getBean(JdbcTemplateWrapperTenant.class);
  49 + List<Map<String, Object>> data = jdbcTemplate.queryForList(sql, 16L);
  50 + System.out.println(data);
25 51 }
26 52 }
... ...
src/test/resources/application.properties
... ... @@ -13,9 +13,9 @@ spring.gson.serialize-nulls=true
13 13 spring.mvc.dispatch-options-request=true
14 14  
15 15 # shardingspere
16   -spring.shardingsphere.datasource.names=ds0,ds1
17   -spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
18   -spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
  16 +spring.shardingsphere.datasource.names=ds0
  17 +spring.shardingsphere.datasource.common.type=com.alibaba.druid.pool.DruidDataSource
  18 +spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
19 19 spring.shardingsphere.datasource.ds0.url=jdbc:mysql://rdsifmezqifmezqo.mysql.rds.aliyuncs.com:3306/bzyun_wxorder?characterEncoding=UTF-8
20 20 spring.shardingsphere.datasource.ds0.username=tylife
21 21 spring.shardingsphere.datasource.ds0.password=lexi365
... ... @@ -24,27 +24,27 @@ spring.shardingsphere.datasource.ds0.max-active=20
24 24 spring.shardingsphere.datasource.ds0.min-idle=5
25 25 spring.shardingsphere.datasource.ds0.max-wait=60000
26 26  
27   -spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
28   -spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
29   -spring.shardingsphere.datasource.ds1.url=jdbc:mysql://121.42.142.102:3306/bzyun_wxorder?characterEncoding=UTF-8
30   -spring.shardingsphere.datasource.ds1.username=dev
31   -spring.shardingsphere.datasource.ds1.password=taover02
32   -spring.shardingsphere.datasource.ds1.initial-size=10
33   -spring.shardingsphere.datasource.ds1.max-active=20
34   -spring.shardingsphere.datasource.ds1.min-idle=5
35   -spring.shardingsphere.datasource.ds1.max-wait=60000
36   -
37   -spring.shardingsphere.sharding.tables.wxorder_order.actual-data-nodes=ds0.wxorder_order,ds1.wxorder_order
38   -spring.shardingsphere.sharding.tables.wxorder_order.database-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingDatabaseAlgorithmHint
39   -spring.shardingsphere.sharding.tables.wxorder_order.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingTableAlgorithmHint
40   -spring.shardingsphere.sharding.tables.wxorder_order.key-generator.column=id
41   -spring.shardingsphere.sharding.tables.wxorder_order.key-generator.type=SNOWFLAKE-SELF
42   -
43   -spring.shardingsphere.sharding.tables.wxorder_order_goods.actual-data-nodes=ds0.wxorder_order_goods
44   -spring.shardingsphere.sharding.tables.wxorder_order_goods.database-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingDatabaseAlgorithmHint
45   -spring.shardingsphere.sharding.tables.wxorder_order_goods.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingTableAlgorithmHint
46   -spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.column=id
47   -spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.type=SNOWFLAKE-SELF
  27 +#spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
  28 +#spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  29 +#spring.shardingsphere.datasource.ds1.url=jdbc:mysql://121.42.142.102:3306/bzyun_wxorder?characterEncoding=UTF-8
  30 +#spring.shardingsphere.datasource.ds1.username=dev
  31 +#spring.shardingsphere.datasource.ds1.password=taover02
  32 +#spring.shardingsphere.datasource.ds1.initial-size=10
  33 +#spring.shardingsphere.datasource.ds1.max-active=20
  34 +#spring.shardingsphere.datasource.ds1.min-idle=5
  35 +#spring.shardingsphere.datasource.ds1.max-wait=60000
  36 +#
  37 +#spring.shardingsphere.sharding.tables.wxorder_order.actual-data-nodes=ds0.wxorder_order,ds1.wxorder_order
  38 +#spring.shardingsphere.sharding.tables.wxorder_order.database-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingDatabaseAlgorithmHint
  39 +#spring.shardingsphere.sharding.tables.wxorder_order.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingTableAlgorithmHint
  40 +#spring.shardingsphere.sharding.tables.wxorder_order.key-generator.column=id
  41 +#spring.shardingsphere.sharding.tables.wxorder_order.key-generator.type=SNOWFLAKE-SELF
  42 +#
  43 +#spring.shardingsphere.sharding.tables.wxorder_order_goods.actual-data-nodes=ds0.wxorder_order_goods
  44 +#spring.shardingsphere.sharding.tables.wxorder_order_goods.database-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingDatabaseAlgorithmHint
  45 +#spring.shardingsphere.sharding.tables.wxorder_order_goods.table-strategy.hint.algorithm-class-name=com.taover.repository.shardingsphere.ShardingTableAlgorithmHint
  46 +#spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.column=id
  47 +#spring.shardingsphere.sharding.tables.wxorder_order_goods.key-generator.type=SNOWFLAKE-SELF
48 48  
49 49 taover.sharding.workerId=1
50 50 taover.sharding.maxVibrationOffset=3
... ...