Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 1, 2022 08:04 pm GMT

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).

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-openfeign</artifactId>    <version>3.1.0</version></dependency><dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies></dependencyManagement>

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:

<properties>    <java.version>1.8</java.version>    <spring-cloud.version>2021.0.0</spring-cloud.version></properties>

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.

@SpringBootApplication@EnableFeignClients("io.xrio.movies.controller.client")public class MoviesApplication {  ...}

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 PostDTO.java file:

@Datapublic class PostDTO {    private Long id;    private Long userId;    private String title;    private String body;}

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 = "https://jsonplaceholder.typicode.com/posts")public interface PostClient {    @GetMapping    List<PostDTO> getPosts();}

@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:

@RestController@RequestMapping("post")@AllArgsConstructorpublic class PostController {    final PostClient postClient;    @GetMapping    public List<PostDTO> getPosts(){        return postClient.getPosts();    }}

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

More articles Here.


Original Link: https://dev.to/xrio/spring-internal-micro-service-driven-communication-5co8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To