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.
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.
- @EnableFeignClients: Placed on the main class to turn on the Feign engine.
- @FeignClient(name = "PAYMENT-SERVICE"): Tells Spring to create a proxy implementation of this interface. The
namemust match the Eureka registration name. - No Implementation: You don't write the class. Spring writes it for you.
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);
}
}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.
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
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login