Microservice: Open Feign Microservice Communication Advanced
Introduction#
In a microservices architecture, smooth and effective inter-service communication is essential to construct scalable and maintainable applications. Spring Cloud OpenFeign allows for smooth communication between services through the declarative HTTP client, with little boilerplate code required. With the integration of Spring Cloud Eureka for service discovery and load balancing features, the flexibility and reliability of microservices are increased with OpenFeign. This article explores how OpenFeign can improve microservice communication among Spring Boot applications, thereby profiting from efficiency, scalability, and maintainability.
Spring Cloud Open Feign#
Spring Cloud OpenFeign is an amazing tool for creating HTTP clients in any Spring-based microservice. OpenFeign allows you to call other services without the overhead of configuring the HTTP requests, error handling, or response processing.
Steps to Set Up OpenFeign in Your Spring Boot Project:#

1. Add Dependency (order-service)#
2. Create a Client Interface (Add @FeignClient) inside order-service#
@FeignClient(name = "service name")
→ Defines a Feign client for inter-service calls.@FeignClient(name = "service name", path = "base path")
→ Defines a Feign client for order-service with a base path.- Advantages:
- Avoids duplicate URL definitions
- Keeps Feign clients clean and organized
- Works with Eureka for dynamic service discovery
3. Add @EnableFeignClients
inside the main application class (order-service)#
4. Create a post mapping to create a order in OrderController (order-service)#
5. Create a business logic for post mapping to create an order in OrderService (order-service)#
6. Create OrderRequestDto And OrderRequestItemDto Class (inventory-service)#
7. Create a put mapping for reduce-stock in ProductController (inventory-service)#
8. Create a business logic for put mapping for reduce-stock in ProductService (inventory-service)#
Output:#
1. Run all services:#
After running all services (the sequence is 1. Run Eureka Server (Discovery Service) 2. Run other clients services. 3. Last run gateway service on default port 8080)
2. Wait for all registered client services inside Eureka:#
if your services are not fully registered before another service tries to communicate with them, you may encounter a "Service Unavailable" exception.
3. Hit the url http://localhost:8080/api/v1/orders/core/create-order in the Browser or Postman with the request body:#
After starting Eureka and all client services, test the API using a browser (for GET requests) or Postman (for all requests). If you are trying to fetch other services you can easily fetch it. Here, we are trying to fetch inventory service data by using api-gateway (port=8080). /api/v1
ignored because of using StripPrefix=2
. We need to comment “RedirectTo=302, <http://codingshuttle.com
>” and “AddRequestHeader=X-Custom-Header, ABCD
” from the gateway filter.

Ensure the service is registered, then hit the correct URL. If you get a "503 Service Unavailable," wait and retry.

Database:#
a. I hardcoded the value inside the product table for testing purposes.#
Right-click on the row, and after that, go to ‘Edit’ > ‘Add row’. And after that, you can put the values.

After that ‘save’ them.


b. After hitting the endpoint with the request body Product table.#
You can see the price and stock are reduced for IDs 3 and 4.

c. After hitting the endpoint with the request body items table.#

d. After hitting the endpoint with the request body orders table.#

Conclusion#
This article expounds on Spring Cloud OpenFeign, a declarative HTTP client for Spring Boot microservices, which is meant to make communication through REST APIs less boilerplate. It includes service discovery integration, load balancing, and custom configuration, as well as giving a new meaning to microservice efficiency, scalability, and maintainability.