微服务实现不同登陆_不同微服务之间的联接表

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.

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?

解决方案

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:

SELECT * FROM Users WHERE some condition is true

Get list of Users back including their ids.

ProductMicroserivce:

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

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

The reverse can also be done, ProductMicroserivce:

SELECT * FROM Products WHERE some condition is true

Extract all the UserIds then call the UserMicroservice:

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

你可能感兴趣的:(微服务实现不同登陆)