【问题标题】:How to access spring.application.instance_id programmatically?如何以编程方式访问 spring.application.instance_id?
【发布时间】:2015-09-03 03:28:24
【问题描述】:

我的 Spring Boot/Cloud 应用程序的“applicationname.yml”文件中有以下内容。如何在我的 java 代码中获取 spring.application.instance_id 的值?这个“applicationname.yml”文件托管在“Spring Cloud Config Server”中。

eureka:
  password: password
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${vcap.services.${PREFIX:}eureka.credentials.uri:http://user:password@localhost:8761}/eureka/
  instance:
    preferIpAddress: true
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}

我有一个 java 类,我试图使用 @Value 注释访问这个变量的值,它给了我一个错误。这是我必须获取并打印 java 类中的值

@Value("${eureka.instance.instanceId}")
private String EinstanceId;

@Value("${spring.application.instance_id}")
private String SinstanceId;

错误信息

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.citigroup.ccp.psg.error.PSGErrorFilter.instanceId; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'eureka.instance.instanceId' in string value "${eureka.instance.instanceId}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 49 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'eureka.instance.instanceId' in string value "${eureka.instance.instanceId}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:955)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 51 more

【问题讨论】:

  • 您是否收到错误消息或意外行为或其他什么?很明显,你所拥有的并不是你想要的,但了解它正在在做什么真的很有帮助。

标签: java spring spring-boot spring-cloud netflix-eureka


【解决方案1】:

你错过了metadata 块。使用:

@Value("${eureka.instance.metadataMap.instanceId}") String instanceId;

据我所知 spring.application 命名空间中没有 instance_id 属性。

【讨论】:

  • 宾果游戏!!!有效。我在哪里可以看到,这个 metadataMap 下还有哪些其他可用的属性?
  • metaDataMap 可以说是属性的哈希图。你可以把任何你想要的东西放在那里,它对使用你的其他服务是可见的。 instanceId 在这里很特殊——它必须是唯一的才能在 eureka 中找到这个服务实例。很难用几句话来解释 :) 请看一下文档:cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html
  • + 如果您需要查找 .yml 映射到的某个类,您可以在 spring 云源中搜索“@ConfigurationProperties("eureka.instance")”。在这种情况下,它的 EurekaInstanceConfigBean.java
  • 除此之外,spring.application.instance_id 在 Cloud Foundry 上运行时自动设置。否则,您应该使用其他值,这就是 ${spring.application.instance_id:${server.port}} - 如果设置则使用该属性,否则回退到 server.port
【解决方案2】:

在 Spring 表达式语言中使用 @Value 注释

import org.springframework.beans.factory.annotation.Value;

//...

    @Value("${spring.application.instance_id}")
    private String instanceId;

//...

【讨论】:

  • 收到一条错误消息,提示无法解析字符串值“${spring.application.instance_id}”中的占位符“eureka.instance.instanceId”,可能是什么原因?或者修复这个错误?
  • 我觉得应该是eureka.instance.metadataMap.instanceId
  • 对此还有其他想法吗?我还得到无法解析值“$ {spring.application.instance_id}”中的占位符“spring.application.instance_id”。我把它放在我的控制器里
猜你喜欢
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-11
相关资源
最近更新 更多