当前位置:   article > 正文

SpringBoot整合ShardingSphere-JDBC实现分库分表(根据年分库,根据月分表)_shardingsphere-jdbc 5.3.0根据年份自动分表

shardingsphere-jdbc 5.3.0根据年份自动分表

SpringBoot整合ShardingSphere-JDBC实现分库分表(根据年分库,根据月分表)

一、说明

1、仅演示如何使用,具体相关详细知识点可参照官网介绍 (附:官网地址:https://shardingsphere.apache.org/)

2、该分库分表为静态分库分表,即提前生成多个数据源,如果需要添加租户,则需要停机新增数据源(动态新增租户在整理中)

二、需求

1、以关系型数据库MySQL存储所有数据,实现一年一个库,每张存储相关数据的表为每月一张,但涉及到的用户表、角色表等系统表不分表

2、可根据年月动态生成数据源,且系统需不要停机重启,自动增加租户数据源信息(学习阶段,暂未实现,暂时以静态生成,需要停机新增或减少数据源)

三、产品选择

1、阿里巴巴:MyCat (需要安装使用)

2、当当网:ShardingSphere (推荐使用,以jar包的方式引用)(官网地址:https://shardingsphere.apache.org/)

四、项目版本

说明:只提供了分库分表所关联的产品版本,该项目为聚合多模块项目

1、Java:1.8

2、SpringBoot:2.2.8.RELEASE

3、MySQL:8.0

4、Sharding-Jdbc:4.0.0-RC1

五、安装与使用

说明:
1、Java、MySQL、SpringBoot项目创建省略,仅说明分库分表相关的配置
2、MySQL提前将多个库和数据表生成好

5.1、pom.xml中引入依赖

<!-- shardingJDBC-->
<dependency>
	<groupId>org.apache.shardingsphere</groupId>
	<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
	<version>4.0.0-RC1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5.2、数据源/数据分片配置(该项目采用yaml配置)

配置一:application.yml

说明:管理哪个配置文件生效

# 配置文件环境配置
# dev:开发环境
# test:测试环境
# pro:生产环境
spring:
  profiles:
    include: dev,sharding_jdbc
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

配置二:application-dev.yml

说明:仅展示mybatis和解决bean重命问题的配置,其他配置未展现

spring:
  main:
    allow-bean-definition-overriding: true
mybatis:
  type-aliases-package: com.hxlinks.**.model.*
  mapper-locations: classpath*:/com/hxlinks/*/mapper/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

配置三:application-sharding_jdbc.yml

说明:分库分表配置

spring:
  shardingsphere:
    datasource:
      # 定义数据源
      names: network,network2022,network2023
      # 默认数据源
      network:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1/network?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&allowMultiQueries=TRUE
        username: root
        password: '123456'
      # 2022年数据源
      network2022:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1/network2022?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&allowMultiQueries=TRUE
        username: root
        password: '123456'
      # 2023年数据源
      network2023:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1/network2023?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&allowMultiQueries=TRUE
        username: root
        password: '123456'
    sharding:
      # 默认数据源,未分片的表默认执行库
      default-data-source-name: network
      # 分库分表
      tables:
      # 其中一个要分表的表名,逻辑表名
        test:
        # 分库
          database-strategy:
            inline:
           	 # 以表中的哪个字段
              sharding-column: year
              algorithm-expression: network$->{year}
          # 分片节点
          actual-data-nodes: network$->{2022..2023}.test$->{01..12}
          # 分表
          table-strategy:
            inline:
           	 # 以表中的哪个字段
              sharding-column: month
              algorithm-expression: test$->{month}
          # 主键
          key-generator:
            column: id
            type: SNOWFLAKE
    props:
      # 打开sql输出日志
      sql:
        show: true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

5.3、代码编写

说明:代码和未分库分表之前一样,不需要写真实表名(test01、test02),还是逻辑表名(test)

5.4、测试结果展示

5.4.1:不参与分片的数据表

在这里插入图片描述

5.4.2:参与分片的数据表

在这里插入图片描述

5.4.3:数据库展示

在这里插入图片描述

六、可能出现的问题以及解决方案

6.1:下划线问题

错误提示:

***************************
APPLICATION FAILED TO START
***************************

Description:

Configuration property name 'spring.shardingsphere.datasource.network_2022' is not valid:

    Invalid characters: '_'
    Bean: testController
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

Action:

Modify 'spring.shardingsphere.datasource.network_2022' so that it conforms to the canonical names requirements.


Process finished with exit code 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

报错原因:

​ sharding-jdbc版本问题,高版本可能出现不可以使用 “_” 下划线定义逻辑表名报错的问题,推荐用 “-” 这种横线

解决方案:

​ 不使用 “_” 或改为"-"

6.2:数据源URL问题

错误提示:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

报错原因

​ 是sharding-jdbc-4.1.1最新版本spring boot配置类存在问题导致。

解决方案

说明:解决方案有多种,使用最简单的一种

​ 1、降低sharding-jdbc 版本,由4.1.1 修改为 4.0.0-RC1

​ 2、重新写一个配置类,在自己项目中,可以跟sharding-jdbc-4.1.1最新版本spring boot配置类内容一致(没有去实现,该方式较麻烦)

6.3:有的表不需要参与分库分表,该怎么解决

问题描述

​ 我数据库中只需要分数据量较大的数据表,配置、用户、角色等数据量不大的表该如何处理

解决方案

​ 配置默认数据源,可参考上面的数据源/分片配置

    sharding:
      # 默认数据源,未分片的表默认执行库
      default-data-source-name: network
  • 1
  • 2
  • 3

七、参考文献

官方文档:
	https://shardingsphere.apache.org/document/current/cn/overview/
其他博主文档:
	https://www.proyy.com/6971978161038819341.html
	https://blog.csdn.net/weixin_47077809/article/details/113694604
	https://blog.csdn.net/weixin_42592282/article/details/124476024
	https://blog.csdn.net/yulewo123/article/details/116611372
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/756889
推荐阅读
相关标签
  

闽ICP备14008679号