在 Spring boot 中配置两个不同的数据库 - Postgres SQL 和 cassandra Db

问题描述 投票:0回答:1

在我们的应用程序中,数据库是使用 Spring boot starter JPA 和 application.properties 完成的配置 -

spring.datasource.url= postgres
spring.datasource.username=username
spring.datasource.password=password

现在,我想使用 spring data cassandra 的属性在同一应用程序中添加 cassandra 数据库:

spring.data.cassandra.XX

有人可以建议方法,以便我可以连接不同的存储库来连接到任一数据库。

我不想对现有的 JPA 存储库部分进行更改。

有没有办法单独配置 Cassandra 以及如何从同一应用程序连接两个不同的数据库。

spring spring-boot spring-data-jpa spring-data spring-data-cassandra
1个回答
0
投票

我们需要按照以下步骤操作。

  1. 在 application.properties 或配置文件中定义两个单独的数据库配置文件,一个用于 PostgreSQL,另一个用于 Cassandra(例如 tomcat context.xml)。
  2. 为两个数据库编写两个Java数据源配置文件,我们需要在其中配置entityManager,entityManagerFactory,transactionManager等
  3. 定义两个存储库,一个用于 PostgreSQL,另一个用于 Cassandra

这里是使用Mysql和Oracle数据库的工作示例。

© www.soinside.com 2019 - 2024. All rights reserved.