简体   繁体   English

如何使用 Spring Boot 连接到多个 Cosmos DB?

[英]How to connect to more than one Cosmos DB using Spring Boot?

Here are my files:这是我的文件:

application.yml应用程序.yml

azure:
  cosmos:
    uri: ${COSMOS_URI:https://localhost:8081}
    key: ${COSMOS_KEY:...}
    database: ${COSMOS_DB:courses-service}

CosmosService.java CosmosService.java

@Service
public class CosmosService {

    private final RepoCosmos cosmos;

    public CosmosService(final RepoCosmos repoCosmos) {
        this.cosmos = repoCosmos;
    }

    public CourseCosmos saveCourse(final CourseCosmos courseCosmos) {
        cosmos.save(CourseCosmos);
        return courseCosmos;
    }

    public Optional<CourseCosmos> getCourse(final String id) {
        return cosmos.findById(id);
    }

    public void deleteById(final String id, final String partitionKey) {
        cosmos.deleteById(id, new PartitionKey(partitionKey));
    }
}

RepoCosmos.java RepoCosmos.java

@Repository
public interface RepoCosmos extends CosmosRepository<CourseCosmos, String> {
}

CourseCosmos.java CourseCosmos.java

@Container(containerName = "courses", ru = "400")
public class CourseCosmos {

    @Id
    @GeneratedValue
    private String id;

    @PartitionKey
    private String courseStatus;

    private String courseName
    
    // getters setters

}

I'm trying to figure out how to connect my project to a second database.我想弄清楚如何将我的项目连接到第二个数据库。 Somewhere along the lines of:沿着以下路线的某个地方:

azure:
  cosmos:
    uri: ${COSMOS_URI:https://localhost:8081}
    key: ${COSMOS_KEY:...}
    database1: ${COSMOS_DB:computing-courses-service}
    database2: ${COSMOS_DB:medical-courses-service}

When I try and google this, I'm not getting the results I'm expecting.当我尝试谷歌这个时,我没有得到我期望的结果。

EDIT:28/07/2021编辑:28/07/2021

After further research I found this: https://maanvi.medium.com/multi-cosmosdb-configuration-in-springboot-546c55d8d64经过进一步研究,我发现了这一点: https : //maanvi.medium.com/multi-cosmosdb-configuration-in-springboot-546c55d8d64

Connecting to more than one Cosmos Database appears to be relatively new to the Spring Boot library;连接多个 Cosmos 数据库对于 Spring Boot 库来说似乎相对较新; which is probably why I'm having trouble finding examples.这可能就是为什么我很难找到例子的原因。

Does anyone have a working example of connecting to more than one Cosmos DB using Spring Boot?有没有人有使用 Spring Boot 连接到多个 Cosmos DB 的工作示例?

I don't think it has anything to do with Cosmos.我不认为它与 Cosmos 有任何关系。

  • Split the repositories into packages according to different databases.根据不同的数据库将存储库拆分为包。
  • Create different per-database persistence configuration创建不同的每个数据库持久性配置

https://www.baeldung.com/spring-data-jpa-multiple-databases https://www.baeldung.com/spring-data-jpa-multiple-databases

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

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