简体   繁体   English

Spring 多个数据库(数据源)之间的引导切换

[英]Spring Boot switch between multiple databases (data-sources)

I have a spring boot REST API application for a project with 2 different setups ( =2 different jars).我有一个 spring 启动 REST API 应用程序,用于具有 2 个不同设置(= 2 个不同 jar)的项目。 Both need to be realized, but I have no idea on how to do it or what the best way would be.两者都需要实现,但我不知道如何做到这一点或最好的方法是什么。

Both setups need to have a connection with an online database (on a server; AWS) and a connection with an offline or local database (running on the machine/pc itself).两种设置都需要与在线数据库(在服务器上;AWS)建立连接,并与离线或本地数据库(在机器/PC 本身上运行)建立连接。 It is the offline or local connection that is different between both setups.两种设置之间的不同之处在于离线或本地连接。


Setup 1:设置 1:
When the application is started it needs to connect to the online database.当应用程序启动时,它需要连接到在线数据库。 When an error occurs or connection to the online database is lost, it needs to connect to the offline/local database.当发生错误或与在线数据库的连接丢失时,需要连接到离线/本地数据库。

It is not required that that it will reconnect with the online database after an error occurred or the connection was lost.发生错误或连接丢失后,它不需要重新连接在线数据库。

Setup 2:设置 2:
When the application is started it needs to connect to both the online and the offline database.当应用程序启动时,它需要连接到在线和离线数据库。 So when the user does a post to the REST API, both the online and the offline database need to be updated (unless an error occurs or the connection to the online database is lost).所以当用户向REST API发帖时,在线和离线数据库都需要更新(除非发生错误或与在线数据库的连接丢失)。 If the user just does a get request, it is preferred to get the data from the online database, unless an error occurs or the connection to the online database is lost, than it can use the offline database.如果用户只是做一个get请求,最好从在线数据库中获取数据,除非发生错误或者与在线数据库的连接丢失,否则可以使用离线数据库。

It is not required that that it will reconnect with the online database after an error occurred or the connection was lost, but in this setup it would be nice.发生错误或连接丢失后,它不需要重新连接在线数据库,但在此设置中会很好。 Synchronizing the data after connection has been established again isn't required either (but would maybe be a nice feature).再次建立连接后同步数据也不是必需的(但可能是一个不错的功能)。


I have seen a similar post where the solution was to use ha-jdbc but it is an old post... Maven just doesn't find the dependency when I try adding it to my pom.xml file. 我看过 一个类似的帖子,其中解决方案是使用 ha-jdbc 但它是一个旧帖子... Maven 只是在我尝试将它添加到我的 pom.xml 文件时找不到依赖项。

After some more searching and trying I was able to add ha-jdbc to my pom.xml.经过更多的搜索和尝试,我能够将 ha-jdbc 添加到我的 pom.xml 中。 What I had to do was add a repository that contained ha-jdbc.我必须做的是添加一个包含 ha-jdbc 的存储库。

 <project> <repositories> <repository> <id>jbossrepository</id> <name>jbossrepository</name> <url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases/</url> </repository> </repositories> <dependency> <dependency> <groupId>net.sf.ha-jdbc</groupId> <artifactId>ha-jdbc</artifactId> <version>3.0.3</version> </dependency> </dependency> </project>

Yet to see if I can get it to work and if it is what I am looking for... 然而,看看我是否可以让它工作,如果它是我正在寻找的......
ha-jdbc isn't worth trying, one error or problem after the other, it's just old java too... ha-jdbc 不值得尝试,一个接一个的错误或问题,它也只是旧的 java...


Versions:版本:
- java: 1.8 - java:1.8
- org.springframework.boot (spring-boot-starter-parent): 2.1.8.RELEASE - org.springframework.boot(spring-boot-starter-parent):2.1.8.RELEASE
- Database (online & offline): PostgreSQL 11.5 - 数据库(在线和离线):PostgreSQL 11.5

Take a look this example code .看看这个示例代码 There you can find already implemented a working solution.在那里您可以找到已经实施的有效解决方案。 The logic behind it is EnableJpaRepositories which enables it based on packages ( basePackages )它背后的逻辑是EnableJpaRepositories ,它基于包( basePackages )启用它

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM