如何以编程方式访问 spring.application.instance_id?

How to access spring.application.instance_id programmatically?

我的 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 class ,我在其中尝试使用 @Value 注释访问此变量的值,但它给了我一个错误。这是我必须获取并打印 java class

中的值
@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.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

@Value 注释与 Spring 表达式语言一起使用

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

//...

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

//...

你错过了 metadata 块。使用:

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

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