Cause:
Configuring a data source We know we can write the configuration in a YML file or properties and use the SpringBoot auto-configuration feature to do the binding, but what if we need to rely on more than one data source?
Solution:
Keep different data source configurations separate to avoid mixing them together, and pay attention to which data source you are operating on.
First of all we need to eliminate SpringBoot automatic configuration of data sources, using exclude attributes DataSourceAutoConfiguration, DataSourceTransactionManagerAutoConfiguration, JdbcTemplateAutoConfiguration ruled out. As follows:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, JdbcTemplateAutoConfiguration.class })
@Slf4j
public class MultiDataSourceApplication {
public static void main(String[] args) { SpringApplication.run(MultiDataSourceApplication.class, args); }}Copy the code
This eliminates the automatic configuration of data sources by the framework and allows us to control them manually. Take a look at the Properties configuration file, which declares that the two data source urls are default and text.
management.endpoints.web.exposure.include=*
spring.output.ansi.enabled=ALWAYS
default.datasource.url=jdbc:h2:file:~/default
default.datasource.username=MRyan
default.datasource.password=123456
text.datasource.url=jdbc:h2:file:~/text
text.datasource.username=
text.datasource.password=
Copy the code
Then we manually configure a data source and create a configuration class DataSourceProperties binding Properties profile. For the data source we use the default URL declared in the profile.
/** * default data source * use@ConfigurationPropertiesAssign DataSourceProperties to get the application.properties configuration file */
@Bean
@ConfigurationProperties("default.datasource")
public DataSourceProperties defaultSourceProperties(a) {
return new DataSourceProperties();
}
Copy the code
Then create the DataSource bean and print the log to view the DataSource URL.
/** * Create DataSource bean */
@Bean
public DataSource defaultDataSource(a) {
DataSourceProperties dataSourceProperties = defaultSourceProperties();
log.info("default datasource:{}", dataSourceProperties.getUrl());
return dataSourceProperties.initializeDataSourceBuilder().build();
}
Copy the code
Then create the corresponding transaction manager for the DataSource Bean created above
/** * Create a transaction manager */ for the DataSource Bean
@Bean
@Resource
public PlatformTransactionManager defaultManager(DataSource defaultDataSource) {
return new DataSourceTransactionManager(defaultDataSource);
}
Copy the code
At this point we have a data source configured and can run the output without any problems.
In the same way, we configure the second data source, text
/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
/** * text data source * use@ConfigurationPropertiesAssign DataSourceProperties to get the application.properties configuration file */
@Bean
@ConfigurationProperties("text.datasource")
public DataSourceProperties textSourceProperties(a) {
return new DataSourceProperties();
}
/** * Create DataSource bean */
@Bean
public DataSource textDataSource(a) {
DataSourceProperties dataSourceProperties = textSourceProperties();
log.info("text datasource:{}", dataSourceProperties.getUrl());
return dataSourceProperties.initializeDataSourceBuilder().build();
}
/** * Create a transaction manager */ for the DataSource Bean
@Bean
@Resource
public PlatformTransactionManager textManager(DataSource textDataSource) {
return new DataSourceTransactionManager(textDataSource);
}
Copy the code
The final code looks like this:
/ * * *@descriptionHow does SpringBoot configure multiple data sources@Author MRyan
* @Date2020/10/8 for *@Version1.0 * /
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, JdbcTemplateAutoConfiguration.class })
@Slf4j
public class MultiDataSourceApplication {
public static void main(String[] args) {
SpringApplication.run(MultiDataSourceApplication.class, args);
}
/** * default data source * use@ConfigurationPropertiesAssign DataSourceProperties to get the application.properties configuration file */
@Bean
@ConfigurationProperties("default.datasource")
public DataSourceProperties defaultSourceProperties(a) {
return new DataSourceProperties();
}
/** * Create DataSource bean */
@Bean
public DataSource defaultDataSource(a) {
DataSourceProperties dataSourceProperties = defaultSourceProperties();
log.info("default datasource:{}", dataSourceProperties.getUrl());
return dataSourceProperties.initializeDataSourceBuilder().build();
}
/** * Create a transaction manager */ for the DataSource Bean
@Bean
@Resource
public PlatformTransactionManager defaultManager(DataSource defaultDataSource) {
return new DataSourceTransactionManager(defaultDataSource);
}
/ / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
/** * text data source * use@ConfigurationPropertiesAssign DataSourceProperties to get the application.properties configuration file */
@Bean
@ConfigurationProperties("text.datasource")
public DataSourceProperties textSourceProperties(a) {
return new DataSourceProperties();
}
/** * Create DataSource bean */
@Bean
public DataSource textDataSource(a) {
DataSourceProperties dataSourceProperties = textSourceProperties();
log.info("text datasource:{}", dataSourceProperties.getUrl());
return dataSourceProperties.initializeDataSourceBuilder().build();
}
/** * Create a transaction manager */ for the DataSource Bean
@Bean
@Resource
public PlatformTransactionManager textManager(DataSource textDataSource) {
return newDataSourceTransactionManager(textDataSource); }}Copy the code
Run and logNormal operation, no problems.
In the actuator, Beans is in the zone