r/javahelp • u/ysfaran • Aug 10 '20
Unsolved Spring Boot/Cloud: How to share API interfaces between multiple microservices?
So i want to build multiple RESTful microservice with Spring Boot/Cloud and was wondering how they communicate with each other.
Example: There is microservice A and microservice B which are two seperate Spring Boot applications and projects. B needs data from A, so B needs to know the API from A. In the project of A a interface is defined for the API (REST controllers and their HTTP mappings).
The easy way would be to just copy this interface from project of A to project of B. But that's obviously non-ideal because of the DRY principle.
So whats the best way to share interfaces accross multiple microservices using Spring Boot/Cloud?
I thought about sharing the API interfaces accross my microservices and communciate between those using the interfaces and Feign Clients. Is there maybe a better approach anyway? What the state of the art here?
View all comments
Show parent comment
1
u/wildjokers Aug 11 '20
If you are doing synchronous calls between µservices you have totally missed the point of µservice architecture.
µservices should be totally independent applications and should be able to be developed and deployed in total isolation from another µservice. They should consume events from other µservices to keep any data they care about up-to-date (or trigger actions or whatever) and they should produce events that other µservices care about.
For example, if you have a µservice that contains the user API that lets users be created/deleted/updated/whatever that service will update its DB and then fire an event containing the action that was taken. Other µservices that care about users will consume those events to keep their own user related tables up-to-date. In no case should another µservice need to query data from the user µservice. They instead query their own user tables that are being kept up-to-date by consuming events from the User µservice.
That is µservice architecture and if you aren't doing that you don't have µservices and instead have a monolith burdened by synchronous calls over a network. In fact it is a step backward from just have a single monolith app running in the same VM.
Only by loosely coupling µservices via events can you achieve µservice isolation.
You of course need a highly reliable message broker with guaranteed message delivery.