好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

利用Sharding-Jdbc组件实现分表

看到了当当开源的sharding-jdbc组件,它可以在几乎不修改代码的情况下完成分库 分表 的实现。摘抄其中一段介绍:

sharding-jdbc直接封装jdbc api,可以理解为增强版的jdbc驱动,旧代码迁移成本几乎为零:

可适用于任何基于java的orm框架,如:jpa, hibernate, mybatis, spring jdbc template或直接使用jdbc。 可基于任何第三方的数据库连接池,如:dbcp, c3p0, bonecp, druid等。 理论上可支持任意实现jdbc规范的数据库。虽然目前仅支持mysql,但已有支持oracle,sqlserver,db2等数据库的计划。

先做一个最简单的试用,不做分库,仅做分表。选择数据表bead_information,首先复制成三个表:bead_information_0、bead_information_1、bead_information_2

测试实现过程

前提:已经实现srping+mybatis对单库单表做增删改查的项目。

1、修改pom.xml增加dependency

?

1

2

3

4

5

6

7

8

9

10

<dependency>

   <groupid>com.dangdang</groupid>

   <artifactid>sharding-jdbc-core</artifactid>

   <version> 1.4 . 2 </version>

  </dependency>

  <dependency>

   <groupid>com.dangdang</groupid>

   <artifactid>sharding-jdbc-config-spring</artifactid>

   <version> 1.4 . 0 </version>

</dependency>

2、新建一个sharding-jdbc.xml文件,实现分库分表的配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<?xml version= "1.0" encoding= "utf-8" ?>

<beans xmlns= "http://www.springframework.org/schema/beans"

  xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"

  xmlns:context= "http://www.springframework.org/schema/context"

  xmlns:tx= "http://www.springframework.org/schema/tx"

  xmlns:rdb= "http://www.dangdang.com/schema/ddframe/rdb"

  xsi:schemalocation="http: //www.springframework.org/schema/beans

    http: //www.springframework.org/schema/beans/spring-beans.xsd

    http: //www.springframework.org/schema/tx

    http: //www.springframework.org/schema/tx/spring-tx.xsd

    http: //www.springframework.org/schema/context

    http: //www.springframework.org/schema/context/spring-context.xsd

    http: //www.dangdang.com/schema/ddframe/rdb

    http: //www.dangdang.com/schema/ddframe/rdb/rdb.xsd">

 

 

 

  <!-- 配置数据源 -->

  <bean name= "datasource" class = "com.alibaba.druid.pool.druiddatasource" init-method= "init" destroy-method= "close" >

  <property name= "url" value= "jdbc:mysql://localhost:3306/beadhouse" />

  <property name= "username" value= "root" />

  <property name= "password" value= "123456" />

  </bean>

   

  <rdb:strategy id= "tableshardingstrategy" sharding-columns= "id" algorithm- class = "com.springdemo.utill.membersinglekeytableshardingalgorithm" />

 

  <rdb:data-source id= "shardingdatasource" >

  <rdb:sharding-rule data-sources= "datasource" >

   <rdb:table-rules>

   <rdb:table-rule logic-table= "bead_information" actual-tables= "bead_information_${0..2}" table-strategy= "tableshardingstrategy" />

   </rdb:table-rules>

  </rdb:sharding-rule>

  </rdb:data-source>

 

  <bean id= "transactionmanager" class = "org.springframework.jdbc.datasource.datasourcetransactionmanager" >

  <property name= "datasource" ref= "shardingdatasource" />

  </bean>

</beans>

3、将文件引入spring配置文件中。

需要修改几个地方,把sqlsessionfactory和transactionmanager原来关联的datasource统一修改为shardingdatasource(这一步作用就是把数据源全部托管给sharding去管理)

4、实现分表(分库)逻辑,我们的分表逻辑类需要实现singlekeytableshardingalgorithm接口的三个方法dobetweensharding、doequalsharding、doinsharding

(取模除数需要按照自己需求改变,我这里分3个表,所以除以3)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

import java.util.collection;

import java.util.linkedhashset;

import com.dangdang.ddframe.rdb.sharding.api.shardingvalue;

import com.dangdang.ddframe.rdb.sharding.api.strategy.table.singlekeytableshardingalgorithm;

import com.google.common.collect.range;

public class membersinglekeytableshardingalgorithm implements singlekeytableshardingalgorithm<integer> {

 

  @override

  public collection<string> dobetweensharding(collection<string> tablenames, shardingvalue<integer> shardingvalue) {

  collection<string> result = new linkedhashset<string>(tablenames.size());

  range<integer> range = (range<integer>) shardingvalue.getvaluerange();

  for (integer i = range.lowerendpoint(); i <= range.upperendpoint(); i++) {

   integer modvalue = i % 3 ;

   string modstr = modvalue < 3 ? "" + modvalue : modvalue.tostring();

   for (string each : tablenames) {

   if (each.endswith(modstr)) {

    result.add(each);

   }

   }

  }

  return result;

  }

  @override

  public string doequalsharding(collection<string> tablenames, shardingvalue<integer> shardingvalue) {

  integer modvalue = shardingvalue.getvalue() % 3 ;

  string modstr = modvalue < 3 ? "" + modvalue : modvalue.tostring();

  for (string each : tablenames) {

   if (each.endswith(modstr)) {

   return each;

   }

  }

  throw new illegalargumentexception();

  }

  @override

  public collection<string> doinsharding(collection<string> tablenames, shardingvalue<integer> shardingvalue) {

  collection<string> result = new linkedhashset<string>(tablenames.size());

  for (integer value : shardingvalue.getvalues()) {

   integer modvalue = value % 3 ;

   string modstr = modvalue < 3 ? "" + modvalue : modvalue.tostring();

   for (string tablename : tablenames) {

   if (tablename.endswith(modstr)) {

    result.add(tablename);

   }

   }

  }

  return result;

  }

}

5、配置完成,可以实现增删改查测试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://www.cnblogs.com/huangheng01/p/9366325.html

查看更多关于利用Sharding-Jdbc组件实现分表的详细内容...

  阅读:45次