作者:一一哥
随着并发量的不断增加,单个数据库可能难以承受高并发所带来的压力,此时我们通常会对数据库进行拆分或是引入其他数据库,一个项目中使用多个数据库(无论是主从复制- - 读写分离还是分布式数据库结构)的重要性变得越来越明显,那么我们就需要配置多个数据源。
一般项目中整合多数据源有两种方法:分包和AOP。
下面我就先基于Spring-data-jpa,以分包的方式来配置多数据源。
我们按照之前的经验,创建一个web程序,并将之改造成Spring Boot项目,具体过程略。

org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
com.alibaba
druid
1.1.10
在该配置文件中,要进行两个数据库的配置,本案例中我们使用默认的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
第一个数据库配置类
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;
}
我们在一个类中注册两个数据源就可以了。
package com.yyg.boot.config;
import com.zaxxer.hikari.HikariDataSource;
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 DataSourceConfig {
/**
* 主数据源配置 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();
}
}
注意合理的使用@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());
}
}
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;
}
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,JpaSpecificationExecutor {
}
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,JpaSpecificationExecutor {
}
package com.yyg.boot.web;
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 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 UserRepository userRepository;
@Autowired
private GoodsRepository goodsRepository;
@GetMapping(value = "/users")
public List users() {
return userRepository.findAll();
}
@GetMapping(value = "/goods")
public List goods() {
return goodsRepository.findAll();
}
}
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);
}
}

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

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

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

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