How OpenFeign Uses Dynamic Proxies to Simplify Remote Calls
OpenFeign, originally Netflix Feign, provides a declarative way to perform remote service calls by generating dynamic proxy classes that translate interface methods into HTTP requests, and this article explains its origins, basic usage, annotation processing, and the detailed steps of its dynamic proxy creation.
OpenFeign originated from the Netflix Feign project, which was part of Netflix OSS and later contributed to the open‑source community as the Spring Cloud OpenFeign component.
It offers a declarative remote‑call interface that greatly simplifies programming; a simple Hello World example shows that a single line can invoke a remote service:
String response = helloWorldService.hello("Vincent Y.");The remote call is defined by a @FeignClient interface annotated with Spring MVC mapping annotations:
@FeignClient(value = "hello-world-serv")
public interface HelloWorldService {
@PostMapping("/sayHello")
String hello(String guestName);
}OpenFeign uses dynamic proxy technology to encapsulate the remote‑call process. During startup it scans for interfaces annotated with @FeignClient , generates a proxy class (FeignProxyService) for each, registers the proxy in the Spring context, and delegates the method invocation to a remote HTTP request.
OpenFeign Dynamic Proxy Process
In the project initialization phase OpenFeign creates a proxy class that handles all remote calls made through the interface. The following flowchart illustrates the steps (steps 1‑3 occur at startup, step 4 occurs at runtime):
Key steps are:
Project loading : The EnableFeignClients annotation acts as a switch, importing FeignClientsRegistrar to start the OpenFeign loading process.
Scanning : FeignClientsRegistrar scans the specified packages for interfaces annotated with @FeignClient and creates FeignClientFactoryBean objects to parse them.
Parsing @FeignClient : FeignClientFactoryBean extracts service name, path, and other configuration, then triggers dynamic‑proxy construction via ReflectiveFeign .
Building the dynamic proxy : ReflectiveFeign parses method‑level annotations, builds MethodHandler metadata, and creates a Java dynamic proxy that implements InvocationHandler , binding it to the FeignClient interface.
The annotation parsing is performed by the Contract implementation, typically SpringMvcContract . Below is a simplified excerpt showing how RequestMapping on a method is processed:
// parse RequestMapping on method
protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodAnnotation, Method method) {
// omitted code
if (!RequestMapping.class.isInstance(methodAnnotation)
&& !methodAnnotation.annotationType().isAnnotationPresent(RequestMapping.class)) {
return;
}
RequestMapping methodMapping = findMergedAnnotation(method, RequestMapping.class);
RequestMethod[] methods = methodMapping.method();
if (methods.length == 0) {
methods = new RequestMethod[]{RequestMethod.GET};
}
data.template().method(Request.HttpMethod.valueOf(methods[0].name()));
// parse path
if (methodMapping.value().length > 0) {
String pathValue = emptyToNull(methodMapping.value()[0]);
if (pathValue != null) {
if (!pathValue.startsWith("/") && !data.template().path().endsWith("/")) {
pathValue = "/" + pathValue;
}
data.template().uri(pathValue, true);
}
}
// parse produces, consumes, headers
parseProduces(data, method, methodMapping);
parseConsumes(data, method, methodMapping);
parseHeaders(data, method, methodMapping);
data.indexToExpander(new LinkedHashMap<>());
}Even shortcuts like @GetMapping work because they are meta‑annotated with @RequestMapping :
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {
// ...
}In summary, OpenFeign solves remote service invocation by generating a dynamic proxy that translates interface method calls into HTTP requests, handling annotation parsing, metadata creation, and proxy binding, thereby providing a concise, type‑safe way to call microservices.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
