Chapter 10.4☕ 14 min read

Feign Client: Inter-Service Communication

If <code>user-service</code> needs to call <code>payment-service</code>, you could use <code>RestTemplate</code> or <code>WebClient</code>. But writing HTTP client code, parsing JSON, and handling errors is messy. <strong>OpenFeign</strong> lets you call APIs as if they were local Java methods.

01The Concept: Declarative REST Client

The Personal Assistant Analogy:

Instead of you manually dialing the phone number, waiting for the dial tone, and typing the extension (RestTemplate), you just tell your Personal Assistant: "Call the Payment Department and ask for the bill." The assistant handles the phone call, the connection, and the response.

OpenFeign is that Personal Assistant. You just create a Java interface with @FeignClient. Spring Boot generates the actual HTTP call under the hood at runtime.

02Technical Explanation
  1. @EnableFeignClients: Placed on the main class to turn on the Feign engine.
  2. @FeignClient(name = "PAYMENT-SERVICE"): Tells Spring to create a proxy implementation of this interface. The name must match the Eureka registration name.
  3. No Implementation: You don't write the class. Spring writes it for you.
03Full Working Code: Calling another Microservice

1. pom.xml

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

2. Main Class (UserServiceApplication.java)

package com.devinhyderabad;

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

@SpringBootApplication
@EnableFeignClients // 1. Turn on Feign
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}

3. The Feign Client Interface (PaymentClient.java)

package com.devinhyderabad;

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

// 2. Name must match the Eureka registration name of the target service
@FeignClient(name = "PAYMENT-SERVICE")
public interface PaymentClient {

// 3. This matches the REST endpoint in the Payment microservice
@GetMapping("/api/payments/user/{userId}")
String getPaymentStatus(@PathVariable Long userId);
}

4. Usage in UserController

@RestController
class UserController {
@Autowired
private PaymentClient paymentClient; // Spring injects the magic proxy

@GetMapping("/users/{id}/status")
public String getUserStatus(@PathVariable Long id) {
// It looks like a local method call, but it makes an HTTP request!
return paymentClient.getPaymentStatus(id);
}
}
04Why It Matters

Interview Question: "Why use OpenFeign instead of RestTemplate for microservice communication?"

Answer: RestTemplate requires writing a lot of boilerplate code for URL construction, parameter mapping, and response parsing. Feign is declarative — you just write an interface. Feign also integrates seamlessly with Eureka (for discovery) and Resilience4j (for circuit breaking) without changing your business logic.

05Interview Note

Enterprise Note: Feign automatically integrates with Spring Cloud LoadBalancer. If PAYMENT-SERVICE has 3 instances running, Feign will automatically round-robin between them.

Key Takeaways

  • ✅ OpenFeign is a declarative HTTP client — write interfaces, not boilerplate
  • ✅ @FeignClient(name = "SERVICE-NAME") creates a proxy that makes HTTP calls
  • ✅ The name must match the Eureka registration name for discovery to work
  • ✅ Feign integrates with Spring Cloud LoadBalancer for automatic round-robin across instances