Sharding-JDBC:垂直拆分怎么做?

原创: 尹吉欢 猿天地 今天

经过读写分离的优化后,小王可算是轻松了一段时间,读写分离具体的方案请查看这篇文章:Sharding-JDBC:查询量大如何优化?

可是好景不长,业务发展是在太快了。数据库中的数据量猛增,由于所有表都在一个数据库中,导致服务器本地存储快满了。

从上图我们可以看的出来,由于表的数量较多,每个表的数据量也较大,但是还没到水平拆分的地步。目前遇到的问题是服务器的存储不够了,短期内还不用水平拆分,那么方案呼之欲出了:垂直拆分。

解释下什么是垂直拆分?

我们都知道,一个数据库它是由N张表构成,每个表存储的数据都不一样,都对应着各自的业务。

所谓的垂直切分其实就是分类存储,大部分都是按业务类型进行分类。相同的类型存储在相同的库上,不同的类型存储在不同的库上,这样也就将数据或者说压力分担到不同的库上面 。

比如我们可以将用户相关的放一起,订单相关的放一起,行为日志相关的放一起,依次来推下去。

  • 优点:

拆分之后业务规划清晰,数据维护简单,分担了数据集中存储的压力。

  • 缺点:

缺点也很明显,多表join查询无法实现,只能通过接口方式解决,提高了系统复杂度等问题。

做垂直拆分其实跟读写分离是一样的,本质上还是多数据源的问题,本文中先考虑最简单的垂直拆分方式,垂直拆分+读写分离我们下篇文章进行讲解。

垂直拆分步骤

至于怎么整合Sharding-JDBC就不在讲解了,上篇文章有讲解过,直接开始和兴步骤。

假设我们拆分成了2个库,分别是ds0和ds1,每个库中的表不同,ds_0中放了user表,SQL脚本如下:

  1. CREATE DATABASE `ds_0` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

  2. CREATE TABLE `user`(

  3. id bigint(64) not null,

  4. city varchar(20) not null,

  5. name varchar(20) not null,

  6. PRIMARY KEY (`id`)

  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ds_1中放了loudong表,SQL脚本如下:

  1. CREATE DATABASE `ds_1` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

  2. CREATE TABLE `loudong` (

  3. `id` varchar(20) NOT NULL,

  4. `city` varchar(20) NOT NULL,

  5. `region` varchar(20) NOT NULL,

  6. `name` varchar(20) NOT NULL,

  7. `ld_num` varchar(10) NOT NULL,

  8. `unit_num` varchar(10) NOT NULL,

  9. PRIMARY KEY (`id`)

  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8

最核心的还是数据源的配置以及绑定:

  1. spring.shardingsphere.datasource.names=ds0,ds1

  2. # ds0数据源

  3. spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource

  4. spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver

  5. spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8

  6. spring.shardingsphere.datasource.ds0.username=root

  7. spring.shardingsphere.datasource.ds0.password=123456

  8. # ds1数据源

  9. spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource

  10. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver

  11. spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8

  12. spring.shardingsphere.datasource.ds1.username=root

  13. spring.shardingsphere.datasource.ds1.password=123456

  14. # 绑定loudong表所在节点

  15. spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong

  16. # 绑定user表所在节点

  17. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user

  18. # 设置自增ID

  19. spring.shardingsphere.sharding.tables.user.key-generator.column=id

  20. # 设置自增ID算法

  21. spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE

配置完之后该怎么用还是怎么用,完全不用改变一行代码。sharding-jdbc底层会对数据源进行接管。

如果我们不用sharding-jdbc的话,你同样需要配置2个数据源,这个其实差不多,最复杂的就是你在操作数据库的时候需要知道当前的操作是哪个数据源,因为每个数据源中的表都不一样,通过sharding-jdbc框架屏蔽了这些复杂的操作。

垂直拆分下的读写分离步骤

从最开始的单库多表,到读写分离,再到垂直拆分多个库。

循序渐进的为大家讲解高并发,大数据量下的数据库解决方案。并引入开源的Sharding-JDBC来实现具体的方案。

垂直拆分后进一步提升性能的方式就是垂直拆分多库的读写分离,如下图:

要实习这个功能,我们只需要在上面的基础上,为每个库增加一个从节点的配置就可以了,然后用master-slave-rules将主从数据源进行绑定,如下:

  1. spring.shardingsphere.datasource.names=ds0,ds0slave,ds1,ds1slave

  2. # ds0主数据源

  3. spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource

  4. spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver

  5. spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8

  6. spring.shardingsphere.datasource.ds0.username=root

  7. spring.shardingsphere.datasource.ds0.password=123456

  8. # ds0从数据源

  9. spring.shardingsphere.datasource.ds0slave.type=com.alibaba.druid.pool.DruidDataSource

  10. spring.shardingsphere.datasource.ds0slave.driver-class-name=com.mysql.jdbc.Driver

  11. spring.shardingsphere.datasource.ds0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8

  12. spring.shardingsphere.datasource.ds0slave.username=root

  13. spring.shardingsphere.datasource.ds0slave.password=123456

  14. # ds1主数据源

  15. spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource

  16. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver

  17. spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8

  18. spring.shardingsphere.datasource.ds1.username=root

  19. spring.shardingsphere.datasource.ds1.password=123456

  20. # ds1从数据源

  21. spring.shardingsphere.datasource.ds1slave.type=com.alibaba.druid.pool.DruidDataSource

  22. spring.shardingsphere.datasource.ds1slave.driver-class-name=com.mysql.jdbc.Driver

  23. spring.shardingsphere.datasource.ds1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8

  24. spring.shardingsphere.datasource.ds1slave.username=root

  25. spring.shardingsphere.datasource.ds1slave.password=123456

  26. # 绑定loudong表所在节点

  27. spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong

  28. # 绑定user表所在节点

  29. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user

  30. spring.shardingsphere.sharding.tables.user.key-generator.column=id

  31. spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE

  32. # 读写分离

  33. spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0

  34. spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=ds0slave

  35. spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=ds1

  36. spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=ds1slave

源码参考:https://github.com/yinjihuan/sharding-jdbc

觉得不错的记得关注下哦,给个Star吧!

ShardingJDBC(一)-转载的更多相关文章

  1. Sharding-JDBC实践(一)简介

    转载自:ShardingSphere官网 目录 一.介绍 1. Sharding-JDBC 2. Sharding-Proxy 3. Sharding-Sidecar(TBD) 4. 混合架构 二.功 ...

  2. sharding-jdbc之——分库分表实例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/79368021 一.概述 之前,我们介绍了利用Mycat进行分库分表操作,Mycat ...

  3. 分布式数据库中间件、产品——sharding-jdbc、mycat、drds

    一般对于业务记录类随时间会不断增加的数据,当数据量增加到一定量(一般认为整型值为主的表达到千万级,字符串为主的表达到五百万)的时候,性能将遇到瓶颈,同时调整表结构也会变得非常困难.为了避免生产遇到这样 ...

  4. sharding-JDBC学习笔记

    sharding-JDBC学习笔记 ShardingSphere ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC.Sharding-Pr ...

  5. ShardingJDBC的基本配置和使用

    一.ShardingSphere介绍 ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC.Sharding-Proxy和Sharding-S ...

  6. 基于ShardingJDBC的分库分表详细整理

    转载 https://www.cnblogs.com/jackion5/p/13658615.html 前言 传统应用项目设计通常都是采用单一数据库作为存储方案,但是随着互联网的迅猛发展以及应用数据量 ...

  7. Crystal Clear Applied: The Seven Properties of Running an Agile Project (转载)

    作者Alistair Cockburn, Crystal Clear的7个成功要素,写得挺好. 敏捷方法的关注点,大家可以参考,太激动所以转载了. 原文:http://www.informit.com ...

  8. RTP与RTCP协议介绍(转载)

    RTSP发起/终结流媒体.RTP传输流媒体数据 .RTCP对RTP进行控制,同步.RTP中没有连接的概念,本身并不能为按序传输数据包提供可靠的保证,也不提供流量控制和拥塞控制,这些都由RTCP来负责完 ...

  9. 《Walking the callstack(转载)》

    本文转载自:https://www.codeproject.com/articles/11132/walking-the-callstack Download demo project with so ...

随机推荐

  1. PHP获取指定日期是星期几的实现方法

    这篇文章主要介绍了PHP获取指定日期是星期几的实现方法,涉及php针对日期的读取.判断与字符串.数组相关运算操作技巧,需要的朋友可以参考下 本文实例讲述了PHP获取指定日期是星期几的实现方法.分享给大 ...

  2. codeforces 622D D. Optimal Number Permutation(找规律)

    D. Optimal Number Permutation time limit per test 1 second memory limit per test 256 megabytes input ...

  3. php各种验证类

    <?php /**  * 验证类  *  */ class VerifyAction{     /**      * 是否为空值      */       public static func ...

  4. C++ STL, set用法。 待更新zzzzz

    set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...

  5. ACM学习历程—HDU5587 Array(数学 && 二分 && 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5587 题目大意就是初始有一个1,然后每次操作都是先在序列后面添加一个0,然后把原序列添加到0后面,然后 ...

  6. js代码--根据经纬度计算距离

    原网页地址:http://www.storyday.com/wp-content/uploads/2008/09/latlung_dis.html <!DOCTYPE html> < ...

  7. POJ1456:Supermarket

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=1456 把物品按照时间排序,显然\ ...

  8. 统计不同的单词(map应用)

    题目描述: 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词.在判断是否满足条件时,字母不区分大小写,但在输出时应保留输入中的大小写,按字典序进行排列(所有 ...

  9. 【转】 Pro Android学习笔记(四四):Dialog(1):触发Dialog

    目录(?)[-] 创建dialog fragment Activity显示对话框 Android提供alert.prompt.pick-list,单选.多选,progress.time-picker和 ...

  10. kafka之一:Windows上搭建Kafka运行环境

    搭建环境 1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-213315 ...