通过读写分离的优化后,小王可算是轻松了一段时间,读写分离具体的方案请查看这篇文章: Sharding-JDBC:查询量大如何优化?node
但是好景不长,业务发展是在太快了。数据库中的数据量猛增,因为全部表都在一个数据库中,致使服务器本地存储快满了。mysql
从上图咱们能够看的出来,因为表的数量较多,每一个表的数据量也较大,可是还没到水平拆分的地步。目前遇到的问题是服务器的存储不够了,短时间内还不用水平拆分,那么方案呼之欲出了:垂直拆分。git
咱们都知道,一个数据库它是由N张表构成,每一个表存储的数据都不同,都对应着各自的业务。github
所谓的垂直切分其实就是分类存储,大部分都是按业务类型进行分类。相同的类型存储在相同的库上,不一样的类型存储在不一样的库上,这样也就将数据或者说压力分担到不一样的库上面 。算法
好比咱们能够将用户相关的放一块儿,订单相关的放一块儿,行为日志相关的放一块儿,依次来推下去。spring
拆分以后业务规划清晰,数据维护简单,分担了数据集中存储的压力。sql
缺点也很明显,多表join查询没法实现,只能经过接口方式解决,提升了系统复杂度等问题。数据库
作垂直拆分其实跟读写分离是同样的,本质上仍是多数据源的问题,本文中先考虑最简单的垂直拆分方式,垂直拆分+读写分离咱们下篇文章进行讲解。bash
至于怎么整合Sharding-JDBC就不在讲解了,上篇文章有讲解过,直接开始和兴步骤。服务器
假设咱们拆分红了2个库,分别是ds_0和ds_1,每一个库中的表不一样,ds_0中放了user表,SQL脚本以下:
CREATE DATABASE `ds_0` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
CREATE TABLE `user`(
id bigint(64) not null,
city varchar(20) not null,
name varchar(20) not null,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
复制代码
ds_1中放了loudong表,SQL脚本以下:
CREATE DATABASE `ds_1` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
CREATE TABLE `loudong` (
`id` varchar(20) NOT NULL,
`city` varchar(20) NOT NULL,
`region` varchar(20) NOT NULL,
`name` varchar(20) NOT NULL,
`ld_num` varchar(10) NOT NULL,
`unit_num` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
复制代码
最核心的仍是数据源的配置以及绑定:
spring.shardingsphere.datasource.names=ds0,ds1
# ds0数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
# ds1数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
# 绑定loudong表所在节点
spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
# 绑定user表所在节点
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
# 设置自增ID
spring.shardingsphere.sharding.tables.user.key-generator.column=id
# 设置自增ID算法
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
复制代码
配置完以后该怎么用仍是怎么用,彻底不用改变一行代码。sharding-jdbc底层会对数据源进行接管。
若是咱们不用sharding-jdbc的话,你一样须要配置2个数据源,这个其实差很少,最复杂的就是你在操做数据库的时候须要知道当前的操做是哪一个数据源,由于每一个数据源中的表都不同,经过sharding-jdbc框架屏蔽了这些复杂的操做。
从最开始的单库多表,到读写分离,再到垂直拆分多个库。
按部就班的为你们讲解高并发,大数据量下的数据库解决方案。并引入开源的Sharding-JDBC来实现具体的方案。
垂直拆分后进一步提高性能的方式就是垂直拆分多库的读写分离,以下图:
要实习这个功能,咱们只须要在上面的基础上,为每一个库增长一个从节点的配置就能够了,而后用master-slave-rules将主从数据源进行绑定,以下:
spring.shardingsphere.datasource.names=ds0,ds0slave,ds1,ds1slave
# ds0主数据源
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456
# ds0从数据源
spring.shardingsphere.datasource.ds0slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0slave.url=jdbc:mysql://localhost:3306/ds0slave?characterEncoding=utf-8
spring.shardingsphere.datasource.ds0slave.username=root
spring.shardingsphere.datasource.ds0slave.password=123456
# ds1主数据源
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456
# ds1从数据源
spring.shardingsphere.datasource.ds1slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1slave.url=jdbc:mysql://localhost:3306/ds1slave?characterEncoding=utf-8
spring.shardingsphere.datasource.ds1slave.username=root
spring.shardingsphere.datasource.ds1slave.password=123456
# 绑定loudong表所在节点
spring.shardingsphere.sharding.tables.loudong.actual-data-nodes=ds1.loudong
# 绑定user表所在节点
spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user
spring.shardingsphere.sharding.tables.user.key-generator.column=id
spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
# 读写分离
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=ds0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=ds0slave
spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=ds1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=ds1slave
复制代码
以为不错的记得关注下哦,给个Star吧!