Spring Boot多数据源配置指南

Spring Boot 如何实现多数据源配置

在Spring Boot中实现多数据源配置主要涉及到以下几个步骤:

  • 配置多个数据源
    application.propertiesapplication.yml配置文件中,你需要为每个数据源定义不同的配置。例如:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 数据源1
    spring.datasource.primary.url=jdbc:mysql://localhost:3306/db1
    spring.datasource.primary.username=root
    spring.datasource.primary.password=secret
    spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver

    # 数据源2
    spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db2
    spring.datasource.secondary.username=root
    spring.datasource.secondary.password=secret
    spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver

    或者使用YAML格式:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    spring:
    datasource:
    primary:
    url: jdbc:mysql://localhost:3306/db1
    username: root
    password: secret
    driver-class-name: com.mysql.cj.jdbc.Driver
    secondary:
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password: secret
    driver-class-name: com.mysql.cj.jdbc.Driver
  • 配置数据源Bean
    在Spring Boot的配置类中,你需要为每个数据源创建相应的DataSource Bean。使用@Primary注解可以指定默认的数据源。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    @Configuration
    public class DataSourceConfig {

    @Primary
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
    }
    }
  • 配置事务管理器
    对于每个数据源,你还需要配置相应的PlatformTransactionManager Bean。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Bean(name = "primaryTransactionManager")
    public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "secondaryTransactionManager")
    public PlatformTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
    }
  • 配置JPA实体管理器(如果使用JPA):
    如果你使用JPA,需要为每个数据源配置EntityManagerFactoryJpaTransactionManager

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    @Bean(name = "primaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(
    EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {
    return builder
    .dataSource(dataSource)
    .packages("com.example.primary.model") // 指定实体类所在的包
    .persistenceUnit("primary")
    .build();
    }

    @Bean(name = "secondaryEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean secondaryEntityManagerFactory(
    EntityManagerFactoryBuilder builder, @Qualifier("secondaryDataSource") DataSource dataSource) {
    return builder
    .dataSource(dataSource)
    .packages("com.example.secondary.model") // 指定实体类所在的包
    .persistenceUnit("secondary")
    .build();
    }

    @Bean(name = "primaryJpaTransactionManager")
    public JpaTransactionManager primaryJpaTransactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
    }

    @Bean(name = "secondaryJpaTransactionManager")
    public JpaTransactionManager secondaryJpaTransactionManager(@Qualifier("secondaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
    }
  • 在Service中指定使用的数据源
    在业务逻辑层,你可以通过@Primary注解或者@Transactional注解的transactionManager属性来指定使用哪个事务管理器,从而控制使用哪个数据源。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    @Service
    public class MyService {

    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Transactional(transactionManager = "primaryTransactionManager")
    public void usePrimaryDataSource() {
    // 使用primaryDataSource的业务逻辑
    }

    @Transactional(transactionManager = "secondaryTransactionManager")
    public void useSecondaryDataSource() {
    // 使用secondaryDataSource的业务逻辑
    }
    }

通过上述步骤,你可以在Spring Boot应用中配置并使用多个数据源。