Implementing Dynamic Feign Clients for Flexible Microservice Calls in Spring Cloud
This article explains how to create a dynamic Feign client in Spring Cloud by defining a generic service interface, a dynamic client wrapper, and a factory that builds Feign instances at runtime, allowing flexible GET and POST calls to any microservice without writing repetitive client interfaces.
Feign simplifies inter‑service HTTP calls in microservice architectures, but defining a separate @FeignClient interface for each service can become cumbersome.
To make Feign usage more flexible, the article introduces a dynamic Feign approach: a generic service interface with wildcard @GetMapping and @PostMapping methods, a dynamic client that obtains a Feign proxy by service name, and a factory that builds the proxy at runtime.
First, the traditional static Feign clients are shown:
@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);
// ...
}Next, a generic interface DynamicService is defined with path variables for the target URL and parameters:
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 DynamicClient class uses a DynamicFeignClientFactory to retrieve a Feign proxy for a given service name and then delegates the generic 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);
}
}The factory builds Feign clients dynamically using Spring's FeignClientBuilder:
@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 methods with the target 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 dynamic approach eliminates the need to write a separate Feign interface for each microservice, making the codebase more maintainable and reusable.
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
