您好,登錄后才能下訂單哦!
看到了當(dāng)當(dāng)開(kāi)源的Sharding-JDBC組件,它可以在幾乎不修改代碼的情況下完成分庫(kù)分表的實(shí)現(xiàn)。摘抄其中一段介紹:
Sharding-JDBC直接封裝JDBC API,可以理解為增強(qiáng)版的JDBC驅(qū)動(dòng),舊代碼遷移成本幾乎為零:
先做一個(gè)最簡(jiǎn)單的試用,不做分庫(kù),僅做分表。選擇數(shù)據(jù)表bead_information,首先復(fù)制成三個(gè)表:bead_information_0、bead_information_1、bead_information_2
測(cè)試實(shí)現(xiàn)過(guò)程
前提:已經(jīng)實(shí)現(xiàn)srping+mybatis對(duì)單庫(kù)單表做增刪改查的項(xiàng)目。
1、修改pom.xml增加dependency
<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、新建一個(gè)sharding-jdbc.xml文件,實(shí)現(xiàn)分庫(kù)分表的配置
<?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"> <!-- 配置數(shù)據(jù)源 --> <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配置文件中。
需要修改幾個(gè)地方,把sqlSessionFactory和transactionManager原來(lái)關(guān)聯(lián)的dataSource統(tǒng)一修改為shardingDataSource(這一步作用就是把數(shù)據(jù)源全部托管給sharding去管理)
4、實(shí)現(xiàn)分表(分庫(kù))邏輯,我們的分表邏輯類(lèi)需要實(shí)現(xiàn)SingleKeyTableShardingAlgorithm接口的三個(gè)方法doBetweenSharding、doEqualSharding、doInSharding
(取模除數(shù)需要按照自己需求改變,我這里分3個(gè)表,所以除以3)
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、配置完成,可以實(shí)現(xiàn)增刪改查測(cè)試。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。