主人公小王入职了一家刚起步的创业公司,公司正在研发一款App。为了快速开发出能够投入市场进行宣传的版本,小王可是天天加班到很晚,忙了一段时间后终于把第一个版本赶出来了。
初期功能不多,表也不多,用的MySql存储业务数据。就一个节点,当然每天凌晨有定时备份机制。
下图是目前的一个现状:
得益于运营人员的大力推广,这款App初见成效。注册用户越来越多,查询量越来越大,对于不太会更新的数据小王加上了缓存,又撑了一段时间。
对于某些数据还是要查数据库,按目前的业务发展,单节点的数据库已经快满足不了需求了。而且读和写都在一起,小王打算进行一次优化,将数据库做读写分离,一主多从。
下图是改进后的一个现状:
将读请求全部走从节点,主节点只写入来缓解数据的查询压力,数据库部署这块正好小王公司有个运维可以搞定,但是应用程序这块也得支持多数据源才行呀。
小王是个雷厉风行的人,行动力极强,马上脑袋中就有了方案,配置多个数据源不就行了,然后用不同的数据源进行数据操作就可以了嘛!
伪代码如下:
// 主数据源
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
//指定数据源配置前缀
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
// 从数据源
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary //在同样的DataSource中,首先使用被标注的DataSource
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
假设我们用JdbcTemplate操作数据库:
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
配置完成后我们在操作数据的时候选用不同的JdbcTemplate就可以满足需求了。有个问题是一旦从节点多了起来,也就意味着会有多个JdbcTemplate,使用的时候是不是还得有个算法,用哪个来操作,比较麻烦。
于是小王找到了我,我这人是个热心肠。既然找到了我肯定得帮助下,当然我不是帮小王写代码,只是给他提供思路+方案。
我对小王说:ShardingSphere知道么,你用这个吧,比你自己去配多数据源简单多了。ShardingSphere是后来规划的,最开始是只有 Sharding-JDBC 一款产品,基于客户端形式的分库分表。后面发展变成了现在的Apache ShardingSphere(Incubator) ,它是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(规划中)这3款相互独立,却又能够混合部署配合使用的产品组成。它们均提供标准化的数据分片、分布式事务和数据库治理功能,可适用于如Java同构、异构语言、容器、云原生等各种多样化的应用场景。
经过我的指导小王还是顺利的用Sharding-JDBC将读写分离整出来了,下面给大家分享下步骤。
第一步:创建2个数据库,模拟一主一从,当然如果你有现成的主从环境更好啦
CREATE DATABASE `ds_0` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
CREATE DATABASE `ds_1` 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_0和ds_1这两个库中分别创建一个user表,用于数据操作演示。https://anime-pictures.net/profile/view_ext_profile/220572
https://anime-pictures.net/profile/view_ext_profile/220572?asfmn
https://anime-pictures.net/profile/view_ext_profile/220572?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220572?lang=en
https://anime-pictures.net/profile/view_ext_profile/220572?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220572?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220572?lang=es
https://anime-pictures.net/profile/view_ext_profile/220572?lang=it
https://anime-pictures.net/profile/view_ext_profile/220572?lang=de
https://anime-pictures.net/profile/view_ext_profile/220572?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220579
https://anime-pictures.net/profile/view_ext_profile/220579?rklsw
https://anime-pictures.net/profile/view_ext_profile/220579?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220579?lang=en
https://anime-pictures.net/profile/view_ext_profile/220579?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220579?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220579?lang=es
https://anime-pictures.net/profile/view_ext_profile/220579?lang=it
https://anime-pictures.net/profile/view_ext_profile/220579?lang=de
https://anime-pictures.net/profile/view_ext_profile/220579?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220587
https://anime-pictures.net/profile/view_ext_profile/220587?kdrzr
https://anime-pictures.net/profile/view_ext_profile/220587?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220587?lang=en
https://anime-pictures.net/profile/view_ext_profile/220587?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220587?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220587?lang=es
https://anime-pictures.net/profile/view_ext_profile/220587?lang=it
https://anime-pictures.net/profile/view_ext_profile/220587?lang=de
https://anime-pictures.net/profile/view_ext_profile/220587?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220601
https://anime-pictures.net/profile/view_ext_profile/220601?qlsus
https://anime-pictures.net/profile/view_ext_profile/220601?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220601?lang=en
https://anime-pictures.net/profile/view_ext_profile/220601?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220601?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220601?lang=es
https://anime-pictures.net/profile/view_ext_profile/220601?lang=it
https://anime-pictures.net/profile/view_ext_profile/220601?lang=de
https://anime-pictures.net/profile/view_ext_profile/220601?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220615
https://anime-pictures.net/profile/view_ext_profile/220615?luooe
https://anime-pictures.net/profile/view_ext_profile/220615?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220615?lang=en
https://anime-pictures.net/profile/view_ext_profile/220615?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220615?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220615?lang=es
https://anime-pictures.net/profile/view_ext_profile/220615?lang=it
https://anime-pictures.net/profile/view_ext_profile/220615?lang=de
https://anime-pictures.net/profile/view_ext_profile/220615?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220623
https://anime-pictures.net/profile/view_ext_profile/220623?psbow
https://anime-pictures.net/profile/view_ext_profile/220623?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220623?lang=en
https://anime-pictures.net/profile/view_ext_profile/220623?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220623?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220623?lang=es
https://anime-pictures.net/profile/view_ext_profile/220623?lang=it
https://anime-pictures.net/profile/view_ext_profile/220623?lang=de
https://anime-pictures.net/profile/view_ext_profile/220623?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220630
https://anime-pictures.net/profile/view_ext_profile/220630?kbaoj
https://anime-pictures.net/profile/view_ext_profile/220630?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220630?lang=en
https://anime-pictures.net/profile/view_ext_profile/220630?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220630?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220630?lang=es
https://anime-pictures.net/profile/view_ext_profile/220630?lang=it
https://anime-pictures.net/profile/view_ext_profile/220630?lang=de
https://anime-pictures.net/profile/view_ext_profile/220630?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220643
https://anime-pictures.net/profile/view_ext_profile/220643?dkcam
https://anime-pictures.net/profile/view_ext_profile/220643?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220643?lang=en
https://anime-pictures.net/profile/view_ext_profile/220643?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220643?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220643?lang=es
https://anime-pictures.net/profile/view_ext_profile/220643?lang=it
https://anime-pictures.net/profile/view_ext_profile/220643?lang=de
https://anime-pictures.net/profile/view_ext_profile/220643?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220674
https://anime-pictures.net/profile/view_ext_profile/220674?hyxag
https://anime-pictures.net/profile/view_ext_profile/220674?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220674?lang=en
https://anime-pictures.net/profile/view_ext_profile/220674?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220674?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220674?lang=es
https://anime-pictures.net/profile/view_ext_profile/220674?lang=it
https://anime-pictures.net/profile/view_ext_profile/220674?lang=de
https://anime-pictures.net/profile/view_ext_profile/220674?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220690
https://anime-pictures.net/profile/view_ext_profile/220690?purux
https://anime-pictures.net/profile/view_ext_profile/220690?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220690?lang=en
https://anime-pictures.net/profile/view_ext_profile/220690?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220690?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220690?lang=es
https://anime-pictures.net/profile/view_ext_profile/220690?lang=it
https://anime-pictures.net/profile/view_ext_profile/220690?lang=de
https://anime-pictures.net/profile/view_ext_profile/220690?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220696
https://anime-pictures.net/profile/view_ext_profile/220696?lvoeb
https://anime-pictures.net/profile/view_ext_profile/220696?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220696?lang=en
https://anime-pictures.net/profile/view_ext_profile/220696?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220696?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220696?lang=es
https://anime-pictures.net/profile/view_ext_profile/220696?lang=it
https://anime-pictures.net/profile/view_ext_profile/220696?lang=de
https://anime-pictures.net/profile/view_ext_profile/220696?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220716
https://anime-pictures.net/profile/view_ext_profile/220716?wtjwm
https://anime-pictures.net/profile/view_ext_profile/220716?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220716?lang=en
https://anime-pictures.net/profile/view_ext_profile/220716?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220716?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220716?lang=es
https://anime-pictures.net/profile/view_ext_profile/220716?lang=it
https://anime-pictures.net/profile/view_ext_profile/220716?lang=de
https://anime-pictures.net/profile/view_ext_profile/220716?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220725
https://anime-pictures.net/profile/view_ext_profile/220725?hpflv
https://anime-pictures.net/profile/view_ext_profile/220725?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220725?lang=en
https://anime-pictures.net/profile/view_ext_profile/220725?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220725?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220725?lang=es
https://anime-pictures.net/profile/view_ext_profile/220725?lang=it
https://anime-pictures.net/profile/view_ext_profile/220725?lang=de
https://anime-pictures.net/profile/view_ext_profile/220725?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220740
https://anime-pictures.net/profile/view_ext_profile/220740?swcze
https://anime-pictures.net/profile/view_ext_profile/220740?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220740?lang=en
https://anime-pictures.net/profile/view_ext_profile/220740?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220740?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220740?lang=es
https://anime-pictures.net/profile/view_ext_profile/220740?lang=it
https://anime-pictures.net/profile/view_ext_profile/220740?lang=de
https://anime-pictures.net/profile/view_ext_profile/220740?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220744
https://anime-pictures.net/profile/view_ext_profile/220744?simjy
https://anime-pictures.net/profile/view_ext_profile/220744?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220744?lang=en
https://anime-pictures.net/profile/view_ext_profile/220744?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220744?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220744?lang=es
https://anime-pictures.net/profile/view_ext_profile/220744?lang=it
https://anime-pictures.net/profile/view_ext_profile/220744?lang=de
https://anime-pictures.net/profile/view_ext_profile/220744?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220761
https://anime-pictures.net/profile/view_ext_profile/220761?sqduf
https://anime-pictures.net/profile/view_ext_profile/220761?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220761?lang=en
https://anime-pictures.net/profile/view_ext_profile/220761?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220761?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220761?lang=es
https://anime-pictures.net/profile/view_ext_profile/220761?lang=it
https://anime-pictures.net/profile/view_ext_profile/220761?lang=de
https://anime-pictures.net/profile/view_ext_profile/220761?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220767
https://anime-pictures.net/profile/view_ext_profile/220767?paytp
https://anime-pictures.net/profile/view_ext_profile/220767?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220767?lang=en
https://anime-pictures.net/profile/view_ext_profile/220767?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220767?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220767?lang=es
https://anime-pictures.net/profile/view_ext_profile/220767?lang=it
https://anime-pictures.net/profile/view_ext_profile/220767?lang=de
https://anime-pictures.net/profile/view_ext_profile/220767?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220172
https://anime-pictures.net/profile/view_ext_profile/220172?pjxrz
https://anime-pictures.net/profile/view_ext_profile/220172?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220172?lang=en
https://anime-pictures.net/profile/view_ext_profile/220172?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220172?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220172?lang=es
https://anime-pictures.net/profile/view_ext_profile/220172?lang=it
https://anime-pictures.net/profile/view_ext_profile/220172?lang=de
https://anime-pictures.net/profile/view_ext_profile/220172?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220173
https://anime-pictures.net/profile/view_ext_profile/220173?tnbzt
https://anime-pictures.net/profile/view_ext_profile/220173?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220173?lang=en
https://anime-pictures.net/profile/view_ext_profile/220173?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220173?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220173?lang=es
https://anime-pictures.net/profile/view_ext_profile/220173?lang=it
https://anime-pictures.net/profile/view_ext_profile/220173?lang=de
https://anime-pictures.net/profile/view_ext_profile/220173?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220184
https://anime-pictures.net/profile/view_ext_profile/220184?ffrrh
https://anime-pictures.net/profile/view_ext_profile/220184?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220184?lang=en
https://anime-pictures.net/profile/view_ext_profile/220184?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220184?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220184?lang=es
https://anime-pictures.net/profile/view_ext_profile/220184?lang=it
https://anime-pictures.net/profile/view_ext_profile/220184?lang=de
https://anime-pictures.net/profile/view_ext_profile/220184?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220185
https://anime-pictures.net/profile/view_ext_profile/220185?brugd
https://anime-pictures.net/profile/view_ext_profile/220185?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220185?lang=en
https://anime-pictures.net/profile/view_ext_profile/220185?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220185?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220185?lang=es
https://anime-pictures.net/profile/view_ext_profile/220185?lang=it
https://anime-pictures.net/profile/view_ext_profile/220185?lang=de
https://anime-pictures.net/profile/view_ext_profile/220185?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220189
https://anime-pictures.net/profile/view_ext_profile/220189?ejgdl
https://anime-pictures.net/profile/view_ext_profile/220189?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220189?lang=en
https://anime-pictures.net/profile/view_ext_profile/220189?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220189?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220189?lang=es
https://anime-pictures.net/profile/view_ext_profile/220189?lang=it
https://anime-pictures.net/profile/view_ext_profile/220189?lang=de
https://anime-pictures.net/profile/view_ext_profile/220189?lang=fr
https://anime-pictures.net/profile/view_ext_profile/220197
https://anime-pictures.net/profile/view_ext_profile/220197?woemo
https://anime-pictures.net/profile/view_ext_profile/220197?lang=zh_CN
https://anime-pictures.net/profile/view_ext_profile/220197?lang=en
https://anime-pictures.net/profile/view_ext_profile/220197?lang=ru
https://anime-pictures.net/profile/view_ext_profile/220197?lang=jp
https://anime-pictures.net/profile/view_ext_profile/220197?lang=es
https://anime-pictures.net/profile/view_ext_profile/220197?lang=it
https://anime-pictu