SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源

时间:2022-12-24 13:02:21


SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源

作者:一一哥

在上一节中,我通过分包的方式实现了多数据源的配置,接下来我通过AOP切面的方式,带领大家实现第二种多数据源配置方式,该方式是在前面案例的基础上进行编写的。

一. 实现过程

1. 创建web项目

我们按照之前的经验,创建一个web程序,并将之改造成Spring Boot项目,具体过程略。

SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源


2. 添加依赖包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>

3. 创建application.yml配置文件

在该配置文件中,要进行两个数据库的配置,本案例中我们使用默认的HikariDataSource数据源。

spring:
main:
allow-bean-definition-overriding: true
datasource:
ds1:
url: jdbc:mysql://localhost:3306/db1?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: syc
driverClassName: com.mysql.jdbc.Driver
ds2:
url: jdbc:mysql://localhost:3306/db4?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: syc
driverClassName: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
# type: com.alibaba.druid.pool.DruidDataSource
jpa:
database: mysql
show-sql: true
hibernate:
ddl-auto: update
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
database-platform: org.hibernate.dialect.MySQL5Dialect

4. 创建数据库配置类

第一个数据库配置类

package com.yyg.boot.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description db1数据源配置类
*/
@ConfigurationProperties(prefix = "spring.datasource.ds1")
@Component("ds1Properties")
@Data
public class Ds1Properties {

private String url;

private String username;

private String password;

private String driverClassName;

}

第2个数据库配置类

package com.yyg.boot.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description db4数据源配置类
*/
@ConfigurationProperties(prefix = "spring.datasource.ds2")
@Component("ds2Properties")
@Data
public class Ds2Properties {

private String url;

private String username;

private String password;

private String driverClassName;

}

5. 注册数据源

我们在一个类中注册两个数据源就可以了。

package com.yyg.boot.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description 数据源的配置类
*/
@Configuration
public class DataSourceRegisterConfig {

/**
* 主数据源配置 ds1数据源
*/
@Primary
@Bean(name = "ds1Properties")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public DataSourceProperties ds1DataSourceProperties() {
return new DataSourceProperties();
}

/**
* 主数据源 ds1数据源
*/
@Primary
@Bean(name = "ds1DataSource")
public DataSource ds1DataSource(@Qualifier("ds1Properties") DataSourceProperties dataSourceProperties) {
//HikariDataSource","org.apache.tomcat.jdbc.pool.DataSource", "org.apache.commons.dbcp2.BasicDataSource
return dataSourceProperties.initializeDataSourceBuilder().build();
}

/**
* 第二个ds2数据源配置
*/
@Bean(name = "ds2Properties")
@ConfigurationProperties(prefix = "spring.datasource.ds2")
public DataSourceProperties ds2DataSourceProperties() {
return new DataSourceProperties();
}

/**
* 第二个ds2数据源
*/
@Bean("ds2DataSource")
public DataSource ds2DataSource(@Qualifier("ds2Properties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}

}

6. 配置数据源、连接工厂、事务管理器、扫描dao目录

注意合理的使用@Primary注解!

配置第一个数据源管理器

package com.yyg.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description 配置数据源、连接工厂、事务管理器、dao目录
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "managerFactory1", // 配置连接工厂
transactionManagerRef = "transactionManager1", // 配置事物管理器
basePackages = {"com.yyg.boot.dao.db01"} // 设置dao所在位置

)
public class ManagerFactory01Config {

/**
* 配置数据源,连接第1个数据源
*/
@Autowired
@Qualifier("ds1DataSource")
private DataSource ds1DataSource;

@Primary
@Bean(name = "managerFactory1")
public LocalContainerEntityManagerFactoryBean buildEntityManagerFactory1(EntityManagerFactoryBuilder builder) {
return builder
// 设置数据源
.dataSource(ds1DataSource)
//设置实体类所在位置.扫描所有带有 @Entity 注解的类
.packages("com.yyg.boot.entity")
// Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
// Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
.persistenceUnit("ds1PersistenceUnit")
.build();

}

/**
* 配置事务管理器
*/
@Bean(name = "transactionManager1")
public PlatformTransactionManager transactionManagerDatabase1(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(buildEntityManagerFactory1(builder).getObject());
}

}

配置第2个数据源管理器

package com.yyg.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description 配置数据源、连接工厂、事务管理器、dao目录
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "managerFactory2", // 配置连接工厂
transactionManagerRef = "transactionManager2", // 配置事物管理器
basePackages = {"com.yyg.boot.dao.db02"} // 设置dao所在位置

)
public class ManagerFactory02Config {

/**
* 配置数据源,连接第2个数据源
*/
@Autowired
@Qualifier("ds2DataSource")
private DataSource ds2DataSource;

@Bean(name = "managerFactory2")
public LocalContainerEntityManagerFactoryBean buildEntityManagerFactory2(EntityManagerFactoryBuilder builder) {
return builder
// 设置数据源
.dataSource(ds2DataSource)
//设置实体类所在位置.扫描所有带有 @Entity 注解的类
.packages("com.yyg.boot.entity")
// Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
// Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
.persistenceUnit("ds2PersistenceUnit")
.build();

}

/**
* 配置事务管理器
*/
@Bean(name = "transactionManager2")
public PlatformTransactionManager transactionManagerDatabase1(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(buildEntityManagerFactory2(builder).getObject());
}

}

7. 创建数据源类型

利用ThreadLocal确保线程安全性,每个线程之间不会相互影响。

package com.yyg.boot.datasource;

import lombok.extern.slf4j.Slf4j;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/7
* @Description 数据源类型
*/
@Slf4j
public class DataSourceType {

public enum SourceType {
/**
* 用户数据源
*/
DS_USER,
/**
* 商品数据源
*/
DS_SHOP
}

/**
* 使用ThreadLocal保证线程安全
*/
private static final ThreadLocal<SourceType> TYPES = new ThreadLocal<>();

/**
* 往当前线程里设置数据源类型
*/
public static void setDataSourceType(SourceType dataSourceType) {
if (dataSourceType == null) {
throw new NullPointerException();
}
log.warn("[设置当前数据源为]:" + dataSourceType);
TYPES.set(dataSourceType);
}

/**
* 获取数据源类型
*/
public static SourceType getDataSourceType() {
SourceType dataSourceType = TYPES.get() == null ? SourceType.DS_USER : TYPES.get();
log.warn("[当前数据源的类型为]:" + dataSourceType);
return dataSourceType;
}

/**
* 清空数据类型
*/
public static void removeDataSourceType() {
TYPES.remove();
}

}

8. 定义动态数据源

定义一个动态数据源,继承AbstractRoutingDataSource 抽象类,并重写determineCurrentLookupKey()方法。

AbstractRoutingDataSource这个类是实现多数据源的关键,作用是动态切换数据源。
在该类中有一个targetDataSources集合,该集合是AbstractRoutingDataSource的一个map类型的属性,其中key表示每个数据源的名字,value为每个数据源。
然后根据determineCurrentLookupKey()这个方法获取当前数据源在map中的key值,然后determineTargetDataSource()方法中动态获取当前数据源,如果当前数据源不存且默认数据源也不存在时就会抛出异常。

package com.yyg.boot.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/7
* @Description 动态切换数据源
*/
public class DynamicDataSource extends AbstractRoutingDataSource {

@Override
protected Object determineCurrentLookupKey() {
return DataSourceType.getDataSourceType();
}

}

9. 配置多个数据源

package com.yyg.boot.datasource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/7
* @Description 多数据源配置
*/
@Configuration
public class DynamicDataSourceConfig {

@Bean(name = "dynamicDataSource")
public DynamicDataSource dynamicDataSource(@Qualifier("ds1DataSource") DataSource ds1DataSource,
@Qualifier("ds2DataSource") DataSource ds2DataSource) {
Map<Object, Object> targetDataSource = new HashMap<>();
targetDataSource.put(DataSourceType.SourceType.DS_SHOP, ds1DataSource);
targetDataSource.put(DataSourceType.SourceType.DS_USER, ds2DataSource);
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSource);
dataSource.setDefaultTargetDataSource(ds2DataSource);
return dataSource;
}

}

10. 定义AOP切面

package com.yyg.boot.datasource;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/7
* @Description Description
*/
@Aspect
@Component
@Slf4j
public class DataSourceAop {

@Before("execution(* com.yyg.boot.service.impl.GoodsServiceImpl.*(..))")
public void setDataSource01() {
log.warn("db01商品数据源");
DataSourceType.setDataSourceType(DataSourceType.SourceType.DS_SHOP);
}

@Before("execution(* com.yyg.boot.service.impl.UserServiceImpl.*(..))")
public void setDataSource02() {
log.warn("db02用户数据源");
DataSourceType.setDataSourceType(DataSourceType.SourceType.DS_USER);
}

}

11. 创建Entity实体类

Goods商品类

package com.yyg.boot.entity;

import lombok.Data;

import javax.persistence.*;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description db1中的商品表
*/
@Entity
@Table(name = "goods")
@Data
public class Goods {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

private String name;

}

User用户类

package com.yyg.boot.entity;

import lombok.Data;

import javax.persistence.*;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description db4中的用户表
*/
@Entity
@Table(name = "user")
@Data
public class User {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

private String username;

private String birthday;

private String sex;

private String address;

}

12. 创建Dao层代码

GoodsRepository类

package com.yyg.boot.dao.db01;

import com.yyg.boot.entity.Goods;
import com.yyg.boot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description Description
*/
@Repository
public interface GoodsRepository extends JpaRepository<Goods, Long>,JpaSpecificationExecutor<User> {
}

UserRepository类

package com.yyg.boot.dao.db02;

import com.yyg.boot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description Description
*/
@Repository
public interface UserRepository extends JpaRepository<User, Long>,JpaSpecificationExecutor<User> {
}

13. 创建Service代码

UserServiceImpl实现类

package com.yyg.boot.service.impl;

import com.yyg.boot.dao.db02.UserRepository;
import com.yyg.boot.entity.User;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/7
* @Description Description
*/
@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRepository userRepository;

@Override
public List<User> findAll() {
return userRepository.findAll();
}

}

GoodsServiceImpl实现类

package com.yyg.boot.service.impl;

import com.yyg.boot.dao.db01.GoodsRepository;
import com.yyg.boot.dao.db02.UserRepository;
import com.yyg.boot.entity.Goods;
import com.yyg.boot.entity.User;
import com.yyg.boot.service.GoodsService;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/7
* @Description Description
*/
@Service
public class GoodsServiceImpl implements GoodsService {

@Autowired
private GoodsRepository goodsRepository;

@Override
public List<Goods> findAll() {
return goodsRepository.findAll();
}

}

14. 创建Controller接口

package com.yyg.boot.web;

import com.yyg.boot.entity.Goods;
import com.yyg.boot.entity.User;
import com.yyg.boot.service.GoodsService;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description Description
*/
@RestController
public class GoodsController {

@Autowired
private UserService userService;

@Autowired
private GoodsService goodsService;

@GetMapping(value = "/users")
public List<User> users() {
return userService.findAll();
}

@GetMapping(value = "/goods")
public List<Goods> goods() {
return goodsService.findAll();
}

}

15. 创建入口类

package com.yyg.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @Author 一一哥Sun
* @Date Created in 2020/4/3
* @Description Description
*/
@SpringBootApplication
public class DataSourceApplication {

public static void main(String[] args){
SpringApplication.run(DataSourceApplication.class,args);
}

}

16. 完整项目结构

SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源


17. 运行测试

我们首先测试一下goods接口,查询的是db1数据库里的数据。

SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源


对应的db1数据库里的数据。

SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源

然后再测试一下users接口,查询的是db4数据库里的数据。

SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源

对应数据库里的数据。

SpringBoot2.x系列教程48--多数据源配置之AOP动态切换数据源


至此,我们在Spring Boot中,利用JPA实现了配置两个数据源,其实也可以以此类推,配置3个,4个乃至更多的数据源!