我如何在MySQL和MariaDB的春季启动应用程序中配置多个(两个以上)数据源



我必须连接到多个数据源在运行时在一个spring启动应用程序与亚马逊RDS部署MySQL和MariaDB实例分别。目前,两者都是独立的MariaDB实例。我使用了通常的弹簧引导配置,就像这里一样在Spring Boot中创建多个数据源和模式

我在这里面临一个问题,那就是我无法连接到辅助数据源,因为我能够连接到主要数据源。下面是一些代码片段:

另外,我想知道在运行时如果原来的数据库实例关闭而不影响应用程序或重新启动应用程序的情况下,是否有任何机制可以退回到相同模式的其他数据库实例。我不知道。

import com.zaxxer.hikari.HikariDataSource;
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.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.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "a.b.c.d.e",
entityManagerFactoryRef = "aEntityManagerFactory",
transactionManagerRef = "aTransactionManager"
)
public class aConfiguration {
@Bean
@Primary
@ConfigurationProperties("app.datasource.a")
public DataSourceProperties aDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("app.datasource.a.configuration")
public DataSource aDataSource() {
return aDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
}
@Primary
@Bean(name = "aEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean aEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(aDataSource())
.packages(SomeQualifiedEntity.class)
.build();
}
@Primary
@Bean
public PlatformTransactionManager aTransactionManager(
final @Qualifier("aEntityManagerFactory") LocalContainerEntityManagerFactoryBean aEntityManagerFactory) {
return new JpaTransactionManager(aEntityManagerFactory.getObject());
}
}
import com.zaxxer.hikari.HikariDataSource;
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.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.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "u.v.w.x.y",
entityManagerFactoryRef = "bEntityManagerFactory",
transactionManagerRef = "bTransactionManager"
)
public class aConfiguration {
@Bean
@Primary
@ConfigurationProperties("app.datasource.b")
public DataSourceProperties bDataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
@ConfigurationProperties("app.datasource.b.configuration")
public DataSource bDataSource() {
return bDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
}
@Primary
@Bean(name = "bEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean bEntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(bDataSource())
.packages(SomeQualifiedEntity.class)
.build();
}
@Primary
@Bean
public PlatformTransactionManager bTransactionManager(
final @Qualifier("bEntityManagerFactory") LocalContainerEntityManagerFactoryBean bEntityManagerFactory) {
return new JpaTransactionManager(aEntityManagerFactory.getObject());
}
}
app.datasource.a.url=
app.datasource.a.username=
app.datasource.a.password=
app.datasource.a.driverClassName=org.mariadb.jdbc.Driver
app.datasource.b.url=
app.datasource.b.username=
app.datasource.b.password=
app.datasource.b.driverClassName=org.mariadb.jdbc.Driver

我希望我能提供实体和存储库,但是我不能。对不起。

我在应用程序中用spring配置了数据库/数据源。属性文件。虽然我只有一个DB,但出于好奇,我尝试了多个DB,结果很有效。也许可以在那里试试?格式如下:

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.data.jpa.repositories.bootstrap-mode=default

相关内容

  • 没有找到相关文章

最新更新