操作数据库流程
所以知道了上面的流程,我们可以根据不同的业务情况,提供不同的DataSource,动态的提供DataSource
分别创建read_db.user和write_db.user,并向read_db.user写入数据
mysql> create database read_db;
Query OK, 1 row affected (0.14 sec)
mysql> create database write_db;
Query OK, 1 row affected (0.01 sec)
mysql> create table read_db.user (
-> id bigint(20) auto_increment not null comment '主键ID',
-> name varchar(30) null default null comment '姓名',
-> primary key (id)
-> );
Query OK, 0 rows affected, 1 warning (0.29 sec)
mysql>
mysql> insert into read_db.user (id, name) values
-> (1, 'read_name1'),
-> (2, 'read_name2'),
-> (3, 'read_name3'),
-> (4, 'read_name4'),
-> (5, 'read_name5');
Query OK, 5 rows affected (0.16 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> create table write_db.user (
-> id bigint(20) auto_increment not null comment '主键ID',
-> name varchar(30) null default null comment '姓名',
-> primary key (id)
-> );
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql>
我们可以通过org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
来帮我们实现动态数据源的切换,而且AbstractRoutingDataSource实现了很多DataSource的方法,稳定性更好
AbstractRoutingDataSource需要给如下三个属性赋值:
动态选择DataSource的实现逻辑如下:
......省略部分......
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = this.determineCurrentLookupKey();
DataSource dataSource = (DataSource)this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
} else {
return dataSource;
}
}
......省略部分......
而这个determineCurrentLookupKey是由我们自定义的DynamicDataSourceConfig进行设置的
所以我们的MyDynamicDataSourceConfig只需要继承AbstractRoutingDataSource,并完成上面的三个属性值设置,再做一些简单的配置即可
mysql
mysql-connector-java
8.0.31
com.alibaba
druid
1.2.15
com.baomidou
mybatis-plus-boot-starter
3.5.2
org.springframework.boot
spring-boot-starter-aop
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 用于读的数据库
spring.datasource.datasource1.url=jdbc:mysql://192.168.28.12:3306/read_db
spring.datasource.datasource1.username=root
spring.datasource.datasource1.password=Root_123
spring.datasource.datasource1.driver-class-name=com.mysql.cj.jdbc.Driver
# 用于写的数据库
spring.datasource.datasource2.url=jdbc:mysql://192.168.28.12:3306/write_db
spring.datasource.datasource2.username=root
spring.datasource.datasource2.password=Root_123
spring.datasource.datasource2.driver-class-name=com.mysql.cj.jdbc.Driver
实现功能:
package com.hh.springboottest.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* @Author 贺欢
* @Date 2022/11/24
* @Description
*/
@Configuration
public class MyDataSourceConfig {
@ConfigurationProperties(prefix = "spring.datasource.datasource1")
// 向IOC容器添加name = dataSource1的DataSource
@Bean
public DataSource dataSource1() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
@ConfigurationProperties(prefix = "spring.datasource.datasource2")
// 向IOC容器添加name = dataSource2的DataSource
@Bean
public DataSource dataSource2() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
}
不使用AbstractRoutingDataSource。implement DataSource, InitializingBean说明:
继承AbstractRoutingDataSource实现的功能:
package com.hh.springboottest.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/*
不使用AbstractRoutingDataSource。implement DataSource, InitializingBean说明:
1. 在afterPropertiesSet方法中,初始化readOrWrite的值
2. 重写getConnection方法,根据readOrWrite的值的不同,获取不同的dataSource返回Connection
3. 重写DataSource的抽象方法。方法的返回对象测试时返回null等
*/
@Component
// 当前IOC容器由dataSource1、dataSource2、当前DataSource
// Mapper获取DataSource时,优先获取当前DataSource
@Primary
public class MyDynamicDataSourceConfig extends AbstractRoutingDataSource {
// 用于存放读写标识。ThreadLocal能保证多线程并发安全
public static ThreadLocal readOrWrite = new ThreadLocal<>();
// 从IOC容器获取name = dataSource1的DataSource
@Autowired
DataSource dataSource1;
// 从IOC容器获取name = dataSource2的DataSource
@Autowired
DataSource dataSource2;
// 返回当前的读写标识
@Override
protected Object determineCurrentLookupKey() {
return readOrWrite.get();
}
// 初始化MyDynamicDataSourceConfig后,会调用该方法进行各种属性值的设置
@Override
public void afterPropertiesSet() {
Map
说明:
package com.hh.springboottest.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MYDS {
// 设置默认值
String value() default "r";
}
说明:
package com.hh.springboottest.aspect;
import com.hh.springboottest.annotation.MYDS;
import com.hh.springboottest.config.MyDynamicDataSourceConfig;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class DynamicDataSourceAspect {
// 前置
@Before("within(com.hh.springboottest.service.impl.*) && @annotation(myds)")
public void before(JoinPoint point, MYDS myds) {
String readOrWrite = myds.value();
MyDynamicDataSourceConfig.readOrWrite.set(readOrWrite);
System.out.println(readOrWrite);
}
// 环绕通知
}
package com.hh.springboottest.myController;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class User {
private Long id;
private String name;
}
package com.hh.springboottest.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hh.springboottest.myController.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper {
}
Service接口实现
package com.hh.springboottest.service;
import com.hh.springboottest.myController.User;
public interface UserService {
public User getUser(Long id);
public void saveUser(User user);
}
ServiceImpl实现类
package com.hh.springboottest.service.impl;
import com.hh.springboottest.mapper.UserMapper;
import com.hh.springboottest.myController.User;
import com.hh.springboottest.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@MYDS("r")
public User getUser(Long id) {
return userMapper.selectById(id);
}
@MYDS("w")
public void saveUser(User user) {
userMapper.insert(user);
}
}
package com.hh.springboottest;
import com.hh.springboottest.config.MyDynamicDataSourceConfig;
import com.hh.springboottest.myController.User;
import com.hh.springboottest.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@Slf4j
@SpringBootTest
// 开启AOP功能
@EnableAspectJAutoProxy
public class MyApplicationTest {
@Autowired
UserService userService;
@Test
public void readTest() {
// 不使用AOP基于注解,手动切换数据源
// MyDynamicDataSourceConfig.readOrWrite.set("r");
User user = userService.getById(1);
log.info("获取到的用户为:{}", user);
}
@Test
public void writeTest() {
// 不使用AOP基于注解,手动切换数据源
// MyDynamicDataSourceConfig.readOrWrite.set("w");
User user = new User(1L, "write_name1");
userService.save(user);
}
}
运行测试类,结果如下:
r
2022-11-24 06:22:31.082 INFO 33932 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
2022-11-24 06:22:31.082 INFO 33932 --- [ main] com.hh.springboottest.MyApplicationTest : 获取到的用户为:User(id=1, name=read_name1)
2022-11-24 06:22:31.126 INFO 33932 --- [ionShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-0} closing ...
2022-11-24 06:22:31.126 INFO 33932 --- [ionShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closing ...
2022-11-24 06:22:31.131 INFO 33932 --- [ionShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed
同时查看write_db.user表,数据如下:
mysql> select * from write_db.user;
+----+-------------+
| id | name |
+----+-------------+
| 1 | write_name1 |
+----+-------------+
1 row in set (0.10 sec)
mysql>
我们这里实现的,是使用AOP,适合复杂业务读写多数据源场景。还有另一种通过Mybatis插件的方式,适合读写分离业务