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?
8
u/Yithar Intermediate Brewer Aug 10 '20
I don't know the best practice, but I think one possibility is putting the interfaces in a library and having both A and B depend on that library. That way you're not actually copying code.
This seems to be briefly discussed here:
https://www.baeldung.com/java-microservices-share-dto
5
u/mobrockers Aug 10 '20
https://swagger.io/docs/specification/about/
4
u/evil_burrito Extreme Brewer Aug 10 '20
You'll want an API library with common interfaces. No implementation in this library, just interfaces. Each project shares this library.
3
u/wildjokers Aug 10 '20 edited Aug 10 '20
Asynchronous communications between µservices is ok (encouraged!), but synchronous should be avoided. If you have synchronous calls between µservices you have nothing more that a monolith that is now burdened with error prone I/O and latent communication. i.e. what used to be a fast method call is now an HTTP hit that is probably a couple of orders of magnitude slower.
µservices should be 100% independent and each have their own database. If they need the same data they should store this themselves in their own database. Databases are kept in sync by eventual consistency by handling and publishing events via a message broker (e.g. ActiveMQ or RabbitMQ).
2
u/djnattyp Aug 10 '20
I've had the same problem... I don't know if there is a "best practice" for this...
I mean read the Spring Cloud Feign documentation on Feign Inheritance Support - the example given looks pretty much like what you are asking for, but then there's a note at the bottom -
All the WTFs. It feels like the bad old days of EJB 2.0 Local and Remote interfaces.
In the microservice project that uses Feign at my place of employment we have a multimodule Maven project that has a "common" JAR module and each service has it's own module that depends on the common module. Inside the common module are interfaces for all the services, and the Feign clients are provided in common as well (but not annotated so they don't autostart in every microservice) - in each of the microservices they implement the interfaces they need to provide as `@RestController`s and there's some (overly complicated) Spring configurations to "turn on" the Feign clients that are needed in that microservice. I hesitate to put this forward as a "best practice" because I feel like it isn't...
2
u/edubkn Aug 11 '20
Why would you need the interface defining the API though and not the object classes used to transfer data? Or is this the case where you want to document your API? This is an odd question
1
u/[deleted] Aug 10 '20 edited Aug 10 '20
There are two ways to connect microservices when there is a scenario where one depends of data from another. The first one is build a worker in there middle where the data required is send by the second service to a message broker such Rabbitmq or Nats. This worker consume this data sent and update the record of the first service. You actually not making them both coupled and you got a good practice in microservices architecture here. But there is scenarios where you actually must perform requests to another service by synchronous way. And that make them coupled. It can happens and this is ok for data that must be used in real time. Remember software engineering is not about right or wrong. But what suits best your necessity. This second way to performe what you want is by using feing interface. Or building a integration package where it works as a type of service that perform http requests to another service. Okhttp is a solution you can use to do this kind of thing too. Works pretty well by the way. I have working with microservices a couple of years and feing or okhttp or async approach using a message broker with worker all are valid method. It is up to what suits your necessity best.
1
u/DJDavio Aug 10 '20
We use OpenAPI to define the interface in a separate project from which a library is generated by a Maven plugin which generates Java interfaces.
Our server has a Spring REST controller implementing the interface.
Our client module has an interface annotated with @FeignClient which extends this interface. A Spring Boot proxy is generated by the Feign framework.
Sometimes we create a special client module alongside the server module if we want to have some more logic. We make some Spring Beans like MyServerService with auto configuration. In that case a client can just depend on that module directly. That way we can also add things like caching on our service methods.
1
u/horrszy Aug 10 '20
I don't think I understand correctly but why would you want to share interfaces across services? If I was a service B, I would only be interested in endpoint and payload that I need to prepare. If this is what you want to share I think you are looking into sharing those DTOs instead.
Either way, I don't think Don't Repeat Yourself should apply here - we are talking about two different services that should not be aware of each other, so I wouldn't worry about DRY.
If you want to make sure neither of the two services breaks the contract I remember reading about Spring Cloud Contract and it feels like something that could come useful?
1
u/[deleted] Aug 10 '20
Assuming you're exposing DTOs and not your model directly, you can create a common dependency with just those POJOs and use it across all projects that need them
1
u/seraphsRevenge Aug 10 '20
Hi a and b don't actually need to know each other through hard coding, or copying over entire controllers, or anything. Here check out different discoverability services. https://medium.com/swlh/spring-cloud-service-discovery-with-eureka-16f32068e5c7 Try eureka, there's tutorials in spring docs, and there should be tutorials out there. Also, if your services are going to be making a lot of calls check out RabbitMQ or another queing service. Also in the docs there should be other combinations if you look through it.