根据环境定义不同的Feign客户端实现 [英] Define different Feign client implementations based on environment

查看:779
本文介绍了根据环境定义不同的Feign客户端实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring启动应用程序,该应用程序使用Feign通过Eureka调用外部Web服务.我希望能够使用Feign接口的模拟实现来运行该应用程序,因此我可以在本地运行该应用程序,而不必运行Eureka或外部Web服务.我曾想过要定义一个运行配置,使我能够执行此操作,但是正在努力使其正常运行.问题是无论我尝试什么,Spring的魔术"都在为Feign接口定义一个bean.

I have a Spring boot application which uses Feign to call an external web service via Eureka. I'd like to be able to run the application using a mocked out implementation of the Feign interface, so I can run the application locally without necessarily having Eureka or the external web service running. I had imagined defining a run configuration that allowed me to do this, but am struggling to get this working. The issue is that the Spring "magic" is defining a bean for the Feign interface no matter what I try.

伪装界面

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

服务

public class MyService {
    private FooResource fooResource;

    ...

    public void getFoo() {
        String response = this.fooResource.getResponse();
        ...
    }
}

我尝试添加一个配置类,如果Spring概要文件是本地的",则有条件地注册了一个bean,但是当我使用该Spring概要文件运行应用程序时却从未调用过:

I tried adding a configuration class that conditionally registered a bean if the Spring profile was "local", but that was never called when I ran the application with that Spring profile:

@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";
            }
        };
    }
}

在我的服务运行时,MyService中的FooResource成员变量的类型为

At the point my service runs, the FooResource member variable in MyService is of type

HardCodedTarget(type = FoorResource,url = http://foo-service )

根据IntelliJ.这是由Spring Cloud Netflix框架自动生成的类型,因此尝试与远程服务进行实际通信.

according to IntelliJ. This is the type that is automatically generated by the Spring Cloud Netflix framework, and so tries to actually communicate with the remote service.

有没有一种方法可以根据配置设置有条件地覆盖Feign接口的实现?

Is there a way I can conditionally override the implementation of the Feign interface depending on a configuration setting?

推荐答案

Having posted the same question on the Spring Cloud Netflix github repository, a useful answer was to use the Spring @Profile annotation.

我创建了一个未用@EnabledFeignClients注释的替代入口点类,并创建了一个新的配置类,为我的Feign接口定义了实现.现在,这使我可以在本地运行我的应用程序,而无需运行Eureka或任何依赖服务.

I created an alternative entry point class that was not annotated with @EnabledFeignClients, and created a new configuration class that defined implementations for my Feign interfaces. This now allows me to run my application locally without the need to have Eureka running, or any dependent services.

这篇关于根据环境定义不同的Feign客户端实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆