我们在Spring Boot应用程序中使用了多个数据源配置。这两个数据源仅属于mysql。
使用以下命令配置多个数据源:
pom.xml更改:
<!-- exclude tomcat jdbc connection pool, use HikariCP -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- exclude tomcat-jdbc, Spring Boot will use HikariCP automatically -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
.properties:
spring.db1.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db1?zeroDateTimeBehavior=convertToNull
spring.db1.datasource.driverClassName=com.mysql.jdbc.Driver
spring.db1.datasource.username=root
spring.db1.datasource.password=
spring.db1.datasource.hikari.maximum-pool-size=5
spring.db1.datasource.hikari.minimum-idle=1
spring.db2.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db2?zeroDateTimeBehavior=convertToNull
spring.db2.datasource.driverClassName=com.mysql.jdbc.Driver
spring.db2.datasource.username=root
spring.db2.datasource.password=
spring.db2.datasource.hikari.maximum-pool-size=5
spring.db2.datasource.hikari.minimum-idle=1
数据源Bean配置:
@Bean
@ConfigurationProperties(prefix = "spring.db1.datasource")
public DataSource db1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.db2.datasource")
public DataSource db2Source() {
return DataSourceBuilder.create().build();
}
启动应用程序时,默认情况下会创建db1(@Primary数据源)的10个连接,但不考虑最大池大小和最小空闲属性。 同样,它没有为db2创建连接。
是否缺少任何配置?请帮助
答案 0 :(得分:0)
确保您拥有@Primary
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.db1.datasource")
public DataSource db1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.db2.datasource")
public DataSource db2Source() {
return DataSourceBuilder.create().build();
}
根据https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html,您可以按照以下说明定义属性
spring.db1.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db1?zeroDateTimeBehavior=convertToNull
spring.db1.datasource.username=root
spring.db1.datasource.password=
spring.db1.datasource.maximum-pool-size=5
spring.db1.datasource.minimumIdle=1
spring.db2.datasource.jdbcUrl=jdbc:mysql://localhost:3306/db2?zeroDateTimeBehavior=convertToNull
spring.db2.datasource.username=root
spring.db2.datasource.password=
spring.db2.datasource.maximum-pool-size=5
spring.db2.datasource.minimumIdle=1
还可以使用DataSourceBuilder来基于连接字符串自动检测驱动程序。
您可以定义logging.level.com.zaxxer.hikari.HikariConfig=TRACE
可以看到您的连接属性正常工作。
答案 1 :(得分:0)
看一下我的解决方法:https://github.com/dijalmasilva/spring-boot-multitenancy-datasource-liquibase
使用此解决方案,您以后可以添加更多数据源,而无需更改代码!
答案 2 :(得分:0)
我有一个类似的问题,即非主数据库无法连接。
最后,我进行了以下更改,并成功运行了- 您可以在以下位置定义池大小和其他参数 用于特定数据库配置的DataSourceProperties。
首先请检查您的包裹结构-
com.example.product.entity.db1
com.example.product.entity.db2
com.example.product.repository.db1
com.example.product.repository.db2
com.example.product.configuration
DB1Configuration
DB2Configuration
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "db1EntityManagerFactory",
transactionManagerRef="db1TransactionManager",
basePackages="com.example.product.repository.db1"
)
public class DB1Configuration
{
@Bean(name = "db1DataSourceProperties")
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties db1DataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "db1DataSource")
public DataSource db1DataSource()
{
return db1DataSourceProperties().
initializeDataSourceBuilder().
type(HikariDataSource.class).build();
}
@Primary
@Bean(name = "db1EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("db1DataSource")DataSource dataSource){
return builder.dataSource(dataSource)
.packages("com.example.product.entity.db1").
persistenceUnit("db1").build();
}
@Primary
@Bean(name="db1TransactionManager")
public PlatformTransactionManager reconTransactionManager(@Qualifier("db1EntityManagerFactory")EntityManagerFactory reconEntityManagerFactory)
{
return new JpaTransactionManager(db1EntityManagerFactory);
}
}
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder.Builder;
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;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "db2EntityManagerFactory",
transactionManagerRef ="db2TransactionManager",
basePackages={"com.exmaple.product.repository.db2"}
)
public class DB2Configuration
{
@Bean(name = "db2DataSourceProperties")
@ConfigurationProperties("db2.datasource")
public DataSourceProperties db2DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "db2DataSource")
public DataSource db2DataSource()
{
return db2DataSourceProperties().
initializeDataSourceBuilder().
type(BasicDataSource.class).build();
}
@Bean(name = "db2EntityManagerFactory")
public LocalContainerEntityManagerFactoryBean
itmsEntityManagerFactory(final EntityManagerFactoryBuilder builder)
{
Builder dataSource = builder.dataSource(db2DataSource());
return dataSource.
packages("com.example.product.entity.db2").persistenceUnit("db2").build();
}
@Bean(name = "db2TransactionManager")
public PlatformTransactionManager itmsTransactionManager(@Qualifier("db2EntityManagerFactory")EntityManagerFactory db2EntityManagerFactory)
{
return new JpaTransactionManager(db2EntityManagerFactory);
}
}
application.properties
spring.datasource.driver = com.mysql.cj.jdbc.Driver
spring.datasource.url= jdbc:mysql://localhost:3306/db1
spring.datasource.username=root
spring.datasource.password=root
db2.datasource.driver=com.mysql.cj.jdbc.Driver
db2.datasource.url=jdbc:mysql://localhost:3306/db2
db2.datasource.username=root
db2.datasource.password=root