VOOZH about

URL: https://www.geeksforgeeks.org/system-design/what-is-feign-client-in-microservices/

⇱ What is Feign Client in Microservices? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

What is Feign Client in Microservices?

Last Updated : 23 Jul, 2025

Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable small components with some specified responsibilities. It is considered the building block of modern applications.

FeignClient also known as Spring Cloud OpenFeign is a Declarative REST Client in Spring Boot Web Application. But what do you mean by Declarative REST Client? It means we need to specify the client specification as an Interface and Spring Boot will take care of the implementation for us. Writing web services with the help of FeignClient is very easier. FeignClient is mostly used to consume REST API endpoints which are exposed by third-party or microservice.

Let's understand the Feign Client by developing two Microservices using Java Spring Boot.

Example Project

In this project, we are going to develop two Microservices

  1. employee-service
  2. address-service

Developing employee-service Step by Step

Step 1: Create a New Spring Boot Project in Spring Initializr

To create a new Spring Boot project, please refer to How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA. For this project choose the following things

  • Project: Maven
  • Language: Java
  • Packaging: Jar
  • Java: 17

 Please choose the following dependencies while creating the project.

  • Spring Boot DevTools
  • Spring Data JPA
  • MySQL Driver
  • Spring Web
  • OpenFeign

Generate the project and run it in IntelliJ IDEA by referring to the above article.

Note: We have used the MySQL database in this project.

Step 2: Create Schema in MySQL Workbench and Put Some Sample Data

Go to your MySQL Workbench and create a schema named gfgmicroservicesdemo and inside that create a table called employee and put some sample data as shown in the below image. Here we have created 4 columns and put some sample data.

  1. id
  2. name
  3. email
  4. age

👁 Spring-Boot-Microservices-1

Now we are going to fetch Employee Data from Employee Table in our Spring Boot project. To do it refer to the following steps. Before moving to IntelliJ IDEA let's look at the complete project structure for our Microservices.

👁 Feignclient-1

Step 3: Make Changes in Your application.properties File

Now make the following changes in your application.properties file.

spring.datasource.url=jdbc:mysql://localhost:3306/gfgmicroservicesdemo
spring.datasource.username=put your username here
spring.datasource.password=put your password here
spring.application.name=employee-service
server.port=8080
# Set Your Context Path Here
server.servlet.context-path=/employee-service

Step 4: Create Your Entity/Model Class

Go to the src > main > java > entity and create a class Employee and put the below code. This is our model class.

Step 5: Create Your Repository Interface

Go to the src > main > java > repository and create an interface EmployeeRepo and put the below code. This is our repository where we write code for all the database-related stuff.

Note: Please refer to this article to know more about JpaRepository.

Step 6: Create an EmployeeResponse and AddressResponse Class

Go to the employee-service > src > main > java > response and create a class AddressResponse and put the below code.

Go to the src > main > java > response and create a class EmployeeResponse and put the below code.

Step 7: Create an AddressClient Interface

Go to the employee-service > src > main > java > feignclient and create an interface AddressClient and put the below code.

Step 8: Modify EmployeeServiceApplication Class

Go to the employee-service > src > main > java > EmployeeServiceApplication and annotate it with @EnableFeignClients annotation. Below is the complete code for EmployeeServiceApplication Class.

Step 9: Create Your Service Class

Now go to the employee-service > src > main > java > service > EmployeeService and modify the EmployeeService class as below. Add the below code inside this class.

@Autowired
private AddressClient addressClient;
// Using FeignClient
ResponseEntity<AddressResponse> addressResponse = addressClient.getAddressByEmployeeId(id);
employeeResponse.setAddressResponse(addressResponse.getBody());

Below is the complete code for EmployeeService Class.

Step 10: Create an Employee Controller

Go to the src > main > java > controller and create a class EmployeeController and put the below code. Here we are going to create an endpoint "/employees/{id}" to find an employee using id.

Step 11: Create a Configuration Class

Go to the src > main > java > configuration and create a class EmployeeConfig and put the below code.

Note: You may refer to these two articles

Before running the Microservice below is the complete pom.xml file. Please cross-verify if you have missed some dependencies

Developing address-service Step by Step

Step 1: Create a New Spring Boot Project in Spring Initializr

To create a new Spring Boot project, please refer to How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA. For this project choose the following things

  • Project: Maven
  • Language: Java
  • Packaging: Jar
  • Java: 17

 Please choose the following dependencies while creating the project.

  • Spring Boot DevTools
  • Spring Data JPA
  • MySQL Driver
  • Spring Web

Generate the project and run it in IntelliJ IDEA by referring to the above article.

Note: We have used the MySQL database in this project.

Step 2: Create Schema in MySQL Workbench and Put Some Sample Data

Go to your MySQL Workbench and create a schema named gfgmicroservicesdemo and inside that create a table called address and put some sample data as shown in the below image. 

Address Table:

Here we have created 4 columns and put some sample data.

  1. id
  2. city
  3. state
  4. employee_id

Note: In the Address table, employee_id is a foreign key so create it accordingly. We are going to perform a SQL join operation in our native SQL query. So create tables carefully.

👁 Spring-Boot-Native-Query-1

Before moving to IntelliJ IDEA let's have a look at the complete project structure for our Microservices.

👁 Spring-Boot-Native-Query-2

Step 3: Make Changes in Your application.properties File

Now make the following changes in your application.properties file.

spring.datasource.url=jdbc:mysql://localhost:3306/gfgmicroservicesdemo
spring.datasource.username=put your username here
spring.datasource.password=put your password here
spring.application.name=address-service
server.port=8081
server.servlet.context-path=/address-service

Step 4: Create Your Entity/Model Class

Go to the src > main > java > entity and create a class Address and put the below code. This is our model class.

Step 5: Create Your Repository Interface

Go to the src > main > java > repository and create an interface AddressRepo and put the below code. This is our repository where we write code for all the database-related stuff.

Note: Please refer to this article to know more about JpaRepository.

Step 6: Create an AddressResponse Class

Go to the src > main > java > response and create a class AddressResponse and put the below code.

Step 7: Create Your Service Class

Go to the src > main > java > service and create a class AddressService and put the below code. This is our service class where we write our business logic.

Step 8: Create an Address Controller

Go to the src > main > java > controller and create a class AddressController and put the below code. Here we are going to create an endpoint "/address/{employeeId}" to find the address using employee_id. Thats why we have created a foreign key in the Address table and we have performed the SQL join operation in the native query to get our desired result.

Step 9: Create a Configuration Class

Go to the src > main > java > configuration and create a class AddressConfig and put the below code.

Note: You may refer to these two articles

Before running the Microservice below is the complete pom.xml file. Please cross-verify if you have missed some dependencies

Run Both Your Address and Employee Microservices

Now run your both Address and Employee Microservices. If everything goes well then you may see the following screen in your console. Please refer to the below image.

👁 microservice-communication-2

Step 7: Test Your Endpoint in Postman

Now open Postman and hit the following URL

GET: http://localhost:8080/employee-service/employees/2

And you can see the following response

{
"id": 2,
"name": "Asish",
"email": "asis@gmail",
"age": "30",
"addressResponse": {
"id": 1,
"city": "BLS",
"state": "Odisha"
}
}

Please refer to the below image.

👁 microservice-communication-3

Comment

Explore