如何让Spring Data处理多个异构DataSource? [英] How to get Spring Data to handle multiple heterogeneous DataSources?

查看:85
本文介绍了如何让Spring Data处理多个异构DataSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功使用使用JPA访问数据教程对于春天。我已经获得了自己的CrudRepository来自动配置特定的DataSource @Bean,它们之间的内部连接由Spring Data(或Spring Boot管理,很难分辨哪个)。

I've been successful using using the Accessing Data With JPA tutorial for Spring. I've gotten a CrudRepository of my own to work automatically by just configuring a specific DataSource @Bean, and the internal connections between these are managed by Spring Data (or Spring Boot, it's hard to tell which).

但是,我无法弄清楚如何让自动化管道来处理第二个DataSource @Bean。注入第二个会导致自动配置类在启动期间爆炸。

However, I can't figure out how to get that automated plumbing to handle a second DataSource @Bean. Injecting a second one causes the autoconfiguration classes to explode during startup.

有关如何执行此操作的任何想法?我为此做的搜索导致文章讨论了多个同类数据源以实现负载平衡或其他目的,这实际上不是我需要的。我有多个具有完全独立内容的数据库,我需要将这些内容添加到此应用程序中,我真的希望避免因为第二个数据库进入混合而复制所有自动配置。

Any thoughts as to how to do this? The searches I've done for this resulted in articles discussing multiple homogeneous DataSources for load balancing or other purposes, which is really not what I need. I have multiple databases with completely separate content that I need to pull into this app and I'd really like to avoid having to replicate all that automated configuration just because a second database entered the mix.

我希望这很简单,但我担心它在自动配置中是不受支持的边缘情况。

I'm hoping this is simple, but I'm fearful that it's an unsupported edge case in the autoconfiguration.

推荐答案

您可以创建两个数据源和实体管理器,其中一个bean标记为@Primary

You can create two datasources and entitymanagers, one bean of them mark as @Primary

@Configuration
@EnableJpaRepositories(basePackages = "io.eddumelendez.springdatajpa.repository1")
public class FirstConfiguration {

    @ConfigurationProperties(prefix = "datasource.postgres")
    @Bean
    @Primary
    public DataSource postgresDataSource() {
        return DataSourceBuilder.create().
                build();
    }

    @Bean(name = "entityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean emf1(EntityManagerFactoryBuilder builder){
        return builder
                .dataSource(postgresDataSource())
                .packages("io.eddumelendez.springdatajpa.domain1")
                .persistenceUnit("users")
                .build();
    }

}

另一个数据源的配置:

@Configuration
@EnableJpaRepositories(basePackages = "io.eddumelendez.springdatajpa.repository2", entityManagerFactoryRef = "emf2")
public class SecondConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "datasource.mysql")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean emf2(EntityManagerFactoryBuilder builder){
        return builder
                .dataSource(mysqlDataSource())
                .packages("io.eddumelendez.springdatajpa.domain2")
                .persistenceUnit("customers")
                .build();
    }

}

你的application.properties应该看起来像这个:

Your application.properties should looks like this:

datasource.mysql.url=jdbc:mysql://localhost:3306/mysql_demo
datasource.mysql.username=root
datasource.mysql.password=root

datasource.postgres.url=jdbc:postgresql://localhost:5432/postgres_demo
datasource.postgres.username=postgres
datasource.postgres.password=postgres

这篇关于如何让Spring Data处理多个异构DataSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆