数据分库分表-Sharding-jdbc

Posted TGB-Earnest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据分库分表-Sharding-jdbc相关的知识,希望对你有一定的参考价值。

简介

随着业务的发展,我们的数据量会越来越大,这时候我们就要考虑分库分表了。

Demo

一、创建数据库

-- 创建数据库
CREATE DATABASE course_db;
-- 使用创建的数据库
USE course_db;
-- 设置字符集
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for course_1
-- ----------------------------
DROP TABLE IF EXISTS `course_1`;
CREATE TABLE `course_1`  (
  `cid` bigint(20) NOT NULL,
  `cname` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `cstatus` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for course_2
-- ----------------------------
DROP TABLE IF EXISTS `course_2`;
CREATE TABLE `course_2`  (
  `cid` bigint(20) NOT NULL,
  `cname` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `cstatus` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`cid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

二、建立POM

        <!--  mysql驱动  -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        <!--   MP 启动器     -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!--   Sharding-jdbc的spring-boot依赖   -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-core-common</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>
        <!--引入Knife4j的官方start包,Swagger2基于Springfox2.10.5项目-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <!--使用Swagger2-->
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

三、application.properties

# 应用名称
spring.application.name=shard
# 应用服务 WEB 访问端口
server.port=8080

spring.mvc.pathmatch.matching-strategy=ant-path-matcher

#数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/course_db?serverTimezone=UTC&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=stone&run

#需要加入下面的配值允许重载bean名称,主要用于后面sql对表的操作,MybatisPlus是根据类名作为表名的
spring.main.allow-bean-definition-overriding=true

#分片策略,多个数据源则用逗号隔开,例如: ds0,ds1
spring.shardingsphere.datasource.names=ds0
#配置数据源内容,下面的ds0 是上面设置的,因此需要同名
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driverClassName=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://127.0.0.1:3306/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=stone&run

#表的位置 在那个数据源(数据库),那个表。 下面的tables.course中的course是表名以什么开头
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds0.course_$->1..2

#指定course表里面主键cid的生成策略  SNOWFLAKE是雪花算法
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

#表的分片策略
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid

#表示加入到course_1或者course_2表中
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->cid % 2 +1

#打开sql输出日志
spring.shardingsphere.props.sql.show=true


四、编写代码

entity

@Data
public class Course 
    private Long cid;
    private String cname;
    private Long userId;
    private String cstatus;

mybatis

/**
 * <p> MybatisPlus配置类 </p>
 */
@Configuration
@MapperScan("com.zxd.shard.dao" )
public class MybatisPlusConfig 


五、测试


@SpringBootTest
class ShardApplicationTests 
    @Autowired
    public CourseMapper courseMapper;

    @Test
    void contextLoads() 
        Course course = new Course();
        course.setCname("java");
        course.setUserId(100L);
        course.setCstatus("Normal");
        courseMapper.insert(course);
    


总结

Demo 案例 https://gitee.com/zxdljb/springboot-demo.git

以上是关于数据分库分表-Sharding-jdbc的主要内容,如果未能解决你的问题,请参考以下文章

数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 路由之分库分表配置

数据量大了一定要分表,分库分表Sharding-JDBC入门与项目实战

Sharding-JDBC分库分表使用实例

分库分表sharding-jdbc实践—分库分表入门

数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 路由之分库分表路由

SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分库分表实践