Implementing a Dynamic Feign Client for Simplified Microservice Calls in Spring

This article explains how to replace numerous static Feign client interfaces with a single dynamic Feign client in Spring, showing the generic service definition, a client factory, and usage examples that streamline inter‑service HTTP calls while keeping code reusable and maintainable.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Implementing a Dynamic Feign Client for Simplified Microservice Calls in Spring

Feign makes inter‑service calls in a microservice architecture concise by eliminating the need to write low‑level Java Http code.

Usually, developers create a separate Feign client interface for each microservice and map each controller method, which can become cumbersome.

For example, static Feign clients for system and user services might look like this:

@FeignClient(name = "system")
public interface SystemClient {
    @GetMapping("/system/test1")
    JsonResult test1(String test1);
    @GetMapping("/system/test2")
    JsonResult test2(String test2);
    // ...
}

@FeignClient(name = "user")
public interface UserClient {
    @GetMapping("/user/test1")
    JsonResult test1(String test1);
    @GetMapping("/user/test2")
    JsonResult test2(String test2);
    // ...
}

To avoid this redundancy, a dynamic Feign client can be created that receives the target service name, URL, and parameters at runtime.

First, define a generic interface with common GET and POST methods:

public interface DynamicService {
    @PostMapping("{url}")
    Object executePostApi(@PathVariable("url") String url, @RequestBody Object params);

    @GetMapping("{url}")
    Object executeGetApi(@PathVariable("url") String url, @SpringQueryMap Object params);
}

The url represents the controller endpoint of the target microservice, and params are the request parameters (with @RequestBody for POST).

Next, implement a dynamic client that obtains a Feign client instance from a factory and forwards calls:

@Component
public class DynamicClient {
    @Autowired
    private DynamicFeignClientFactory<DynamicService> dynamicFeignClientFactory;

    public Object executePostApi(String feignName, String url, Object params) {
        DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName);
        return dynamicService.executePostApi(url, params);
    }

    public Object executeGetApi(String feignName, String url, Object params) {
        DynamicService dynamicService = dynamicFeignClientFactory.getFeignClient(DynamicService.class, feignName);
        return dynamicService.executeGetApi(url, params);
    }
}

In this context, feignName corresponds to the application.name of the microservice to be called (e.g., "system").

The factory creates Feign client proxies on demand:

@Component
public class DynamicFeignClientFactory<T> {
    private FeignClientBuilder feignClientBuilder;

    public DynamicFeignClientFactory(ApplicationContext appContext) {
        this.feignClientBuilder = new FeignClientBuilder(appContext);
    }

    public T getFeignClient(final Class<T> type, String serviceId) {
        return this.feignClientBuilder.forType(type, serviceId).build();
    }
}

Usage is straightforward: obtain the DynamicClient bean and call the generic method with the desired service name, URL, and parameters.

DynamicClient dynamicClient = SpringUtil.getBean(DynamicClient.class);
Object result = dynamicClient.executePostApi("system", "/system/test", new HashMap<>());
System.out.println("==========>" + JSONObject.toJSONString(result));

This approach provides a universal Feign client that can invoke any microservice endpoint without writing separate interfaces for each, greatly simplifying development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaMicroservicesspringfeignDynamicClient
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.