通過讀寫分離的優化後,小王可算是輕鬆了一段時間,讀寫分離具體的方案請查看這篇文章: 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吧!