Comparing Two Feign Usage Patterns for Elegant Producer‑Consumer Communication in Microservices

The article examines two ways to define Feign interfaces in a microservice architecture—having the service producer define the Feign client (SPI) versus letting the service consumer define it (API)—and compares their constraints, flexibility, dependency management, and impact on development workflow.

Architect's Journey
Architect's Journey
Architect's Journey
Comparing Two Feign Usage Patterns for Elegant Producer‑Consumer Communication in Microservices
In microservices, how should a Feign interface be defined so that producers and consumers communicate gracefully?

What is Feign?

Feign is a pseudo‑RPC component that proxies HTTP client frameworks (e.g., HttpClient, OkHttp) and hides low‑level details. It lets developers declare HTTP client requests through annotated Java interfaces.

What does Feign do?

Parse and load method parameters.

Generate a dynamic proxy for the specified @FeignClient.

Parse method metadata defined in the Feign interface.

Assemble a Request object and dispatch the HTTP call.

Two ways to use Feign

Example scenario: an order service calls a user service.

Approach 1 – Producer defines Feign (SPI‑oriented)

Define the interface in an api module and annotate it with @FeignClient, e.g., user-api/.../user/feign/IUserFeignClient.java .

Implement the interface in the service module and annotate the class with @RestController, e.g., user-service/.../user/feign/UserFeignClient.java .

Package the api module as a JAR and deploy it to a Maven repository.

Consumer adds the JAR as a Maven dependency.

Inject the bean ( IUserFeignClient) and invoke methods such as userFeignClient.login().

Approach 2 – Consumer defines Feign (API‑oriented)

Define the interface in the consumer’s service module and annotate it with @FeignClient, e.g., order-service/.../user/service/UserService.java .

The producer implements the corresponding REST controller in its service module, e.g., user-service/.../user/controller/UserController.java , without providing a concrete implementation of the consumer’s interface.

Inject the bean ( UserService) and invoke methods such as userService.login().

Comparison of the two approaches

Producer‑defined Feign (SPI‑oriented)

Strong constraint: Because the consumer imports the JAR, mismatched method signatures are caught at compile time. For example, if the producer changes a parameter, the consumer sees a compilation error after updating the dependency.

Language‑specific: The JAR can only be used by Java projects.

Avoid duplicate definitions: Consumers reuse the same interface from the JAR, eliminating repeated definitions of request/response types.

Consumer‑defined Feign (API‑oriented)

Weak constraint: The contract relies on developer agreement rather than compile‑time checks; the underlying HttpClient implementation is used directly.

Language‑agnostic: No JAR dependency means the consumer can be written in any language.

Simplified development flow: No need to package and publish a JAR; developers focus on business code, avoiding frequent version‑dependency updates.

Flexible trimming: Consumers can select only a subset of producer APIs and can trim parameters and return values, improving isolation.

Small impact scope: The producer only needs to implement its own functionality; exposing a Feign interface does not force changes to its service layer, although converting VO to DTO may require additional code.

Preferred usage scenario

Considering constraints, isolation, flexibility, and impact scope, the author prefers the second approach—defining Feign interfaces in the consumer’s service layer—without treating them as ordinary Service classes.

backendJavamicroservicesfeignapi-designService Architecture
Architect's Journey
Written by

Architect's Journey

E‑commerce, SaaS, AI architect; DDD enthusiast; SKILL enthusiast

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.