“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1, the introduction of

Feign is a REST client library that defines REST clients in an interface-driven manner. Eureka service registry client in The Netflix system of Spring Cloud supports Ribbon client load balancer, while Feign is essentially the Ribbon packaging, which implements service search and load balancing through the Ribbon.

In the Spring Cloud Netflix system, we usually conduct service communication in the following two ways:

  1. RestTemplate supports load balancing
  2. Feign generated client interface

Both methods use the Ribbon to search for services, and then use load balancing strategies to select services for service communication. The specific method depends on your preferences.

A little bit about Feign:

You often hear people say OpenFeign and people say Feign, which gives the illusion that it’s two things. In fact, Feign was also an open source project of Netflix, and later it became a separate open source project, renamed OpenFeign. This is quite common, as HarmonyOS has Open HarmonyOS.

2, the body

Before you start learning About Feign, you need to have some knowledge about microservices. You can check out the previous articles of this column to get started with microservices and Eureka, and build a Eureka service and client to learn about Feign.

2.1 Service Construction

Set up a Eureka Server for service registration discovery, and prepare two identical service provider Servers and one service CONSUMER. The overall services are as follows:

2.2 RestTemplate

Since Feign, like RestTemplate, uses the Ribbon to find services, let’s take a look at how the RestTemplate consumes services in the microservice architecture above. The server code posted here will be the same in Feign and will not be posted again.

1, Service provider Server-01 exposes REST endpoint:

2. Service provider Server-02 exposes REST endpoints:


3. Declare the RestTemplate bean and add the @loadBalanced annotation. This annotation tells Spring Boot that the RestTemplate needs to be able to find services through the Ribbon.


4. The service Consumer uses RestTemplate to consume the service code:

There are two service providers. With the help of the Ribbon and Eureka client, service discovery and load balancing can be realized. Note that the service address is no longer hard-coded, but written as the service name Server (case insensitive) registered by the service provider on Eureka.

5. Request the/Consumer endpoint provided by Consumer from any Http client, and refresh the request continuously. As you can see, the RestTemplate will train server-01 and server-02 in turn

2.3 Feign Client

1. Import dependencies

2. Add a configuration class to start Feign Client

Define the Feign interface

This interface is defined and no implementation class is required. When Feign discovers this interface when Spring Boot is running, Feign automatically creates an implementation class and exposes it as a bean in the Spring application context

4. Send the request through the Feign client interface

Injection Feign interface, you can directly call the method in the interface (implementation is done by Feign) to initiate a request.

5. Request the/Consumer endpoint provided by Consumer from any Http Client, refresh the request continuously, and you can see that Feign Client will rotate server-01 and server-02 in turn

2.4 summarize

This article gives you an introduction to Feign, along with a brief look at the use of RestTemplate. Note that Fegin is not responsible for resolving service names or load balancing, which is implemented by its integrated Ribbon. Feign can replace the RestTemplate, and the code is much more readable by comparison, but the overall performance is slightly lower.