Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Integrating and Configuring OpenFeign in Spring Cloud Microservices

Tech May 15 1

Manually concatenating URL strings in RestTemplate becomes cumbersome and error-prone when dealing with multiple request parameters. OpenFeign solves this by providing a declarative, template-based HTTP client that allows developers to invoke remote services as if they were local method calls.

Spring Cloud enhances OpenFeign to support Spring MVC annotations. Additionally, since it integrates with Ribbon by default, it provides client-side load balancing out of the box.

To enable Feign functionality, ensure the @EnableFeignClients annotation is present on the main application class to trigger interface scanning.

Defining the Service Provider

First, add the necessary dependency to the provider module:

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

The provider exposes REST endpoints that the client will consume:

@RestController
@RequestMapping("/api/v1")
public class EmployeeResource {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/employee/{empId}")
    public Employee fetchEmployee(@PathVariable Integer empId) {
        return employeeService.findEmployeeById(empId);
    }
}

Creating the Feign Client Interface

Define a Java interface annotated with @FeignClient. This acts as a proxy for the remote service.

@FeignClient(name = "employee-service")
@RequestMapping("/api/v1")
public interface EmployeeClient {

    @GetMapping("/employee/{empId}")
    Employee getEmployee(@PathVariable("empId") Integer id);

    @DeleteMapping("/employee/remove")
    ResponseMessage removeEmployee(@RequestParam("empId") Integer id);

    @PostMapping("/employee/save")
    Employee saveEmployee(@RequestBody Employee employee);
}
  • name: The service ID registered in the discovery server (e.g., Nacos or Eureka).
  • @PathVariable: Used for REST-style URL parameters.
  • @RequestParam: Used for query parameters (e.g., ?empId=1).
  • @RequestBody: Used to send objects serialized as JSON.

Implementing the Consumer

Inject the Feign interface into the consumer controller to execute the remote call:

@RestController
@RequestMapping("/client")
public class FrontendController {

    @Autowired
    private EmployeeClient employeeClient;

    @GetMapping("/details/{id}")
    public Employee getDetails(@PathVariable Integer id) {
        return employeeClient.getEmployee(id);
    }
}

Consumer configuration for service discovery:

server:
  port: 8080

spring:
  application:
    name: feign-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

How Feign Works

  1. Registration: @EnableFeignClients triggers FeignClientsRegistrar, which scans for @FeignClient interfaces and registers proxy beans in the Spring context.
  2. Template Creation: When a method is invoked, the proxy (specifically SynchronousMethodHandler) creates a RequestTemplate containing the URL and parameters.
  3. Execution: The template is converted into a Request, and an HTTP client (default is HttpURLConnection, but can be configured to use Apache HttpClient or OkHttp) executes the call.

Configuration and Optimization

Logging

To debug requests, configure the logging level in application.yml. Set the specific Feign client package to DEBUG level.

feign:
  client:
    config:
      employee-service:
        loggerLevel: FULL

spring:
  main:
    allow-bean-definition-overriding: true

logging:
  level:
    com.example.clients: DEBUG

Timeout Settings

Since Feign uses Ribbon for load balancing, timeouts are configured via Ribbon properties. Note that specific logging configurations might mask timeout exceptions in logs.

feign:
  client:
    config:
      employee-service:
        ConnectTimeout: 5000
        ReadTimeout: 5000

Connection Pooling

The default HttpURLConnection does not use connection pooling. To improve performance and avoid the overhead of TCP handshakes for every request, switch to Apache HttpClient.

Add the dependency:

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
</dependency>

Spring Cloud will automatically detect the classpath dependency and use the pooling mechanism.

GZIP Compression

Reducing payload size significantly improves network performance. Enable GZIP compression in the consumer configuration:

feign:
  compression:
    request:
      enabled: true
      mime-types: text/xml,application/xml,application/json
      min-request-size: 2048
    response:
      enabled: true
Tags: Spring Cloud

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.