What OpenFeign do?
OpenFeign is a declarative HTTP client for Spring Boot. Instead of writing code to call another microservice using RestTemplate or WebClient, you simply define an interface, and OpenFeign…
OpenFeign is a declarative HTTP client for Spring Boot.
Instead of writing code to call another microservice using RestTemplate or WebClient, you simply define an interface, and OpenFeign generates the implementation automatically.
Think of it like this:
“I want to call another service.”
OpenFeign says:
“Just tell me what endpoint you want. I’ll write the networking code.”
Without OpenFeign
Suppose Order Service wants customer information.
Order Service
|
| HTTP Request
▼
Customer Service
Without Feign, you write lots of code.
RestTemplate restTemplate = new RestTemplate();
Customer customer =
restTemplate.getForObject(
"http://customer-service/api/customers/1",
Customer.class
);
Or using WebClient
WebClient.create()
.get()
.uri("http://customer-service/api/customers/1")
.retrieve()
.bodyToMono(Customer.class);
You must manually
- create request
- specify URL
- deserialize response
- handle errors
- manage load balancing
With OpenFeign
Just create an interface.
@FeignClient(name = "customer-service")
public interface CustomerClient {
@GetMapping("/api/customers/{id}")
Customer getCustomer(@PathVariable Long id);
}
Now simply call
Customer customer =
customerClient.getCustomer(1L);
Looks like a normal Java method.
Behind the scenes, Feign sends the HTTP request.
What happens internally?
Order Service
CustomerClient.getCustomer(1)
│
▼
OpenFeign
│
Creates HTTP Request
│
GET /api/customers/1
│
Customer Service
│
JSON Response
│
Feign converts JSON
│
Customer Object
Everything happens automatically.
Why is it called Declarative?
Because you declare what you want.
You don’t write networking code.
You only declare:
@FeignClient(name="customer-service")
and
@GetMapping("/customers/{id}")
Feign handles everything else.
In modern enterprise applications
Suppose you have
API Gateway
|
-------------------------------
Order Service
Payment Service
Inventory Service
Customer Service
Notification Service
Now
Order Service needs
- Customer Service
- Inventory Service
- Payment Service
Instead of writing HTTP code three times
RestTemplate...
RestTemplate...
RestTemplate...
You create
CustomerClient
InventoryClient
PaymentClient
Very clean.
How does Feign know where Customer Service is?
This is where service discovery comes in.
Earlier (with Eureka)
@FeignClient(name="customer-service")
Feign asks Eureka
Where is customer-service?
Eureka replies
10.0.1.25
10.0.1.32
10.0.1.48
Feign picks one instance.
Nowadays (Kubernetes)
There is usually no Eureka.
@FeignClient(name="customer-service")
Feign asks Kubernetes DNS
customer-service.default.svc.cluster.local
Kubernetes returns
Pod 1
Pod 2
Pod 3
Load balancing is handled by Kubernetes.
Does Feign perform load balancing?
Yes.
Earlier
Feign
↓
Ribbon
↓
Eureka
Ribbon performed client-side load balancing.
Nowadays
Ribbon is removed.
Spring Cloud uses Spring Cloud LoadBalancer when needed, and in Kubernetes the Service object typically distributes traffic to healthy pods, so Feign works naturally with Kubernetes networking.
Example
Suppose
Customer Service has
Pod 1
10.0.0.5
Pod 2
10.0.0.6
Pod 3
10.0.0.7
Feign calls
customer-service
Kubernetes Service chooses
Pod 2
Next request
Pod 3
Next request
Pod 1
You don’t need to code this.
Typical enterprise flow
User
│
API Gateway
│
Order Service
│
Feign Client
│
Kubernetes Service
│
Customer Pods
│
JSON Response
│
Feign
│
Java Object
│
Order Service
What extra features does Feign provide?
Besides making HTTP calls, OpenFeign integrates well with Spring:
- Automatic JSON serialization/deserialization
- Path variables and request parameters
- Request headers
- Request bodies (POST/PUT)
- Error decoding
- Request interceptors (e.g., add JWT tokens to every request)
- Logging
- Timeout configuration
- Integration with resilience libraries such as circuit breakers and retries
Example:
@FeignClient(name = "payment-service")
public interface PaymentClient {
@PostMapping("/payments")
PaymentResponse makePayment(
@RequestBody PaymentRequest request);
}
Just call:
paymentClient.makePayment(request);
No HTTP boilerplate required.
Real-world analogy
Imagine you want to order food from another department in a company.
Without Feign
You have to:
- Find the phone number
- Dial it
- Explain your request
- Listen to the response
- Write everything down
With Feign
You simply tell your assistant:
“Get lunch from the cafeteria.”
Your assistant handles all the communication and brings back the result.
OpenFeign acts like that assistant—it hides the details of making HTTP requests so your code can focus on business logic.
In one sentence
OpenFeign lets one microservice call another by writing a simple Java interface instead of manually creating HTTP requests, making inter-service communication much cleaner and easier to maintain.