SpringBoot使用Sharding-JDBC分庫分表
本文介紹SpringBoot使用當當Sharding-JDBC進行分庫分表。
1.有關Sharding-JDBC
有關Sharding-JDBC介紹這裏就不在多說,之前Sharding-JDBC是當當網自研的關系型數據庫的水平擴展框架,現在已經捐獻給Apache,具體可以查看Github,地址是:https://shardingsphere.apache.org/document/current/cn/overview/
shardingsphere文檔地址是:https://shardingsphere.apache.org/document/current/cn/overview/。
目前貌似還不能從Maven倉庫下載依賴,需要手動下載源碼打包使用,所以本文使用的還是當當網的依賴。
2.本文場景
2.1 數據庫
接下來介紹一下本文的場景,本文是分別創建了2個數據庫database0和database1。其中每個數據庫都創建了2個數據表,goods_0和goods_1,如圖所示。這裏藍色的代表database0中的表,紅色的代表database1中的表。綠色goods表是虛擬表(圖畫的比較醜,審美不好,湊合看吧)。
2.2 分庫
本文分庫樣例比較簡單,根據數據庫表中字段goods_id的大小進行判斷,如果goods_id大於20則使用database0,否則使用database1。
2.3 分表
分樣例比較簡單,根據數據庫表中字段goods_type的數值的奇偶進行判斷,奇數使用goods_1表,偶數使用goods_0表。
2.4 代碼流程
流程大致是這樣,在應用程序中我們操作虛擬表goods,但是當真正操作數據庫的時候,會根據我們的分庫分表規則進行匹配然後操作。
3.代碼實現
本文使用SpringBoot2.0.3,SpringData-JPA,Druid連接池,和當當的sharding-jdbc。
3.1 建表SQL
創建表和數據庫的SQL如下所示。
CREATE DATABASE database0; USE database0; DROP TABLE IF EXISTS `goods_0`; CREATE TABLE `goods_0` ( `goods_id` bigint(20) NOT NULL, `goods_name` varchar(100) COLLATE utf8_bin NOT NULL, `goods_type` bigint(20) DEFAULT NULL, PRIMARY KEY (`goods_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `goods_1`; CREATE TABLE `goods_1` ( `goods_id` bigint(20) NOT NULL, `goods_name` varchar(100) COLLATE utf8_bin NOT NULL, `goods_type` bigint(20) DEFAULT NULL, PRIMARY KEY (`goods_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; CREATE DATABASE database1; USE database1; DROP TABLE IF EXISTS `goods_0`; CREATE TABLE `goods_0` ( `goods_id` bigint(20) NOT NULL, `goods_name` varchar(100) COLLATE utf8_bin NOT NULL, `goods_type` bigint(20) DEFAULT NULL, PRIMARY KEY (`goods_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; DROP TABLE IF EXISTS `goods_1`; CREATE TABLE `goods_1` ( `goods_id` bigint(20) NOT NULL, `goods_name` varchar(100) COLLATE utf8_bin NOT NULL, `goods_type` bigint(20) DEFAULT NULL, PRIMARY KEY (`goods_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
3.2 依賴文件
新建項目,加入當當的sharding-jdbc-core依賴和druid連接池,完整pom如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dalaoyang</groupId>
<artifactId>springboot2_shardingjdbc_fkfb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot2_shardingjdbc_fkfb</name>
<description>springboot2_shardingjdbc_fkfb</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<!-- sharding-jdbc -->
<dependency>
<groupId>com.dangdang</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>1.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.3 配置信息
在配置信息中配置了兩個數據庫的信息和JPA的簡單配置。
##Jpa配置
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
##數據庫配置
##數據庫database0地址
database0.url=jdbc:mysql://localhost:3306/database0?characterEncoding=utf8&useSSL=false
##數據庫database0用戶名
database0.username=root
##數據庫database0密碼
database0.password=root
##數據庫database0驅動
database0.driverClassName=com.mysql.jdbc.Driver
##數據庫database0名稱
database0.databaseName=database0
##數據庫database1地址
database1.url=jdbc:mysql://localhost:3306/database1?characterEncoding=utf8&useSSL=false
##數據庫database1用戶名
database1.username=root
##數據庫database1密碼
database1.password=root
##數據庫database1驅動
database1.driverClassName=com.mysql.jdbc.Driver
##數據庫database1名稱
database1.databaseName=database1
3.4 啟動類
啟動類加入了@EnableAutoConfiguration取出數據庫自動配置,使用@EnableTransactionManagement開啟事務,使用@EnableConfigurationProperties註解加入配置實體,啟動類完整代碼請入所示。
package com.dalaoyang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableTransactionManagement(proxyTargetClass = true)
@EnableConfigurationProperties
public class Springboot2ShardingjdbcFkfbApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2ShardingjdbcFkfbApplication.class, args);
}
}
3.5 實體類和數據庫操作層
這裏沒什麽好說的,就是簡單的實體和Repository,只不過在Repository內加入between方法和in方法用於測試,代碼如下所示。
Goods實體類。
package com.dalaoyang.entity;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author yangyang
* @date 2019/1/29
*/
@Entity
@Table(name="goods")
@Data
public class Goods {
@Id
private Long goodsId;
private String goodsName;
private Long goodsType;
}
GoodsRepository類。
package com.dalaoyang.repository;
import com.dalaoyang.entity.Goods;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* @author yangyang
* @date 2019/1/29
*/
public interface GoodsRepository extends JpaRepository<Goods, Long> {
List<Goods> findAllByGoodsIdBetween(Long goodsId1,Long goodsId2);
List<Goods> findAllByGoodsIdIn(List<Long> goodsIds);
}
3.6 數據庫配置
本文使用了兩個實體來接收數據庫信息,並且創建數據源,也可以采用別的方式。首先看一下Database0Config和Database1Config兩個類的代碼。
Database0Config類。
package com.dalaoyang.database;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* @author yangyang
* @date 2019/1/30
*/
@Data
@ConfigurationProperties(prefix = "database0")
@Component
public class Database0Config {
private String url;
private String username;
private String password;
private String driverClassName;
private String databaseName;
public DataSource createDataSource() {
DruidDataSource result = new DruidDataSource();
result.setDriverClassName(getDriverClassName());
result.setUrl(getUrl());
result.setUsername(getUsername());
result.setPassword(getPassword());
return result;
}
}
Database1Config類。
package com.dalaoyang.database;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
/**
* @author yangyang
* @date 2019/1/30
*/
@Data
@ConfigurationProperties(prefix = "database1")
@Component
public class Database1Config {
private String url;
private String username;
private String password;
private String driverClassName;
private String databaseName;
public DataSource createDataSource() {
DruidDataSource result = new DruidDataSource();
result.setDriverClassName(getDriverClassName());
result.setUrl(getUrl());
result.setUsername(getUsername());
result.setPassword(getPassword());
return result;
}
}
接下來新建DataSourceConfig用於創建數據源和使用分庫分表策略,其中分庫分表策略會調用分庫算法類和分表算法類,DataSourceConfig類代碼如下所示。
package com.dalaoyang.database;
import com.dalaoyang.config.DatabaseShardingAlgorithm;
import com.dalaoyang.config.TableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.keygen.DefaultKeyGenerator;
import com.dangdang.ddframe.rdb.sharding.keygen.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author yangyang
* @date 2019/1/29
*/
@Configuration
public class DataSourceConfig {
@Autowired
private Database0Config database0Config;
@Autowired
private Database1Config database1Config;
@Autowired
private DatabaseShardingAlgorithm databaseShardingAlgorithm;
@Autowired
private TableShardingAlgorithm tableShardingAlgorithm;
@Bean
public DataSource getDataSource() throws SQLException {
return buildDataSource();
}
private DataSource buildDataSource() throws SQLException {
//分庫設置
Map<String, DataSource> dataSourceMap = new HashMap<>(2);
//添加兩個數據庫database0和database1
dataSourceMap.put(database0Config.getDatabaseName(), database0Config.createDataSource());
dataSourceMap.put(database1Config.getDatabaseName(), database1Config.createDataSource());
//設置默認數據庫
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap, database0Config.getDatabaseName());
//分表設置,大致思想就是將查詢虛擬表Goods根據一定規則映射到真實表中去
TableRule orderTableRule = TableRule.builder("goods")
.actualTables(Arrays.asList("goods_0", "goods_1"))
.dataSourceRule(dataSourceRule)
.build();
//分庫分表策略
ShardingRule shardingRule = ShardingRule.builder()
.dataSourceRule(dataSourceRule)
.tableRules(Arrays.asList(orderTableRule))
.databaseShardingStrategy(new DatabaseShardingStrategy("goods_id", databaseShardingAlgorithm))
.tableShardingStrategy(new TableShardingStrategy("goods_type", tableShardingAlgorithm)).build();
DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
return dataSource;
}
@Bean
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
}
}
3.7 分庫分表算法
由於這裏只是簡單的分庫分表樣例,所以分庫類這裏實現SingleKeyDatabaseShardingAlgorithm類,采用了單分片鍵數據源分片算法,需要重寫三個方法,分別是:
- doEqualSharding:SQL中==的規則。
- doInSharding:SQL中in的規則。
- doBetweenSharding:SQL中between的規則。
本文分庫規則是基於值大於20則使用database0,其余使用database1,所以簡單if,else就搞定了,分庫算法類DatabaseShardingAlgorithm代碼如下所示。
package com.dalaoyang.config;
import com.dalaoyang.database.Database0Config;
import com.dalaoyang.database.Database1Config;
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.SingleKeyDatabaseShardingAlgorithm;
import com.google.common.collect.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.LinkedHashSet;
/**
* 這裏使用的都是單鍵分片策略
* 示例分庫策略是:
* GoodsId<=20使用database0庫
* 其余使用database1庫
* @author yangyang
* @date 2019/1/30
*/
@Component
public class DatabaseShardingAlgorithm implements SingleKeyDatabaseShardingAlgorithm<Long> {
@Autowired
private Database0Config database0Config;
@Autowired
private Database1Config database1Config;
@Override
public String doEqualSharding(Collection<String> availableTargetNames, ShardingValue<Long> shardingValue) {
Long value = shardingValue.getValue();
if (value <= 20L) {
return database0Config.getDatabaseName();
} else {
return database1Config.getDatabaseName();
}
}
@Override
public Collection<String> doInSharding(Collection<String> availableTargetNames, ShardingValue<Long> shardingValue) {
Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
for (Long value : shardingValue.getValues()) {
if (value <= 20L) {
result.add(database0Config.getDatabaseName());
} else {
result.add(database1Config.getDatabaseName());
}
}
return result;
}
@Override
public Collection<String> doBetweenSharding(Collection<String> availableTargetNames,
ShardingValue<Long> shardingValue) {
Collection<String> result = new LinkedHashSet<>(availableTargetNames.size());
Range<Long> range = shardingValue.getValueRange();
for (Long value = range.lowerEndpoint(); value <= range.upperEndpoint(); value++) {
if (value <= 20L) {
result.add(database0Config.getDatabaseName());
} else {
result.add(database1Config.getDatabaseName());
}
}
return result;
}
}
分表和分庫類似,無非就是實現的類不一樣,實現了SingleKeyTableShardingAlgorithm類,策略使用值奇偶分表,分表算法類TableShardingAlgorithm如代碼清單所示。
package com.dalaoyang.config;
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import com.google.common.collect.Range;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.LinkedHashSet;
/**
* 這裏使用的都是單鍵分片策略
* 示例分表策略是:
* GoodsType為奇數使用goods_1表
* GoodsType為偶數使用goods_0表
* @author yangyang
* @date 2019/1/30
*/
@Component
public class TableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<Long> {
@Override
public String doEqualSharding(final Collection<String> tableNames, final ShardingValue<Long> shardingValue) {
for (String each : tableNames) {
if (each.endsWith(shardingValue.getValue() % 2 + "")) {
return each;
}
}
throw new IllegalArgumentException();
}
@Override
public Collection<String> doInSharding(final Collection<String> tableNames, final ShardingValue<Long> shardingValue) {
Collection<String> result = new LinkedHashSet<>(tableNames.size());
for (Long value : shardingValue.getValues()) {
for (String tableName : tableNames) {
if (tableName.endsWith(value % 2 + "")) {
result.add(tableName);
}
}
}
return result;
}
@Override
public Collection<String> doBetweenSharding(final Collection<String> tableNames,
final ShardingValue<Long> shardingValue) {
Collection<String> result = new LinkedHashSet<>(tableNames.size());
Range<Long> range = shardingValue.getValueRange();
for (Long i = range.lowerEndpoint(); i <= range.upperEndpoint(); i++) {
for (String each : tableNames) {
if (each.endsWith(i % 2 + "")) {
result.add(each);
}
}
}
return result;
}
}
3.8 Controller
接下來創建一個Controller進行測試,保存方法使用了插入40條數據,根據我們的規則,會每個庫插入20條,同時我這裏還創建了三個查詢方法,分別是查詢全部,between查詢,in查詢,還有刪除全部方法。Controller類代碼如下所示。
package com.dalaoyang.controller;
import com.dalaoyang.entity.Goods;
import com.dalaoyang.repository.GoodsRepository;
import com.dangdang.ddframe.rdb.sharding.keygen.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @author yangyang
* @date 2019/1/29
*/
@RestController
public class GoodsController {
@Autowired
private KeyGenerator keyGenerator;
@Autowired
private GoodsRepository goodsRepository;
@GetMapping("save")
public String save(){
for(int i= 1 ; i <= 40 ; i ++){
Goods goods = new Goods();
goods.setGoodsId((long) i);
goods.setGoodsName( "shangpin" + i);
goods.setGoodsType((long) (i+1));
goodsRepository.save(goods);
}
return "success";
}
@GetMapping("select")
public String select(){
return goodsRepository.findAll().toString();
}
@GetMapping("delete")
public void delete(){
goodsRepository.deleteAll();
}
@GetMapping("query1")
public Object query1(){
return goodsRepository.findAllByGoodsIdBetween(10L, 30L);
}
@GetMapping("query2")
public Object query2(){
List<Long> goodsIds = new ArrayList<>();
goodsIds.add(10L);
goodsIds.add(15L);
goodsIds.add(20L);
goodsIds.add(25L);
return goodsRepository.findAllByGoodsIdIn(goodsIds);
}
}
4.測試
啟動應用,在瀏覽器或HTTP請求工具訪問http://localhost:8080/save,如圖所示,返回success。
接下來在測試一下查詢方法,訪問http://localhost:8080/select,如圖所示,可以看到插入數據沒問題。
然後查看一下數據庫,首先看database0,如圖,每個表都有十條數據,如下所示。
接下來看database1,如下所示。
從上面幾張圖可以看出分庫分表已經按照我們的策略來進行插入,至於其他幾個測試這裏就不做介紹了,無論是查詢和刪除都是可以成功的。
5 源碼
源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_shardingjdbc_fkfb
SpringBoot使用Sharding-JDBC分庫分表