Mysql利用ShardingSphereJDBC进行分库分表

Mysql分库分表

分库分表是为了解决数据量过大而导致数据库性能降低的问题,将原来独立的数据库拆分成若干个数据库组成,将数据库大表拆分成多个数据库表组成,使单一的数据库,单一的数据表的内容变小,从而达到提升数据库性能的目的

分库分表的方式

分库分表分为分库和分表两个部分,而这两个部分都可以统称为数据分片,其目的都是讲数据库分成不同的存储单员,另外从分拆的角度上可以分为垂直分片和水平分片

  • 垂直分片:按照业务来对数据进行分片,又称为纵向分片。他的核心理念就是转库专用。在拆分之前,一个数据库由多个数据表组成,每个表对应不同的业务。 而拆分之后,则是按照业务将表进行归类,分布到不同的数据库或表中,从而将压力分散至不同的数据库或表
  • 水平分片:又称横向分片。相对于垂直分片,它不再将数据根据业务逻辑分类,而是通过某个字段(或某几个字段),根据某种规则将数据分散至多个库或表中,每个分片仅包含数据的一部分。

Sharding-JDBC快速实战

核心概念:

  • 逻辑表:水平拆分的数据库的相同逻辑和数据结构表的总称 (就是一个虚表,用来操作已经分开的数据库表文件)
  • 真实表:在分片的数据库中真实存在的物理表。
  • 数据节点:数据分片的最小单元。由数据源名称和数据表组成
  • 绑定表:分片规则一致的主表和子表。
  • 广播表:也叫公共表,指素有的分片数据源中都存在的表,表结构和表中的数据在每个数据库中都完全一致。例如字典表。
  • 分片键:用于分片的数据库字段,是将数据库(表)进行水平拆分的关键字段。SQL中若没有分片字段,将会执行全路由,性能会很差。
  • 分片算法:通过分片算法将数据进行分片,支持通过=、BETWEEN和IN分片。分片算法需要由应用开发者自行实现,可实现的灵活度非常高。
  • 分片策略:真正用于进行分片操作的是分片键+分片算法,也就是分片策略。在 ShardingJDBC中一般采用基于Groovy表达式的inline分片策略

快速实战

maven

    <!--数据库 连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.22</version>
        </dependency>
    <!--ShardingSphere  依赖-->
      <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>

分表不分库

application.propertise

#配置一个数据源
spring.shardingsphere.datasource.names=m1
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root


#配置绑定表 course表两个真实表的分布情况  m1数据源内的 course1   course2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{1..2}

# 配置 cid这个列需要自动生成 type采用雪花算法(推特的算法)   props配置算法 可以不用配置 有默认
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1

#表策略 分片建采用cid   分片算法采用后面的表达式
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid%2+1}

#sql执行语句打印
spring.shardingsphere.props.sql.show=true
spring.main.allow-bean-definition-overriding=true

分库分表配置

#配置第一个数据源 名字随意起 m1
spring.shardingsphere.datasource.names=m1,m2
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#配置第二个数据源 名字是m2
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/coursedb_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root

#配置绑定表 course表两个真实表的分布情况  m1数据源内的 course1   course2 或m2 内的course_1 _2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2}

# 配置 cid这个列需要自动生成 type采用雪花算法(推特的算法)   props配置算法 可以不用配置 有默认
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1

#表策略 分片建采用cid   分片算法采用后面的表达式 
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid%2+1}
#分库策略 根据cid分库 分片算法是cid按2取模1就去m1数据库 2就去m2数据库 
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{cid%2+1}

#sql执行语句打印
spring.shardingsphere.props.sql.show=true
spring.main.allow-bean-definition-overriding=true

Sharding-JDBC读写分离

spring.shardingsphere.datasource.names=m1,m2
#主库
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/coursedb?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#从库
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.drive-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/coursedb_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root

#配置 主从规则 ds0是规则名字  主库的数据源 
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=m1
#配置 主从规则  从库的数据源
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names[0]=m2
#配置数据表的真实表分布情况
spring.shardingsphere.sharding.tables.t_dict.actual-data-nodes=ds0.t_dist


# 配置 cid这个列需要自动生成 type采用雪花算法(推特的算法)   props配置算法 可以不用配置 有默认
spring.shardingsphere.sharding.tables.t_dict.key-generator.column=t_id
spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_dict.key-generator.props.worker.id=1
##绑定表
#spring.shardingsphere.sharding.binding-tables[0]=t_user,t_dict

#sql执行语句打印
spring.shardingsphere.props.sql.show=true
spring.main.allow-bean-definition-overriding=true
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目前分库分表的必要性:由于在做消息中心,消息中心的量级又比较大,目前大概有9000W用户,差不多每天200W~50...
    名字是乱打的阅读 1,459评论 2 11
  • 1 传统项目结构 2 数据库性能瓶颈 ① 数据库连接数据库连接是非常稀少的资源,MySQL数据库默认100个连接,...
    MiniSoulBigBang阅读 594评论 0 1
  • MySQL数据库之互联网常用分库分表方案 mysql如何进行跨实例关联查询? 分布式数据库中间件、产品——shar...
    临风2020阅读 179评论 0 0
  • 1、前言 在互联网还未崛起的时代,我们的传统应用都有这样一个特点:访问量和数据量都比较小,单库单表完全可以支撑整个...
    代码的搬运工阅读 5,268评论 0 1
  • 分库分表原因 前文介绍MySQL主从模式,将读写分离以提高性能。 主从模式对于写少读多的场景确实非常大的优势,但是...
    kyo1992阅读 2,035评论 0 11