Spring Boot Tutorial: Spring Cloud Feign Client

Spring Boot Tutorial: Spring Cloud Feign Client

πŸš€ Welcome to Blog on learning about how the Spring Cloud Feign Client is used for inter - service communication in Spring Boot! πŸŒπŸ’Έ.

Get ready to embark on a captivating exploration into the realm of microservices communication. 🌟 In this upcoming blog series, we're diving deep into the intricacies of one of the most powerful tools in the Spring Cloud arsenal: Feign Client. πŸ’»πŸ”

Introduction

Certainly! In the world of Java development, Feign is a powerful and user-friendly HTTP client library. It's a part of the larger Spring Cloud ecosystem, designed to simplify the process of making HTTP requests.

Now, let's break it down a bit more for beginners:

What's HTTP Client Library ??

During our project work, we encountered scenarios necessitating interservice communication. Similarly, we might find the need to communicate with third-party APIs over the internet, for which we employ specialized libraries. The HTTP Client is a notable example. This library facilitates the issuance of requests to APIs, aiding in the structured retrieval or transmission of data.

Feign Client in a nutshellπŸ₯œ

Spring Cloud Feign Client is a declarative web service client. This implies that it simplifies the design process and provides an easy-to-use approach. The Feign client handles the intricacies of actual communication behind the scenes. It's akin to needing only the knowledge of how to initiate communication, leaving the details of its execution to the Feign client.

Why Feign Client??

  • Simplicity: Feign mitigates the complexity associated with crafting HTTP clients by offering a high-level abstraction. There's no need to grapple with the intricacies of establishing HTTP connections, managing requests, or manually handling responses.

  • Integration with Spring Cloud: In a Spring Cloud environment, Feign seamlessly integrates, presenting itself as an excellent choice for constructing microservices that necessitate smooth communication.

  • Declarative Approach: Feign adopts a declarative approach, allowing you to articulate API calls through annotations and interfaces. This results in code that is both clean and comprehensible. You merely declare your intentions, and Feign adeptly manages the rest.

Annotation based configuration

Feign leverages annotations to furnish configuration details, where we are gonna use the @EnableFeignClients. The @FeignClient annotation designates the interface as a Feign client. Meanwhile, the @GetMapping annotation delineates the HTTP method and endpoint. These annotations serve to abstract the intricacies associated with constructing URLs, encoding parameters, and overseeing headers.

Development:

Now, move to your favorite IDE or to the Spring Initializer and create a Spring boot Application with the following dependencies

Now, what are we gonna use?

In this instance, we will employ a Fake Store API for learning purposes.

Model Layer

We will exclusively utilize Data Transfer Objects (DTOs) since the data is retrieved from the API without the necessity for persistent storage in any database.

Product.java

package com.example.fakestoreapi.model.dto;

import lombok.Data;

@Data
public class Product {

    private int id;

    private String title;

    private String price;

    private String category;

    private String description;

    private String image;
}

External Layer

At this juncture, we will formulate an interface to configure the usage of OpenFeign. By creating the FakeStore interface within an external package, we can tailor its configuration to employ the "get all products" API.

FakeStore.java

package com.example.fakestoreapi.external;

import com.example.fakestoreapi.model.dto.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@FeignClient(url = "https://fakestoreapi.com", name = "fake-store-api")
public interface FakeStore {

    @GetMapping("/products")
    List<Product> getAllProducts();
}

Controller Layer

Now, let's expose the above method as API endpoint.

FakeStoreController.java

package com.example.fakestoreapi.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.fakestoreapi.external.FakeStore;
import com.example.fakestoreapi.model.dto.Product;

import java.util.List;

@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
public class FakeStoreController {

    private final FakeStore fakeStore;

    @GetMapping
    public List<Product> getAllProducts() {
        return fakeStore.getAllProducts();
    }
}

There is no need on any configuration, except the port and application name:

spring:
  application:
    name: fake-store-api
server:
  port: 8088

Conclusion

Thanks for reading our latest article on Spring Boot Tutorial: Spring Cloud Feign Client with practical usage.

You can get source code for this tutorial from our GitHub repository.

Happy Coding!!!!😊

Did you find this article valuable?

Support dev' Scribblz by becoming a sponsor. Any amount is appreciated!

Β