1

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 ?

  • You need to introduce `Ingredient` as a dependency inside the `Product` class . Read more about maven dependency management and packaging your files as jar. – Dev-vruper May 04 '21 at 14:35
  • @Dev-vruper you mean to include the name of the project like Ingredient in maven in the Product project ? –  May 04 '21 at 14:36
  • 1
    https://stackoverflow.com/q/15383322/6309111 . You will need to first package your Ingredient class as a jar and then put in in another project's pom file – Dev-vruper May 04 '21 at 14:41
  • Thank you @Dev-vruper , I am going to be deploying this project to AWS, Do I need to do anything else with that dependency/jar file in deployment ? –  May 04 '21 at 14:48
  • I'm not aware about AWS , so can't help on that part. – Dev-vruper May 04 '21 at 14:55

3 Answers3

4

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.

Ezequiel
  • 136
  • 3
0

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

0

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.

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51