赞
踩
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提前将多个库和数据表生成好
<!-- shardingJDBC-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
配置一:application.yml
说明:管理哪个配置文件生效
# 配置文件环境配置
# dev:开发环境
# test:测试环境
# pro:生产环境
spring:
profiles:
include: dev,sharding_jdbc
配置二: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
配置三: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
说明:代码和未分库分表之前一样,不需要写真实表名(test01、test02),还是逻辑表名(test)
5.4.1:不参与分片的数据表
5.4.2:参与分片的数据表
5.4.3:数据库展示
错误提示:
*************************** 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
报错原因:
sharding-jdbc版本问题,高版本可能出现不可以使用 “_” 下划线定义逻辑表名报错的问题,推荐用 “-” 这种横线
解决方案:
不使用 “_” 或改为"-"
错误提示:
*************************** 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
报错原因:
是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配置类内容一致(没有去实现,该方式较麻烦)
问题描述:
我数据库中只需要分数据量较大的数据表,配置、用户、角色等数据量不大的表该如何处理
解决方案:
配置默认数据源,可参考上面的数据源/分片配置
sharding:
# 默认数据源,未分片的表默认执行库
default-data-source-name: network
官方文档:
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
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。