不同微服务之间的联接表 [英] Join table between Different Microservices

查看:663
本文介绍了不同微服务之间的联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在尝试理解微服务架构.

I am still trying to make sense of micro service architecture.

分离不同应用程序(包括数据库)的想法使我兴奋.但是,如果有两个微服务,我仍然感到困惑.产品和用户.产品和用户各自在其数据库中拥有表产品和用户.根据微服务的最佳实践,我们只能从该服务访问数据库.

The idea to separate different application (include the database) excites me. But I am still confused if there are two micro-services e.g. Product and User. both product and user own table product and user respectively in their database. According to best practice in micro service, we only can access the database from the service.

问题是,让我们假设我们的产品表具有user_id列.我们要搜索产品,该产品还返回创建该产品的用户的名称.这需要在产品微服务中的产品表和用户微服务中的用户表之间进行联接.你如何处理呢?

The problem is, let us suppose we have product table that has user_id column. We want to do search product which also return the name of the user who create the product. This requires join between product table in product micro-service and user table in user micro-service. How do you handle this?

推荐答案

您必须调用每个微服务并手动进行联接,或者将相关的用户ID传递给每个服务.

You'll have to make a call to each microservice and do the join manually or pass in the relevant user ids to each service.

UserMicroservice:

UserMicroservice:

SELECT * FROM Users WHERE some condition is true

获取返回的用户列表,包括其id.

Get list of Users back including their ids.

ProductMicroserivce:

ProductMicroserivce:

SELECT * FROM Products WHERE some condition is true AND userId IN (.........)

因此,用户和产品仍可以位于两个不同的数据库中,这些产品仅需要具有userId的概念.

So users and products can still be in two different databases, the products will just need to have the concept of a userId.

相反,ProductMicroserivce也可以做到:

The reverse can also be done, ProductMicroserivce:

SELECT * FROM Products WHERE some condition is true

提取所有UserId,然后调用UserMicroservice:

Extract all the UserIds then call the UserMicroservice:

SELECT * FROM Users WHERE some condition is true AND id IN (.........)

这篇关于不同微服务之间的联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆