Springboot — FeignClient

FeignClient Springboot microservice

Deepu George Jacob
4 min readDec 6, 2023

FeignClient is a concept and annotation used in the Spring Cloud framework, specifically in the context of building microservices-based applications. Spring Cloud is a set of tools and frameworks within the Spring ecosystem that facilitates the development of distributed systems and microservices.

In the context of microservices, it’s common for one microservice to need to communicate with another. The @FeignClient annotation in Spring Cloud is used to create a declarative web service client. This annotation is applied to an interface, and Spring Cloud generates a proxy at runtime for that interface. This proxy simplifies the process of making HTTP requests to other microservices.

Feign client need dependency in your microservice project build.gradle file

implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

Also you have add the @EnableFeignClients

The @EnableFeignClients annotation in Spring Cloud is used to enable the Feign client functionality in a Spring Boot application. When you apply this annotation to a configuration class or the main application class, it tells Spring to scan for interfaces annotated with @FeignClient and create the necessary proxy beans for them.

Here’s a simple example:

@SpringBootApplication
@EnableFeignClients
public class MyApplication {

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

In this example, the @EnableFeignClients annotation is used in conjunction with @SpringBootApplication. This enables Feign clients in the Spring Boot application and allows Spring to discover and create beans for interfaces annotated with @FeignClient.

The @EnableFeignClients annotation can take additional attributes, such as basePackages or basePackageClasses, to specify the packages where Feign clients should be scanned. This can be useful if your Feign client interfaces are in specific packages.

@SpringBootApplication
@EnableFeignClients(basePackages = "com.example.feign")
public class MyApplication {

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

In this example, Feign clients will be scanned in the com.example.feign package.

By using @EnableFeignClients, you integrate Feign into your Spring Boot application, allowing you to easily create and use declarative HTTP clients for communication between microservices.

See the sample below to create a FeignClient in Java project.

@FeignClient(value = "student-service")
public interface StudentFeignClient {

@GetMapping("api/student/{id}")
ApiResponse<Map<String,Object>> getStudentById(@PathVariable int id);

}

URL and value

In a Feign client annotation in Spring Cloud, the value attribute is used to specify the name of the target microservice or the logical name by which the client will refer to the service. This logical name is used for service discovery and load balancing.

In this example, value = "student-service" indicates that this Feign client is associated with the microservice named "student-service." When the Feign client makes requests, Spring Cloud will use service discovery mechanisms to locate an instance of the "student-service" and perform load balancing if multiple instances are available.

The value specified in @FeignClient is usually the name of the microservice as registered in the service registry (such as Eureka) or any other identifier that helps Spring Cloud locate and communicate with the desired microservice.

In addition to the value attribute, there are other attributes that can be used in @FeignClient annotations, such as url (to specify the URL directly), configuration (to provide additional configuration classes), and others, depending on your specific needs. The choice of attributes depends on the requirements of your microservices architecture and how you want to configure the Feign client.

Here is the example

@FeignClient(url = "http://localhost:4000", value = "student-service")
public interface StudentFeignClient {
@GetMapping("api/student/{id}")
ApiResponse<Map<String,Object>> getStudentById(@PathVariable int id);

}

The path attribute in a @FeignClient annotation in Spring Cloud is used to specify a prefix that will be prepended to all request mappings defined in the associated interface. This can be useful when you want to group and organize related API endpoints under a common path.

Here’s an example:

@FeignClient(value = "student-service", path = "api/students/")
public interface StudentFeignClient {

@GetMapping("/{id}")
ApiResponse<Map<String,Object>> getStudentById(@PathVariable int id);

}

Configuration

In the context of a Feign client in Spring Cloud, the configuration attribute in the @FeignClient annotation allows you to specify a configuration class or classes that customize the behavior of the Feign client. This can be useful when you need to fine-tune aspects of the client, such as connection timeouts, request/response interceptors, error handling, etc.

Here’s an example of using the configuration attribute:

@FeignClient(value = "student-service", configuration = MyFeignClientConfiguration.class)
public interface StudentFeignClient {

@GetMapping("api/students/{id}")
ApiResponse<Map<String,Object>> getStudentById(@PathVariable int id);

}
import feign.Logger;
import org.springframework.context.annotation.Bean;

@Configuration
public class MyFeignClientConfiguration {

@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL; // Customize the Feign logging level
}
}

In this example, the feignLoggerLevel method returns a Logger.Level instance, setting the logging level of the Feign client to FULL. This is just one example, and you can provide various configurations based on your specific requirements.

By using the configuration attribute, you can keep your Feign client interfaces focused on defining the API, while the configuration class handles the customisation of the client behavior. This separation of concerns makes the code more modular and maintainable.

Conclusion

In conclusion, the @FeignClient annotation in Spring Cloud is a powerful tool for building declarative HTTP clients for microservices. It simplifies the process of communicating between microservices by generating a proxy at runtime based on an interface, allowing developers to make HTTP requests to other services with ease.

Key attributes of @FeignClient include:

1. value: Specifies the name or identifier of the target microservice for service discovery and load balancing.

2. path: Optional attribute that allows you to specify a prefix to be added to all request mappings in the associated interface, aiding in the organisation of APIs.

3. configuration: Optional attribute that enables the customization of Feign client behavior by providing a configuration class or classes.

By using @FeignClient along with appropriate attributes and configurations, developers can create clean and maintainable code for microservices communication, abstracting away the complexities of HTTP requests, service discovery, and load balancing within the Spring Cloud ecosystem.

Next Topic :

Feign Client with Api Gateway

Please reach out to me on LinkedIn for more queries.

--

--