Spring Boot Handbook

    Microservice: Open Feign Microservice Communication

    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. Here is a deeper exploration of how it works and how you can make use of it in your projects:

    Key Features of Spring Cloud OpenFeign:#

    • Deployed Declarative HTTP Client: A developer defines interfaces to model the target services, and OpenFeign will generate HTTP calls automatically for you; it does not need to write custom code to make REST API calls.
    • Easy Service Discovery Integration: OpenFeign integrates with the service discovery of Spring Cloud, such as Eureka, thus enabling the discovery based on service names and eliminating host and port information hardcoding. This benefits all dynamic microservices environments.
    • Load Balancing: OpenFeign works well with Spring Cloud LoadBalancer or Ribbon for request distribution across multiple instances of a service.
    • Error handling: OpenFeign contains built-in error handling mechanisms with the possibility of custom error processing through the configuration.
    • Customizable: Headings, request parameters, and many more. The @RequestMapping and @GetMapping annotations are involved in the customization of the HTTP call.

    Steps to Set Up OpenFeign in Your Spring Boot Project:#

    • Add Dependency inside inventory-service
    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
    • Create a Client Interface (Add @FeignClient) inside inventory-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
    package com.codingshuttle.ecommerce.inventory_service.clients; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "order-service",path = "/orders") //name = application name, path = base path public interface OrderFeignClient { @GetMapping("/core/helloOrders") //use same path and mapping String helloOrders(); //use same method as order-service }
    • Inventory Controller Class (ProductController)
    package com.codingshuttle.ecommerce.inventory_service.controllers; @RestController @Slf4j @RequiredArgsConstructor @RequestMapping("/products") public class ProductsController { private final OrderFeignClient orderFeignClient; @GetMapping("/fetchOrder") public String fetchFromOrderService(HttpServletRequest httpServletRequest) { log.info(httpServletRequest.getHeader("X-Custom-Header")); return orderFeignClient.helloOrders(); //instead of restClient we use orderFeignClient } }
    • Add @EnableFeignClients inside the main application class (inventory-service)
    package com.codingshuttle.ecommerce.inventory_service; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients // for bean creation public class InventoryServiceApplication { public static void main(String[] args) { SpringApplication.run(InventoryServiceApplication.class, args); } }

    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 endpoint in the Browser or Postman:#

    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 .

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

    Fetching order

    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.

    Last updated on Mar 05, 2025