【SpringBoot2.0系列06】SpringBoot之多数据源动态切换数据源

【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表数据为
image.png
初始化
slave2库的user
image.png
具体的数据库脚本如下

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 实现数据源注册
 * 实现 Environ
  • 44
    点赞
  • 213
    收藏
    觉得还不错? 一键收藏
  • 42
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值