R
R
revukvr2021-03-22 23:12:21
Java
revukvr, 2021-03-22 23:12:21

How to correctly make interactions between modules in Java Spring?

I want to learn how to write java microservices. There is a project on which I want to organize all this. But the project is not big; 40 people use it. Now it is a monolith. On the Internet, I found that for such small projects as mine, not microservices are made, but a microservice monolith. Broke the project into microservices (11 in total). One of which is working with Amosrm, and the second is the main one who creates some records in the database and analyzes them. Modules do not communicate with each other with Rest, but directly call the controller method of another module. So it turns out that the amo module must interact with the main module and vice versa. How to include these modules in gradle? and how, in general, should modules interact in such an architecture? Otherwise, if I connect amo in the main module and vice versa, it throws an error:

Circular dependency between the following tasks:
:amocrm:compileJava
\--- :datadeal:compileJava
     \--- :amocrm:compileJava (*)


dependencies { //datadeal
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation project(':amocrm')
}

dependencies { //amocrm
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation project(':datadeal')
}


Controller example

@Controller
@AllArgsConstructor
public class OrderControllerImpl implements OrderController {

    private final OrdersService ordersService;

    @Override
    public void sendOrder(Long id) {
        ordersService.sendOrder(id);
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-03-23
@revukvr

Good afternoon.
I will assume that you still have implemented not a microservice monolith, but a monolithic architecture. You just divided the monolithic application into modules and included them in gradle. On the other hand, do you need a microservice monolith or a microservice in general if the application is quite small and only 40 people use it.
If we are just talking about the interaction of modules of a monolithic application, then in the corresponding gradle file of the module, connect the desired module and import the necessary services, classes, etc.
The main thing to note is that there is no cyclic dependency that you have indicated here:

Circular dependency between the following tasks:
:amocrm:compileJava
\--- :datadeal:compileJava
     \--- :amocrm:compileJava (*)

Related info -
https://reflectoring.io/spring-boot-gradle-multi-m...
https://spring.io/guides/gs/multi-module/
A circular dependency occurs when module A refers to a module B, and module B refers to module A. Neither gradle nor maven can solve this problem, since one module needs another module to build, and the first one for the other. It also suggests that the project may not have been correctly divided into modules. You can try to create module C, and put the classes common for modules A & B into it. And then add the dependency of module A on C, and module B also on C. Thus, there will be no cyclic dependency. Also pay attention to the recommendations for organizing the structure of the Spring application, which is indicated in the office. documentation on their website.
If we are talking about the interaction of modules of a microservice monolith, as it is distinguished in some sources, then the modules must be communicated through various asynchronous and synchronous integrations.
If you really need to divide the project into microservices, then how do I better implement a full-fledged microservice architecture through API Gateway. Accordingly, link modules via REST. Here you will already need Spring Cloud API Gateway, Spring Cloud Netflix Eureka Client & Server, etc.
It is possible that there are pitfalls that I am not aware of. I hope that more experienced colleagues will answer this question in more detail.
And so, a small article on this topic: https://habr.com/ru/post/496934/
Suddenly, you will be interested - a small introductory course on Spring Cloud, Eureka, Zulu, etc. -https://www.udemy.com/course/spring-boot-microserv...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question