該教程僅僅適用於4.x版本,在ShardingSphere的迭代歷史中很多的配置和兼容問題很大,這里入手一定要注意版本。
構建一個SpringBoot項目
SpringBoot項目的構建這里不再贅述,這里要提及的一點就是我們構建的時候,基本不需要引入依賴,后面會一步一步加入
數據庫准備
- 構建兩個庫,庫名安裝ds0,ds1來定義
- 數據庫內建立t_order1,t_order2兩個表,表結構一致,只是名字用數字排序
對應SQL如下:
DROP TABLE IF EXISTS `t_order1`;
CREATE TABLE `t_order1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`cloumn` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `t_order2`;
CREATE TABLE `t_order2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`cloumn` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
完成基本的數據庫和工程搭建之后,我們就可以來完成我們的整合sharding jdbc啦
引入shardingsphere和HikariCP連接池
這里引入的sharding sphere是4.1.1,版本問題一定要注意,不然后面可能沒有辦法成功。除了引入sharding sphere這里還引入了web,方便編寫接口來調用。
具體POM文件如下:
<properties>
<java.version>1.8</java.version>
<sharding-sphere.version>4.1.1</sharding-sphere.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
配置表和庫的策略
這里主要的功能是完成數據庫的分片分表,而且由於是示例,這里也僅僅只寫一個表
具體的application.properties配置文件內容如下:
server.port=10080
spring.shardingsphere.datasource.names=ds0,ds1
# 配置第一個數據庫
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=
# 配置第二個數據庫
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/ds1
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=
# 配置分庫策略
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}
# 配置分表策略
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
編寫測試接口
到這里基本的操作都已經完成了, 我們接下來就能使用sharding jdbc來完成開發了。所以接下來我們寫點代碼看看能不能獲取到對應的數據庫了解吧
package com.echo.shardingjdbc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @author tang.sl
*/
@RestController
@RequestMapping(value = "/test")
public class Test {
@Resource
private DataSource dataSource;
@GetMapping(value = "/test")
public String test() throws SQLException {
Connection connection = dataSource.getConnection();
System.out.println(connection);
return "success";
}
}
測試
啟動我們的項目,然后訪問我們寫得接口,我們可以在控制台看到如下內容
總結
- 整合sharding sphere其實並不難,難的地方在於對於各版本不同的了解
- 碰到報錯,先看看版本問題