【ShardingSphere】shardingjdbc入门案例-springboot整合shardingjdbc yml配置版

2,188 阅读2分钟

该教程仅仅适用于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其实并不难,难的地方在于对于各版本不同的了解
  • 碰到报错,先看看版本问题