user14197708
user14197708

Reputation:

Spring Boot Monolithic to Microservices

I have my API in Spring Boot. I have 2 APIs:

API 1 : Product

API 2 : Ingredient

A product consists of ingredients.

Here is my Ingredient Entity class:

        public class Ingredient{
          private Long id;
          private String name;
          private String unit;
          private Double quantity = 0.0;
          private Double componentCost = 0.0;
          private Double componentPrice = 0.0;
        }

I have my product Entity class as follows:

    public class Product{
          private Long id;
          private String name;
          @OneToMany(cascade= CascadeType.ALL)
          @JoinColumn
          private List<Ingredient> productComponents = new ArrayList<>();  <----
          private Double quantity = 0.0;
    }

As we can see productComponents is a List of "Ingredients" class type.

I realized that I am creating them in a Monolithic style, I want to create them following the Microservices architectural design.

This means that I have to create a separate project for each API.

My Question: How to achieve the following in the microservices way:

private List<Ingredient> productComponents = new ArrayList<>();

Since we are going to have "Product" in a project, and "Ingredient" in a separate project ?

Is there a better design to the above example ?

Upvotes: 1

Views: 938

Answers (3)

FishingIsLife
FishingIsLife

Reputation: 2372

I think many people are just following the trend of microservices just because it sounds good. The technical drawbacks are often underestimated. Can you give us a bigger picture why you want to switch to a microservice architechture? Now to your core question on sharing the models or dependencies. So you know the Definition or the principle of microservices? One of the main goals is decoupling. You want to decouple your product and your ingredient ( If your understanding of Domains or ddd is good is another topic). A real microservice archticture does mit even Share a database also when working in different tables/ collections. Use ids to get the representational state of an object. Microservices and Rest apis come with redundancy over complexity. So I recommend to think twice on your project if you really need to Switch. There are also hybrid solutions where you are just encapsulating a highly frequenced part and keep the Rest as a Monolith.

Upvotes: 0

Trần Tuấn Anh
Trần Tuấn Anh

Reputation: 333

Your entities should be placed in external project, build this and add jar file as lib into each subproject.

Upvotes: 0

Ezequiel
Ezequiel

Reputation: 136

First of all, you need to check if it makes sense to have Product and Ingredient in a separated project. It seems they belong to the same domain.

Talking about a microservices approach you should not declare Ingredient entity as dependency of Product.

private List<Ingredient> productComponents = new ArrayList<>();

You should declare the IDs only:

private List<Long> productComponents = new ArrayList<>();

Than you can retrieve the Ingredients entity by doing a call to Ingredients API based on Ids. In this case you could create a DTO with Product+Ingredient composition.

Using Ingredient as jar is not a microservice design, but modularization in my opinion.

Upvotes: 4

Related Questions