SoFunction
Updated on 2025-03-09

The role of @FeignClient annotation in SpringBoot

In microservice architecture, calls between services are very frequent. To simplify calls between services, Spring Boot provides a component called Feign. Feign can help us define and implement the RESTful interface between services, making calls between services more convenient and reliable. In this article, we will dive into what the @FeignClient annotation is, its principles, and how to use it.

What is @FeignClient annotation?

The @FeignClient annotation is a component in Spring Cloud that is implemented based on Netflix Feign. The @FeignClient annotation can help us define and implement the RESTful interface between services, making calls between services more convenient and reliable. The @FeignClient annotation can be used for client API interface definitions, which can convert an HTTP API interface into a Java interface, allowing us to call remote services like local methods.

@FeignClient Annotation Principle

The principle of @FeignClient annotation is very simple, it is based on Spring Cloud and Netflix Feign. The @FeignClient annotation can convert an HTTP API interface into a Java interface and generate a proxy object to implement calls between services. The @FeignClient annotation can be automatically injected into Ribbon for load balancing, making calls between services more stable and reliable.

The core components of the @FeignClient annotation include , FeignClientFactoryBean and FeignClientsRegistrar.

  • : A proxy object used to generate Feign. A Java interface can be generated based on the specified HTTP API interface and automatically injected into Ribbon for load balancing.

  • FeignClientFactoryBean: A proxy object used to create Feign. FeignClientFactoryBean can generate a proxy object based on the specified HTTP API interface and inject it into the Spring container.

  • FeignClientsRegistrar: Used to register @FeignClient annotations. FeignClientsRegistrar can scan all @FeignClient annotations in a project and register them into a Spring container.

How to use @FeignClient annotation?

Let's take a look at how to use the @FeignClient annotation in Spring Boot. For simplicity of demonstration, we will create a service provider and a service consumer and use the @FeignClient annotation to make service calls.

Create a service provider

First, we need to create a Spring Boot project and add Spring Web-related dependencies.

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Then we need to create a RESTful interface and return a string.

@RestController
public class ProviderController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Create a service consumer

Next, we need to create a service consumer and use the @FeignClient annotation to make the service call. We can use Feign to simplify the code of service calls.

First, we need to add Spring Cloud and Feign dependencies.

<dependency>
    <groupId></groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

Then, we need to add Feign's configuration to the configuration file. In the following configuration file, we set Feign's logging level to FULL.

feign:
  client:
    config:
      default:
        loggerLevel: full

Next, we can create a Feign interface to call the service provider's interface.

@FeignClient("provider")
public interface ProviderClient {
    @GetMapping("/hello")
    String hello();
}

Finally, we can inject ProviderClient into the service consumer and call its hello() method to call the service provider's interface.

@RestController
public class ConsumerController {
    private final ProviderClient providerClient;

    public ConsumerController(ProviderClient providerClient) {
         = providerClient;
    }

    @GetMapping("/hello")
    public String hello() {
        return ();
    }
}

Test service call

Now that we have completed the creation of service providers and service consumers, we can start service providers and service consumers and conduct tests on service calls.

First, we need to start the service provider. In the service provider's startup class, we need to add the @EnableEurekaClient annotation and add the Eureka configuration in the configuration file.

@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {
    public static void main(String[] args) {
        (, args);
    }
}
spring:
  application:
    name: provider

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

Then, we need to start serving consumers. In the startup class serving consumers, we need to add the @EnableFeignClients annotation and add Feign's configuration in the configuration file.

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args) {
        (, args);
    }
}
spring:
  application:
    name: consumer

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

feign:
  client:
    config:
      default:
        loggerLevel: full

Finally, we can access the service consumer interface in the browser to test whether the service call is successful.

http://localhost:8080/hello

If the service call is successful, we should be able to see the following output in the browser:

Hello, World!

Summarize

The @FeignClient annotation is a very important component in Spring Boot. It can help us define and implement the RESTful interface between services, making calls between services more convenient and reliable. In this article, we explore in-depth the principles of the @FeignClient annotation and how to use it in Spring Boot to implement calls between services. Through the study of this article, I believe that readers have mastered the basic principles and usage methods of @FeignClient annotations, and can flexibly apply them in actual projects.

This is the end of this article about the role of @FeignClient annotation in SpringBoot. For more related SpringBoot @FeignClient content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!