These are chat archives for spring-cloud/spring-cloud

7th
Aug 2016
Leanid
@herasimau
Aug 07 2016 15:27
hi all
can anyone help to add mysql in spring cloud security
i have inMemoryAuthentication that is working well, but i want to get users from DB
Marcos Barbero
@marcosbarbero
Aug 07 2016 15:31
bitsofinfo
@bitsofinfo
Aug 07 2016 16:11

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 {
.....
Leanid
@herasimau
Aug 07 2016 16:12
@marcosbarbero thanks
Spencer Gibb
@spencergibb
Aug 07 2016 16:21
You can use placeholders ${my.clientName}
bitsofinfo
@bitsofinfo
Aug 07 2016 16:22
@spencergibb but I don't know those at startup time. I need to construct these at anytime during runtime and the "service names" are no pre-known
Spencer Gibb
@spencergibb
Aug 07 2016 16:28
Then you can't use the feign client annotation. You'll need to use the builder directly, see the feign documentation.
bitsofinfo
@bitsofinfo
Aug 07 2016 16:31
yeah figured something like that, was trying to piece that together by looking at the spring-cloud-netflix code on how that @FeignClient is handled but I little more challenging.
bitsofinfo
@bitsofinfo
Aug 07 2016 22:08
@spencergibb I ended up doing this, I cache the resulting bean in a wrapping class as these are created on the fly after the system boots up. Let me know if there are any glaring issues with this approach. Seems to work well so far:

        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;