【问题标题】:Spring Cloud @FeignClient service name from properties file属性文件中的 Spring Cloud @FeignClient 服务名称
【发布时间】:2016-02-19 03:28:51
【问题描述】:

我想通过属性文件在@FeignClient 中设置服务名称,如下所示:

@FeignClient("${service.users}")

并在application.yml 中设置名称,例如:

service.users: user-service

user-service 是服务在 Eureka 中注册的名称。

我试过了,还是不行。它抱怨无效的名称。

这样的事情可以做吗?

【问题讨论】:

标签: spring-cloud netflix-feign


【解决方案1】:

我尝试使用类似的配置:

@FeignClient(name = "${spring.application.name:optional.application.name}")

应用程序.yml, bootstrap.yml:

spring:
  application:
    name: my-test-application

启动后查看日志

2016-05-24 16:11:00 [hystrix-my-test-application-1]                INFO  o.s.c.a.AnnotationConfigApplicationContext.prepareRefresh...

我也在zookeeper active service中找到了

>ls /service/my-test-application 
[8668663c-cce1-4181-94de-4ccaacefa7e3]

在调试模式客户端 bean 中检查 - 它已创建

HardCodedTarget(type=EventBusClient, name=fnma-cp-test, url=http://my-test-application)

所以这个配置应该可以工作。 我的建议是:

  1. 在运行时使用硬编码名称检查您的客户端 bean(您需要确保创建)
  2. 检查配置文件的可见性范围(配置文件中的变量可能超出范围)
  3. 检查您的 application.yml - 我不确定,spring 社区默认保留变量名称“service.users”(可能需要添加特殊依赖项)。或者如果你不知道依赖但需要使用一个,

请创建下一个文件结构:

 application.yml
 META-INF
 |-additional-spring-configuration-metadata.json

additional-spring-configuration-metadata.json 应该有这样的东西

{
  "properties": [
    {
      "name": "service.users",
      "type": "java.lang.String",
      "description": "Description for service.users.",
      "defaultValue": "Some_Value"
    }
  ]
}

无论如何,如果 additional-spring-configuration-metadata.json 有任何问题,您可以在这里找到解释:enter link description here

【讨论】:

    【解决方案2】:

    可以这样完成。

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

    fdmobile.ribbon.listOfServers : value =>> 这将是 application.properties 中的一个属性。

    【讨论】:

    • 这不是解决需要从配置文件中读取 name 属性的问题。无论如何,该问题已在以下commit 中得到解决。不过不知道什么时候发布。
    【解决方案3】:

    这是一个开放的issue。欢迎请求请求:-)

    【讨论】:

    • 我会试一试的。谢谢。
    • 已添加到此commit
    猜你喜欢
    • 2020-05-16
    • 2015-06-18
    • 2015-01-03
    • 2020-11-25
    • 2018-08-10
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多