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

Aug 2016
Aug 07 2016 15:27 UTC
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
Aug 07 2016 15:31 UTC
Aug 07 2016 16:11 UTC

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 {
Aug 07 2016 16:12 UTC
@marcosbarbero thanks
Spencer Gibb
Aug 07 2016 16:21 UTC
You can use placeholders ${my.clientName}
Aug 07 2016 16:22 UTC
@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
Aug 07 2016 16:28 UTC
Then you can't use the feign client annotation. You'll need to use the builder directly, see the feign documentation.
Aug 07 2016 16:31 UTC
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.
Aug 07 2016 22:08 UTC
@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

        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);

        String alias = serviceName + "FeignClient";
        AbstractBeanDefinition beanDefinition = definition.getBeanDefinition();
        BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[] { alias });

        GenericApplicationContext tmpContext = new GenericApplicationContext(appContext);
        BeanDefinitionReaderUtils.registerBeanDefinition(holder, tmpContext);

        Object client = tmpContext.getBean(alias);

        return client;