【SpringBoot2.0系列01】初识SpringBoot
【SpringBoot2.0系列02】SpringBoot之使用Thymeleaf视图模板
【SpringBoot2.0系列03】SpringBoot之使用freemark视图模板
【SpringBoot2.0系列04】SpringBoot之使用JPA完成简单的rest api
【SpringBoot2.0系列05】SpringBoot之整合Mybatis
【SpringBoot2.0系列06】SpringBoot之多数据源动态切换数据源
前言
在前面两节我们已经完成springboot操作mysql数据库,但是在实际业务场景中,数据量迅速增长,一个库一个表已经满足不了我们的需求的时候,我们就会考虑分库分表的操作,那么接下来我们就去学习一下,在springboot中如何实现多数据源,动态数据源切换,读写分离等操作。
实现
1、建库建表
首先,我们在本地新建三个数据库名分别为master
,slave1
,slave2
,我们的目前就是写入操作都是在master
,查询是 slave1,slave2
因此我们在上一篇也就是【SpringBoot2.0系列05】SpringBoot之整合Mybatis基础上进行改动,
我们在master slave1 slave2
中都创建user
表 其中初始化slave1
库的user
表数据为
初始化
slave2
库的user
表
具体的数据库脚本如下
create table master.user
(
id bigint auto_increment comment '主键'
primary key,
age int null comment '年龄',
password varchar(32) null comment '密码',
sex int null comment '性别',
username varchar(32) null comment '用户名'
)
engine=MyISAM collate=utf8mb4_bin
;
create table slave1.user
(
id bigint auto_increment comment '主键'
primary key,
age int null comment '年龄',
password varchar(32) null comment '密码',
sex int null comment '性别',
username varchar(32) null comment '用户名'
)
engine=MyISAM collate=utf8mb4_bin
;
INSERT INTO slave1.user (id, age, password, sex, username) VALUES (2, 22, 'admin', 1, 'admin');
create table slave2.user
(
id bigint auto_increment comment '主键'
primary key,
age int null comment '年龄',
password varchar(32) null comment '密码',
sex int null comment '性别',
username varchar(32) null comment '用户名'
)
engine=MyISAM collate=utf8mb4_bin
;
INSERT INTO slave2.user (id, age, password, sex, username) VALUES (3, 19, 'uuu', 2, 'user');
INSERT INTO slave2.user (id, age, password, sex, username) VALUES (4, 18, 'bbbb', 1, 'zzzz');
2、配置多数据源
经过上面初始化 我们的master.user
是一张空表,我们等下的插入与更新操作就在这上面,那么我们的查询操作就是在slave1.user跟slave2.user
上面了。
上面我们的数据库初始化工作完成了,接下来就是实现动态数据源的过程
首先我们需要在我们的application.yml
配置我们的三个数据源
server:
port: 8989
spring:
datasource:
master:
password: root
url: jdbc:mysql://127.0.0.1:3306/master?useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
type: com.zaxxer.hikari.HikariDataSource
cluster:
- key: slave1
password: root
url: jdbc:mysql://127.0.0.1:3306/slave1?useUnicode=true&characterEncoding=UTF-8
idle-timeout: 20000
driver-class-name: com.mysql.jdbc.Driver
username: root
type: com.zaxxer.hikari.HikariDataSource
- key: slave2
password: root
url: jdbc:mysql://127.0.0.1:3306/slave2?useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
mybatis:
mapper-locations: classpath:/mybatis/mapper/*.xml
config-location: classpath:/mybatis/config/mybatis-config.xml
在上面我们配置了三个数据,其中第一个作为默认数据源也就是我们的master
数据源。主要是写操作,那么读操作交给我们的slave1跟slave2
其中 master 数据源一定是要配置 作为我们的默认数据源,其次cluster集群中,其他的数据不配置也不会影响程序员运行,如果你想添加新的一个数据源 就在cluster下新增一个数据源即可,其中key为必须项,用于数据源的唯一标识,以及接下来切换数据源的标识。
3、注册数据源
在上面我们已经配置了三个数据源,但是这是我们自定义的配置,springboot是无法给我们自动配置,所以需要我们自己注册数据源.
那么就要实现 EnvironmentAware
用于读取上下文环境变量用于构建数据源,同时也需要实现 ImportBeanDefinitionRegistrar
接口注册我们构建的数据源。com.yukong.chapter5.register.DynamicDataSourceRegister
具体代码如下
/**
* 动态数据源注册
* 实现 ImportBeanDefinitionRegistrar 实现数