简体   繁体   English

从两个不同的 Spring 引导应用程序访问相同的 MongoRepository 集合

[英]Accessing the same MongoRepository collection from two different Spring Boot Applications

I am working on two different spring boot services, that will need to access a common MongoRepository collection of Users.我正在处理两个不同的 spring 引导服务,它们需要访问一个公共的 MongoRepository 用户集合。

For the sake of simplicity, we have SpringBootApp1: User.java为了简单起见,我们有 SpringBootApp1: User.java

    @Document(collection = "user")
    public class User {
       @Id
       private String id;
       ....
    }

Then I get the repository as: UserRepository.java然后我将存储库获取为:UserRepository.java

public interface UserRepository extends MongoRepository<User, String> {

}

Now I need another application, SpringBootApp2 that reads the Users.现在我需要另一个读取用户的应用程序 SpringBootApp2。 I was planning to do the same in the second service, but then I would have two versions of User object, one defined on each service and both of them trying to read from the same MongoRepository collection.我计划在第二个服务中做同样的事情,但是我会有两个版本的用户 object,一个在每个服务上定义,并且两个都试图从同一个 MongoRepository 集合中读取。 If one User class is modified in one service, the other will not know and they will start being off-sync, on top of having repetition or code on both services.如果一个用户 class 在一个服务中被修改,另一个将不知道并且他们将开始不同步,除了在两个服务上都有重复或代码之外。

What would be the best approach in this case?在这种情况下最好的方法是什么?

Why you need SpringBootApp2 to read the User table again?为什么需要SpringBootApp2重新读取User表呢? You can expose an API in SpringBootApp1 which will fetch you the desired data by calling from the App2.您可以在 SpringBootApp1 中公开一个 API,它将通过从 App2 调用来获取所需的数据。 All your database related operations you can keep in one micro service or one app, and if you need the data from the database just expose methods in App1 and call that from App2.您可以将所有与数据库相关的操作保留在一个微服务或一个应用程序中,如果您需要数据库中的数据,只需在 App1 中公开方法并从 App2 中调用它。

UPDATE : As you said, App1 does READ-WRITE with the collection and grants access only to authenticated users, and App2 does a simple READ-ONLY activity, in that case, you can use the JPA repo interface in App2 with only the read from collection method implemented.更新:正如您所说,App1 对集合进行读写并仅向经过身份验证的用户授予访问权限,而 App2 执行简单的只读活动,在这种情况下,您可以在 App2 中使用 JPA 回购接口,仅读取采集方法实现。 You don't need any setters method for the Entity and no save/update method implemented for your interface.您不需要实体的任何设置方法,也不需要为您的界面实现任何保存/更新方法。 This design is harmless.这种设计是无害的。 You are just making a call to the same database from two different services.您只是从两个不同的服务调用同一个数据库。 No harm in that.那没有坏处。 Anyways you are always making a fresh query to the collection from App2.无论如何,您总是从 App2 对集合进行全新查询。

Else if you have concerns with this approach as well, and if you dont want to keep multiple DB read logics from different services, then ( I do not know what else functionalities your App1 offers other than DB operations ) I would like to suggest you to create App3 which is only for the sole purpose of DB operations.否则,如果您也对这种方法有顾虑,并且不想保留来自不同服务的多个数据库读取逻辑,那么(我不知道除了数据库操作之外您的 App1 还提供什么功能)我想建议您创建仅用于数据库操作的 App3。

In that case:在这种情况下:

  1. App1 will do security related operations + other services App1会做安全相关的操作+其他服务
  2. Design App3 which does DB manipulation work, without any direct user credentials or security设计执行数据库操作工作的 App3,无需任何直接用户凭据或安全性
  3. App1 will perform authentication, and on success call App3 for any DB READ-WRITE work, App3 wont need any security App1 将执行身份验证,成功后调用 App3 进行任何数据库读写工作,App3 不需要任何安全措施
  4. App2 will call App3 for any DB READ operations, App3 wont need any security App2 将调用 App3 进行任何 DB READ 操作,App3 不需要任何安全措施

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

相关问题 如何从 2 个不同的 spring 引导应用程序访问相同的数据库 - How to access the same DB from 2 different spring boot applications 如何远程调试在相同容器中运行的具有相同代码库但配置文件不同的两个spring boot应用程序? - How to remote debug two spring boot applications running in the same container with the same codebase but with different profiles? Spring Boot mongoRepository查询 - Spring Boot mongoRepository query 如何使用相同的log4j.properties文件记录两个不同的Spring-Boot应用程序? - How to log with same log4j.properties files for two different Spring-Boot Applications? 如何在同一台服务器上运行两个 Spring Boot 应用程序? - How to run two spring boot applications on same server? 在同一tomcat的不同端口上部署Angular 5和Spring Boot应用程序 - Deploy Angular 5 and spring boot applications on same tomcat at different ports Spring Boot MongoRepository忽略验证 - Spring Boot MongoRepository ignoring validation Spring Boot MongoRepository @Rollback 用于测试 - Spring Boot MongoRepository @Rollback for tests Spring Boot 无法扫描 MongoRepository - Spring Boot unable to scan MongoRepository 从两个不同的线程访问相同的变量 - Accessing same variable from two different threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM