简体   繁体   English

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

[英]How can I configure multiple(more than two) data sources in my spring boot application in MySQL and MariaDB,

I have to connect to multiple data sources at runtime in a spring boot application with amazon RDS deployed MySQL and MariaDB instances respectively.我必须在 spring 引导应用程序中在运行时连接到多个数据源,其中亚马逊 RDS 分别部署了 MySQL 和 MariaDB 实例。 For now, both are MariaDB instances residing separately.目前,两者都是 MariaDB 个独立存在的实例。 I have used the usual spring boot configurations like here Multiple data source and schema creation in Spring Boot我已经使用了通常的 spring 启动配置,例如Multiple data source and schema creation in Spring Boot

I am facing a problem here and that is I am not able to connect to the secondary data source where as I am able to connect to the primary data source.我在这里遇到一个问题,那就是我无法连接到辅助数据源,而我可以连接到主数据源。 Here are some code snippets.下面是一些代码片段。

Also, I want to know any mechanism to fall back to other database instances of the same schemas at runtime if the original ones are down without affecting the application or restarting it.另外,我想知道在运行时回退到相同模式的其他数据库实例的任何机制,如果原始数据库实例关闭而不影响应用程序或重新启动它。 I have no idea about that.我对此一无所知。

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

I wish I could provide the entities and the repositories, but I can't.我希望我可以提供实体和存储库,但我不能。 Sorry for that.对不起。

I configured my database / datasource in spring in the application.properties file.我在 application.properties 文件中的 spring 配置了我的数据库/数据源。 Although I just had one DB, I tried it with multiple out of curiosity and it worked.虽然我只有一个数据库,但出于好奇,我尝试了多个数据库,结果成功了。 So maybe try it there?那么也许在那里试试? The format is as such:格式是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用多个数据源创建Spring Boot项目? - How can I make a Spring Boot project with multiple data sources? Spring Boot 配置和使用两个数据源 - Spring Boot configure and use two data sources Spring Boot应用程序中的两个数据源 - Two data sources in a Spring Boot application 为什么Spring Boot / Data无法自动配置多个数据源? - Why can't spring boot/data automagically configure multiple data sources? 在 Spring Boot (v2.6.6) 中,如何从两个数据源中提取并分别访问它们? - In Spring Boot (v2.6.6), how can I pull from two data sources and have them separately accessible? 如何在我的 spring 启动应用程序中使用 JSON 处理器而不是 jackson 或 gson? - How can I use a JSON processor rather than jackson or gson in my spring boot application? 如何使用Spring @Configuration类配置多个JPA数据源? - How do I configure multiple JPA data sources using Spring @Configuration class? 使用Spring Boot和多个数据源,如何在配置类之外指定@primary? - With Spring boot and multiple data sources how do I specify @primary outside the configuration class? Spring Boot Multi Data sources: How to configure multi spring.jpa properties in java class - Spring Boot Multi Data sources : How to configure multi spring.jpa properties in java class "如何在 application.properties 文件中的 Spring Boot 应用程序中配置 HikariCP?" - How do I configure HikariCP in my Spring Boot app in my application.properties files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM