Can anyone recommend a good starting point to dynamically instantiate a new FeignClient that behaves the same way with all the hystrix/ribbon/eureka backed functionality as those statically declared like this?
For me the only thing that will vary at runtime is the "name" of the service (i.e. registered in eureka) by which the endpoints my client will talk to, hence I need to be able to construct these at runtime, and would like to be able to do so in the proper way as they are at startup w/ the declaration below. (i.e imagine name="thing-service", changing to "thing-serviceA", "think-serviceB" etc) I just need to be able to declare and construct these clients at runtime.
@FeignClient(name="thing-service", fallback = ThingServiceClientFallback.class)
public interface ThingClient {
.....
${my.clientName}
BeanDefinitionBuilder definition = BeanDefinitionBuilder
.genericBeanDefinition(FeignClientFactoryBean.class);
String className = clientClass.getName();
definition.addPropertyValue("url", "");
definition.addPropertyValue("path", "");
definition.addPropertyValue("name", serviceName);
definition.addPropertyValue("type", className);
definition.addPropertyValue("decode404", false);
definition.addPropertyValue("fallback", fallbackClass);
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
String alias = serviceName + "FeignClient";
AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
beanDefinition.setPrimary(true);
BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[] { alias });
GenericApplicationContext tmpContext = new GenericApplicationContext(appContext);
BeanDefinitionReaderUtils.registerBeanDefinition(holder, tmpContext);
tmpContext.refresh();
Object client = tmpContext.getBean(alias);
return client;