Skip to main content

Back-End Development

Using Feign Client in Spring Boot: Simplifying Microservice Communication

Social Network Online Sharing Connection Concept

Introduction to Feign Client:

In the world of software, microservices are like building blocks that help make programs more flexible and scalable. But for these blocks to work together, they need to talk to each other effectively. Imagine each block sharing information, asking for help, and working together smoothly.

Meet Feign Client, a super useful tool for microservices. As more microservices appear, they need to deal with the tricky parts of talking over the internet (HTTP communication). Feign Client, made by Netflix, jumps in to help by making asking for things between microservices easier. It’s like putting all the puzzle pieces together, making the services work together better and easier to handle.

 

What is Feign Client:

Think of Feign Client as a smart tool that helps microservices talk to each other online. As it’s core, Feign Client is a dynamic proxy generator for HTTP APIs. It’s like a special translator that helps them understand each other’s language (HTTP APIs). Feign Client is best buddies with Spring Cloud and Spring Boot, making it super easy for them to work together. This teamwork makes it simple for developers to make microservices chat with each other smoothly.

 

Setting Up a Spring Boot Project:

When setting up your Spring Boot project to use Feign Client, ensure that you include the necessary dependencies. For Maven projects, consider adding the following snippet to your pom.xml file of your project:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

 

Enable Feign Client in Main Spring Boot application class:

In your main Spring Boot application class, add the @EnableFeignClients annotation. This tells Spring Boot to enable Feign Client functionality.

package com.springboot.springbootfirstapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
 public class YourApplicationName {

    public static void main(String[] args) {
        SpringApplication.run(YourApplicationName.class, args);
    }
}

 

Creating Feign Client:

The process starts by creating a Feign Client interface using the @FeignClient annotation. This note mentions the name of the other service, so Feign knows where to send things. With this setup, you can list the things you want (declare methods) just like the other service has. Feign will use this interface to generate HTTP requests and then helps you by sending messages(HTTP requests) automatically.

 

package com.springboot.springbootfirstapp.feignclient;

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import com.example.microservices.response.AdressResponse;

@FeignClient(name = "EmployeService", url = "http://address-service-url")
public interface AdressFeignClient {
    
    @GetMapping("{id}")
    AdressResponse getAdressByEmployeId(@PathVariable("id") long id);
    
}

Here replace “http://address-service-url” with the actual URL of the “EmployeService

 

AdressResponse Java class:

package com.example.microservices.response;

public class AdressResponse {
    
    private Long id;
    private String lane1;
    private String lane2;
    private String state;
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getLane1() {
        return lane1;
    }
    
    public void setLane1(String lane1) {
        this.lane1 = lane1;
    }
    
    public String getLane2() {
        return lane2;
    }
    
    public void setLane2(String lane2) {
        this.lane2 = lane2;
    }
    
    public String getState() {
        return state;
    }
    
    public void setState(String state) {
        this.state = state;
    }
    
}

 

Use the Feign Client:

Now you can use the AdressFeignClient¬† interface in your services or controllers to interact with the “EmployeSerive” seamlessly.

import java.util.Arrays;
import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.microservices.response.AdressResponse;
import com.springboot.springbootfirstapp.feignclient.AdressFeignClient;
import com.springboot.springbootfirstapp.model.Employe;
import com.springboot.springbootfirstapp.repository.EmployeRepository;
import com.springboot.springbootfirstapp.response.EmployeResponse;

@Service
public class EmployeService {
    
    @Autowired
    private EmployeRepository employeRepository;
    
    @Autowired
    private ModelMapper modelMapper;

    @Autowired
    private AdressFeignClient adressFeignClient;
    
    public List<EmployeResponse> getAllEmployees() {
        List<Employe> employe = employeRepository.findAll();
        List<EmployeResponse> employeResponse =  Arrays.asList(modelMapper.map(employe, EmployeResponse[].class));
        employeResponse.forEach(emp->{
            AdressResponse adressResponse=adressFeignClient.getAdressByEmployeId(emp.getId());
            emp.setAdressResponse(adressResponse);
        });
        return employeResponse;
    }
}

That’s it! By following these steps, you’ve enabled and used Feign Client in your Spring Boot Application to communicate with the “EmployeService”. Feign Client takes care of generating and sending HTTP requests for you.

Output:

EmployeeService Communicating with AdressService.

Microsoftteams Image

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dinesh Reddy Koranda

Dinesh is an Associate Technical Consultant with over 2.5 years of expertise. He is a JAVA Backend developer and has worked with various technology stacks, including Java, Springboot, Microservices, Hibernate, API's and DB Technologies. In Dinesh's spare time, he enjoys playing cricket and watching films.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram