我可以使用 properties/yml 文件配置 @FeignClient url 吗?

Can I configure a @FeignClient url using a properties/yml file?

我的目标是创建一个包含不同步骤的策略,以使用 eureka、ribbon、hystrix 从 2 个组件之间的点对点通信转变为 "full blown netflix" 通信方式。在每次迭代中,我都想添加更多内容,同时尝试限制对实际代码的更改量。 Feign 是我首选的客户端框架来实现这一点。第一步是创建一个 FeignClient 来与服务器通信:

@FeignClient(url = "http://localhost:9000")
interface Client {
    @RequestMapping(method = RequestMethod.GET, value = "/author/{author}/addedValue/{addedValue}")
    Result addToTotal(@RequestParam(value="author") String author, @RequestParam(value="addedValue") long addedValue);
}

这有效,但我不希望 URL 在注释中被硬编码。我想要这个:@FeignClient() 并具有如下属性构造:client.url: http://localhost:9000

到目前为止,我找不到任何关于如何配置它的线索,而且我在 spring-cloud 源中找不到解决方案。

能不能做,如果能;怎么样?

可以用 "serviceId" 而不是 "url" 来完成。例如

@FeignClient("foo")
interface Client { ... }

foo.ribbon.listOfServers: localhost:9000

例如有关文档,请参阅 http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-ribbon-without-eureka

可以这样做:

@FeignClient(name="fd-mobileapi-service",url="${fdmobile.ribbon.listOfServers}")

其中 fdmobile.ribbon.listOfServers : valueapplication.properties 中的 属性。

我已经测试过了,它可以正常工作。

我得到了一种以非常简单的方式传递环境变量的方法接口FeignClient,

    @FeignClient(url = "https://"+"${url}")
    interface Client {
    
  @RequestMapping(method = RequestMethod.GET, value = "/author/{author}/addedValue/{addedValue}")
    Result addToTotal(@RequestParam(value="author") String author, @RequestParam(value="addedValue") long addedValue);

属性

#URL
url.client=${URL}

.env

URL=https:localhost:9000