Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override Feign client implementation based on setting #1947

Closed
gala-dan opened this issue May 15, 2017 · 5 comments
Closed

Override Feign client implementation based on setting #1947

gala-dan opened this issue May 15, 2017 · 5 comments
Labels

Comments

@gala-dan
Copy link

Is it possible to provide my own implementation of a @FeignClient annotated interface based on a configuration setting? I'd like to be able to define a "local" Spring profile, for example, that will use a mocked out interface that will not try to connect to any external endpoints, allowing me to run my Spring boot application without any external dependencies being up. Additional profiles would allow the clients to be instantiated and injected as they currently are.

I have tried using the @ConditionalOnProperty annotation to serve up a bean for the interface as follows, but that doesn't work:

@FeignClient(name = "http://foo-service")
public interface FooResource {
    @RequestMapping(value = "/doSomething", method = GET)
    String getResponse();
}

...

@Configuration
public class AppConfig {
    @Bean
    @ConditionalOnProperty(prefix = "spring.profile", name = "active", havingValue="local")
    public FooResource fooResource() {
        return new FooResource() {
            @Override
            public String getResponse() {
                return "testing";
            }
        };
    }
}

When the application launches the bean is ignored, and my FooResource instance still tries to contact the remote service.

How can I achieve my aim?

@ryanjbaxter
Copy link
Contributor

This might be something Spring Cloud Contract can help with. Although generally the contracts are only used in tests as far as I know. @marcingrzejszczak thoughts?

@spencergibb
Copy link
Member

Why not use the @Profile annotation? Put @EnableFeignClients in a @Profile("!local").

@marcingrzejszczak
Copy link
Contributor

Spring Cloud Contract mocks out the service discovery in such a way that your call will get redirected to a fake HTTP server stub (e.g. WireMock)

@gala-dan
Copy link
Author

Thanks @spencergibb - you're suggestion worked out well. Appreciate the quick response.

@mattmadhavan
Copy link

Hello @spencergibb and @gala-dan ,
Helped me too!

Thanks
Matt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants