commuicate ' User' model between two microservices - spring-boot

hello everyone i'm new to microservice's architecture ,
so i have a miscroservice which every entity is linked to the 'user' entity , but the problem is that the
'user' entity is in another microservice , is there any solution ?
I don't know where to user the #onetomany relation if i don't have the two entities in the same microservice)
i can't combine the two microservices together.
what about domain model layer ? is it the best solution to user another microservice's data ?
thanks in advance.

Cant you pass it as a secure jwt token which contains users data so its accessible in other micro service? Another approach is store it as a thread local object.

First answering your questions
1 - I don't know where to user the #onetomany relation if i don't have the two entities in the same microservice)
2 - i can't combine the two microservices together.
--> you can combine 2 services into one (provided considering the load of service)
If the load on your service is big - then
You can utilize something like shared database service for multiple microservices
Go through this blog - it talks about few patterns you can utilize.
http://microservices.io/patterns/data/database-per-service.html.
Also go through this question, it will give you some more undersntading
Microservices: how to handle foreign key relationships

Related

Adding Multiple Users in Django Rest frame work

I'm actually learning Django Rest Framework and I have this project that involves multiple user types so for example it consists of Students, Teachers and Admins each one with his attributes so I created a User model that inherits from AbstractUser and 3 other models for each type where each type has a foreign key of it's corresponding user. So here comes the problem how can I use Django-Rest-Auth library with these user types so for example when I use the register endpoint how can I modify it in order to create a Student in the students model based on the request data ?
I would appreciate any ones help.
Thanks in advance

Defining Microservice boundaries

I have started learning and building micro service based project, but I always stuck into scoping situation and end up creating a sort of monolith. in my below foo-bar example, please suggest what should be the scoping and how achieve desired output.
Services or tables
Employees
Department
Employee-Department-Mapping
Assumption
Employee or Department does not have any cross reference for each other all relationships are maintained in 3rd table Employee-Department-Mapping
Relationship could be One-To-Many or Many-To-Many based on business to business, in this example it is One-To-Many (Department with Employee)
Requirement
I want to get total salaries paid department wise. similar to below query, here I am making simple joins on 3 tables. which is only possible if all are in the same database and single micro service.
Select d.DepartmentName, SUM(e.salary)
from Employee e, Department d, Employee-Department-Mapping c
where d.DepartmentName == c.DepartmentName
AND e.Employee == c.Employee
Group By c.DepatmentId
constraints
employee table has salary information
employee department relationship is maintained by 3rd table.
I am not looking for exact answer but an approach to solve such problems.
wonder how would you design your micro service if you need aggregated outputs, would you bring all these tables in the single microservice ? I do not want pull millions of records from everymicroservice and do aggregation in memory.
Microservice boundries -if you don't have any other scalability reason- should be defined by the business. And in this particular case -without knowing any other requirements- I would say you should go for one microservice which manage those 2 entities. Having said that I experienced enough to know that in the real world this solution is not always feasible and possible. Fortunately there are some patterns you can follow to fix the situation you described. For example CQRS could be a solution https://dzone.com/articles/microservices-with-cqrs-and-event-sourcing.
You have to change your thinking to microservices here. what I can propose here is
You will have to do multiple calls to different microservices and return one result you can use API Gateway pattern. You will have to write an aggregator service to aggregate the output of the two tables via two API services.
You will have to denormalize the database and have the dept name in your Employees table if you don't like the above option. That's why we use NoSQL in microservices.

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
In my opinion your case is about how you specify your services especially how you define the bounded context of each service !!
According to the situation above-mentioned I don't see any reason why the product service should know anythings about orders (even if it s just the order-id) and backwards. One more problem I see in this case: your services will not work if one service is not available (E.g when the product service it not online, your order service will not be able to work, because he needs the product details from the product service...).
I Think you should rethink the bounded contexts of your microservices. You should keep in mind:
ensure a loose coupling of the microservices
a microservice has always to work even other Microservices are not available (Resilience / Reliability).
The DDD (domain-driven-design) paradigm with its tools provides her a great help to assist you, during the definition process of your services, you encourage these qualities.
So, the following is JUST an idea (it s not a recommendation and you should review whether it matters for your business case) :
It seems like the "order" process is your core business domain. So you have to focus on it.
The user service (i hope you mean here the customer and not a user in terms of authentication/authorization) has only the responsibility to manage the customers, and may be their adresses, bank-Accountings etc.. It should not know anything about your orders or products.
The same is valid for the product service. It owns only data about products. It has no relation either to the customer nor to the order-service.
The order service by itself deals only with orders and should own only data that belong to an order (like ship Adress and some information about the product-items ordered). I think the customer-Id is also important here to keep the relation between the order and the customer. This way you can e.g get all orders made by a certain customer-id....

Where is best to put the logic for coupled domain models?

A user requests latest news , the news get data from multiple sources (posts, users , photos, comments) . How would you model the news?
Is it good to have a gateway that couples these tables + a service that gets the data from the coupled gateway and handles the data as a response ? Or a domain model that couples the other models (this would mean to add in one of those gateways a joined long query that , in my opinion needs a separate gateway ).
I would create a NewsService, as it would be coordinating the creation of the news, but would defer any specific responsibility to the appropriate model. If it's a news feed, like in facebook, I would create another model, NewsItem which is created upon the entry of a new post, photo etc. This way, the responsibility of build the news would fall more into your domain model and your NewsService would be really just orchestrating the construction of the list. You could even, depending on your app, just use a NewsRepository.

doctrine2 polymorphic reference

I have a "Post" entity and I want users to vote for those posts. Votes by authenticated and anonymous users are being stored in separate DB tables, so there are two separate "VoteAnonymous" and "VoteAuthenticated" entities which implement the same interface.
Now I've got problem with defining a reference in a "Post" entity and its "targetEntity" option. I wonder if there is any way Doctrine2 could pick one of the polymorphic classes as its field's target entity.
Thanks for any help.
P.S. I'm not able to redesign the DB, there's a ton of legacy code that lies upon this data structure.
Doctrine supports inheritance, so you should create two different entities that share a common parrent, say AbstractVote, which defines all properties.
See this answer - it contains an example of such structure.

Resources