• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Configuring multiple data sources in springboot 2.0

King Wang

1 月 3, 2022

Personal blog address :https://alexaccele.github.io/

Because in springboot2.0 There are many problems when configuring multiple data sources in , Also read a lot of Daniel’s blog, and finally solved the problem , So I’ll record my solution here .

In the configuration file url Should be change to the use of sth. jdbc-url

At the beginning, I configured url, The result will be   jdbcUrl is required with driverClassName Error of

Here is the correct configuration

spring:
datasource:
primary:
jdbc-url: jdbc:mysql://localhost:3306/moneysaller?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
backup:
jdbc-url: jdbc:mysql://localhost:3306/moneysaller-backup?useUnicode=true&characterEncoding=utf-8&useSSL=true
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver

Pay attention to the use of @Primary annotation

The following is the injection of master-slave in a unified configuration class DataSource, Pay attention to Lord DataSource Top annotation @Primary annotation

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Configuration
public class DataAccessConfiguration {
@Bean
@Primary
@ConfigurationProperties("spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.backup")
public DataSource backupDataSource() {
return DataSourceBuilder.create().build();
}
}
import com.springboot.entity.Order;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
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.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager",
basePackages = {"com.springboot.seller.repositorys"})
public class PrimaryConfig {
@Resource
@Qualifier("primaryDataSource")
private DataSource primaryDataSource;
@Resource
private JpaProperties jpaProperties;
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.packages(Order.class)
.persistenceUnit("primary")
.build();
}
@Primary
@Bean(name = "primaryEntityManager")
public EntityManager primaryEntityManager(EntityManagerFactoryBuilder builder) {
return primaryEntityManagerFactory(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "primaryTransactionManager")
public PlatformTransactionManager primaryTransactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(primaryEntityManagerFactory(builder).getObject());
}
protected Map<String,Object> getVendorProperties(DataSource dataSource){
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
}
import com.springboot.entity.VerificationOrder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "backupEntityManagerFactory",
transactionManagerRef = "backupTransactionManager",
basePackages = {"com.springboot.seller.repositorysbackup"})
public class BackupConfig {
@Resource
@Qualifier("backupDataSource")
private DataSource backupDataSource;
@Resource
private JpaProperties jpaProperties;
@Bean
public LocalContainerEntityManagerFactoryBean backupEntityManagerFactory(
EntityManagerFactoryBuilder builder) {
return builder
.dataSource(backupDataSource)
.packages(VerificationOrder.class)
.persistenceUnit("backup")
.build();
}
@Bean(name = "backupEntityManager")
public EntityManager backupEntityManager(EntityManagerFactoryBuilder builder) {
return backupEntityManagerFactory(builder).getObject().createEntityManager();
}
@Bean(name = "backupTransactionManager")
public PlatformTransactionManager backupTransactionManager(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(backupEntityManagerFactory(builder).getObject());
}
protected Map<String,Object> getVendorProperties(DataSource dataSource){
return jpaProperties.getHibernateProperties(new HibernateSettings());
}
}

 

When not used @Primary When the annotation , A message similar to the following appears on the console , Missing annotations in different places in the main configuration , There will be similar messages , The difference lies in the number of parameters described and different methods

***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method backupEntityManagerFactory in com.springboot.seller.configuration.BackupConfig required a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' in your configuration.

Above entity class Order and VerificationOrder The corresponding repository The directory structure of is as follows

The purpose of the above separation is to avoid one item being covered due to the different loading order of master-slave data sources .

发表回复