DEV Community 👩‍💻👨‍💻

Cover image for Spring internal micro-service driven communication
Elattar Saad
Elattar Saad

Posted on

Spring internal micro-service driven communication

During the solution architecture phase, especially when we're talking micro-services, we need a sort of internal exchange
of data, they're many tools to do so, such as Spring rest template and web client, but I'm using the one I find the simplest of them all:
Yes, OpenFeign!

Installing OpenFeign

First, we add the OpenFeign dependency (inside the pom.xml file), so it will be imported by Maven (our dependency manager).


Enter fullscreen mode Exit fullscreen mode

As you already noticed, we are using a tool which belongs to the spring cloud project, as result, we need to set the Spring cloud version we're using.
In side the properties tag, we'll add both, java and spring cloud versions:

Enter fullscreen mode Exit fullscreen mode

Please note that I'm using Spring boot version 2.6.1

Also note that I'll be using the movies project from the previous articles

After having all the needed dependencies, we still have to declare our app is using OpenFeign in a certain package.
We decorate our main class with the @EnableFeignClients with the target package which will have the feign clients.

public class MoviesApplication {
Enter fullscreen mode Exit fullscreen mode

OpenFeign client implementation

In the the implementation, I will be sending a simple get request to the jsonplaceholder API (acting as the second micro-service) to get a list of posts using OpenFeign:

In order to make use the Json/POJO auto-conversion which Spring Boot offers, we'll be using a DTO to manipulate the received/sent data (posts).

Inside the controller > dto package, we create the file:

public class PostDTO {

    private Long id;
    private Long userId;
    private String title;
    private String body;

Enter fullscreen mode Exit fullscreen mode

Note that the structure of the DTO is defined by the response of the jsonplaceholder API.

Next, we create our post client (feign client), which is going to be like the following:

@FeignClient(name = "jsonplaceholder", url = "")
public interface PostClient {

    List<PostDTO> getPosts();

Enter fullscreen mode Exit fullscreen mode

@FeignClient will declare this interface as a feign client in order to be implemented according to our needs.

The name attribute mainly useless in this case since we're not addressing the api with a name, but using its url instead. The name is used when a naming registry is integrated (a future article is on the way).

And just like magic, that's all for our client!

Now we need to call our client and return the response to our user. We'll create a PostController which does just that:

public class PostController {

    final PostClient postClient;

    public List<PostDTO> getPosts(){
        return postClient.getPosts();

Enter fullscreen mode Exit fullscreen mode

Finally, that's what you will get when invoking this PostController method:

More articles Here.

Top comments (2)

thorstenhirsch profile image
Thorsten Hirsch

Any reason why you've chosen to go with http instead of messaging?

xrio profile image
Elattar Saad

No not really, just wanna make it the simplest

Make Your Github Profile Stand Out

Github is great, but have you considered how to make yours more attractive for potential employers or other visitors? Even non-tech ones like recruiters!

Take a couple of hours and show your best side as a person - and a programmer.