Spring Cloud Spring Boot mybatis Distributed Microservice Cloud Architecture (19) Configuration and Use of Multiple Data Sources (1)

Keywords: Spring JDBC MySQL Database

Previously, when introducing the use of JdbcTemplate and Spring-data-jpa, single data sources were used. In the case of a single data source, the configuration of Spring Boot is very simple, just need to configure the connection parameters in the application.properties file. However, with the development of business, we usually split the database or introduce other databases, so we need to configure multiple data sources. The following two configurations of multiple data sources are introduced based on the previous examples of JdbcTemplate and Spring-data-jpa.

Multiple Data Source Configuration

Create a Spring configuration class that defines two DataSources to read different configurations in application.properties. In the following example, the primary data source is configured at the beginning of spring.datasource.primary, and the second data source is configured at the beginning of spring. DataSource. second.


@Configuration
public class DataSourceConfig {

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

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

}

The corresponding application.properties configuration is as follows:

spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

JdbcTemplate support

Support for JdbcTemplate is relatively simple, just need to inject the corresponding data source. For example, when creating JdbcTemplate, we inject data sources named primary datasource and secondary datasource to distinguish different JdbcTemplate.


@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
        @Qualifier("primaryDataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
        @Qualifier("secondaryDataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

Next, the test case demonstrates how to use these two JdbcTemplate for different data sources.


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {

	@Autowired
	@Qualifier("primaryJdbcTemplate")
	protected JdbcTemplate jdbcTemplate1;

	@Autowired
	@Qualifier("secondaryJdbcTemplate")
	protected JdbcTemplate jdbcTemplate2;

	@Before
	public void setUp() {
		jdbcTemplate1.update("DELETE  FROM  USER ");
		jdbcTemplate2.update("DELETE  FROM  USER ");
	}

	@Test
	public void test() throws Exception {

		// Insert two data into the first data source
		jdbcTemplate1.update("insert into user(id,name,age) values(?, ?, ?)", 1, "aaa", 20);
		jdbcTemplate1.update("insert into user(id,name,age) values(?, ?, ?)", 2, "bbb", 30);

		// Insert a data into the second data source. If the first data source is inserted, the primary key collision error will be reported.
		jdbcTemplate2.update("insert into user(id,name,age) values(?, ?, ?)", 1, "aaa", 20);

		// Check if there are two data in the first data source to verify that the insertion was successful
		Assert.assertEquals("2", jdbcTemplate1.queryForObject("select count(1) from user", String.class));

		// Check if there are two data in the first data source to verify that the insertion was successful
		Assert.assertEquals("1", jdbcTemplate2.queryForObject("select count(1) from user", String.class));

	}


}

Source of source code

Posted by Cochise on Wed, 06 Feb 2019 01:57:17 -0800