首页 > 解决方案 > Use DB Relationships in spring boot micro services

问题描述

I want to use the many to one and other DB Relationship in micro-service architecture. In monolithic architecture we can create the entity relationship easily as they belongs to same project but in micro-service architecture how we can achieve the same.

Example:

There is one userDeatil service and other is productDetail service.Now there is third service called orderDetail and an order will have userID and ProductIDs associated with it. So how can we manage the relationship between 'user and order' and 'order and product'.

I have searched over net but didn't able to get the fair idea.There is another thread having same query but not having the clear answer. Link

标签: spring-bootforeign-keysmicroservicesentity-relationship

解决方案


在我看来,您的情况是关于如何指定服务,尤其是如何定义每个服务的有界上下文!

根据上述情况,我看不出产品服务有什么理由应该知道有关订单的任何信息(即使它只是 order-id)和倒退。我在这种情况下看到的另一个问题:如果一项服务不可用,您的服务将无法工作(例如,当产品服务不在线时,您的订单服务将无法工作,因为他需要来自产品服务的产品详细信息...)。

我认为您应该重新考虑微服务的有界上下文。你应该记住:

  • 确保微服务的松耦合
  • 即使其他微服务不可用,微服务也始终可以工作(弹性/可靠性)。

DDD(领域驱动设计)范式及其工具为您提供了很大的帮助,在您的服务定义过程中,您鼓励这些品质。

因此,以下只是一个想法(这不是建议,您应该查看它是否对您的业务案例重要):

  • 看起来“订单”流程是您的核心业务领域。所以你必须专注于它。
  • 用户服务(我希望您在这里指的是客户,而不是身份验证/授权方面的用户)只负责管理客户,可能是他们的地址、银行会计等。它不应该知道您的任何信息订单或产品。
  • 产品服务也是如此。它只拥有有关产品的数据。它既与客户无关,也与订单服务无关。
  • 订单服务本身只处理订单,并且应该只拥有属于订单的数据(如发货地址和有关订购的产品项目的一些信息)。我认为客户 ID 在这里也很重要,以保持订单和客户之间的关系。通过这种方式,您可以获得例如由某个客户 ID 发出的所有订单....

推荐阅读