Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 6 of 10 · Microservices with Spring Cloud
Interview question

What is Feign Client and how does it simplify inter-service communication? Feign Client क्या है और यह इंटर-सर्विस कम्युनिकेशन को कैसे आसान बनाता है?

Answer

OpenFeign is a declarative HTTP client: instead of manually building HTTP requests with RestTemplate or WebClient, you define a Java interface annotated with the target service's endpoints, and Spring generates the implementation at runtime — calling another microservice looks like calling a local method.

Feign integrates with Eureka for service discovery (using the service's registered name instead of a hardcoded URL) and with Resilience4j or Hystrix for automatic circuit breaking, and supports custom encoders, decoders, and interceptors for things like propagating an auth header on every outgoing call.

@FeignClient(name = "inventory-service")
public interface InventoryClient {
    @GetMapping("/api/inventory/{productId}")
    Inventory getStock(@PathVariable String productId);
}

@Service
public class OrderService {
    private final InventoryClient inventoryClient;
}

OpenFeign एक डिक्लेरेटिव HTTP क्लाइंट है: RestTemplate या WebClient से मैन्युअली HTTP रिक्वेस्ट बनाने के बजाय, आप लक्षित सर्विस के एंडपॉइंट्स से एनोटेट एक Java इंटरफेस डिफाइन करते हैं, और स्प्रिंग रनटाइम पर इम्प्लीमेंटेशन जनरेट करता है।

Feign, सर्विस डिस्कवरी के लिए Eureka के साथ और स्वचालित सर्किट ब्रेकिंग के लिए Resilience4j या Hystrix के साथ इंटीग्रेट होता है, और हर आउटगोइंग कॉल पर ऑथ हेडर प्रोपेगेट करने जैसी चीज़ों के लिए कस्टम इंटरसेप्टर्स सपोर्ट करता है।

@FeignClient(name = "inventory-service")
public interface InventoryClient {
    @GetMapping("/api/inventory/{productId}")
    Inventory getStock(@PathVariable String productId);
}

Was this answer clear?