关于spring:Spring系列之多个数据源配置

3次阅读

共计 2873 个字符,预计需要花费 8 分钟才能阅读完成。

前言

在上篇文章讲到了如何配置单数据源,然而在理论场景中,会有须要配置多个数据源的场景,比如说,咱们在领取零碎中,单笔操作(蕴含查问、插入、新增)中须要操作主库,在批量查问或者对账单查问等对实时性要求不高的场景,须要应用读库来操作,顺次来加重数据库的压力。那么咱们如何配置多数据源?

这里还是基于 springboot 利用的状况下,咱们看一下怎么配置。
因为 SpringBoot 会实现主动配置,然而 SpringBoot 并不知道咱们的业务场景别离要应用哪一个数据源,因而咱们须要把相干的主动配置敞开。

首先,生成我的项目骨架,引入相应的依赖

  <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-jdbc</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>

     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <scope>runtime</scope>
     </dependency>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
     </dependency>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
     </dependency>

而后,在 Application 排除主动拆卸类

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,JdbcTemplateAutoConfiguration.class})
@Slf4j
public class MultiDataSourceDemoApplication {}

下面代码中,咱们排除了 DataSourceAutoConfiguration、DataSourceTransactionManagerAutoConfiguration、JdbcTemplateAutoConfiguration 三个类,而后就能够本人定义 DataSource 了。

配置数据源

    // 第一个数据源
    @Bean
    @ConfigurationProperties("first.datasource")
    public DataSource firstDataSource() {return DataSourceBuilder.create().build();}

    @Bean
    public JdbcTemplate firstJdbcTemplate() {return new JdbcTemplate(firstDataSource());
    }


    @Bean
    @Resource
    public PlatformTransactionManager firstTxManager(DataSource firstDataSource) {return new DataSourceTransactionManager(firstDataSource);
    }

    // 第二个数据源
    @Bean
    @ConfigurationProperties("second.datasource")
    public DataSource secondDataSource() {return DataSourceBuilder.create().build();}

    @Bean
    public JdbcTemplate secondJdbcTemplate() {return new JdbcTemplate(secondDataSource());
    }

    @Bean
    @Resource
    public PlatformTransactionManager secondTxManager(DataSource secondDataSource) {return new DataSourceTransactionManager(secondDataSource);
    }

application.properties 的配置项信息

management.endpoints.web.exposure.include=*
spring.output.ansi.enabled=ALWAYS

first.datasource.jdbc-url=jdbc:mysql://localhost:3306/first
first.datasource.username=root
first.datasource.password=xxx
second.datasource.jdbc-url=jdbc:mysql://localhost:3306/second
second.datasource.username=root
second.datasource.password=xxx

看一下表构造和数据

运行测试代码:

    @Test
    public void testMutilDataSource(){firstJdbcTemplate.queryForList("SELECT * FROM test1")
                .forEach(row -> log.info("记录:"+row.toString()));

        secondJdbcTemplate.queryForList("SELECT * FROM test2")
                .forEach(row -> log.info("记录:"+row.toString()));
    }

咱们看一下运行成果:

咱们能够看到,两个数据源都初始化胜利了,并且各自数据源执行的后果精确。

下面的形式没有集成 Mybatis,应用的是 jdbcTemplate,网络上还有很多配置形式,比方动静抉择数据源,大同小异,不过笔者还是倡议不同的业务独自指定数据源,容易保护。
咱们曾经演示了简略的单数据源和多数据源的配置形式,咱们下一篇文章将讲一下,SpringBoot 默认的连接池 HikariCP。

正文完
 0