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

4

我在我的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

1
你是否收到了错误信息或意外的行为?显然你拥有的东西并没有做你想要的事情,但知道它正在做什么会帮助你解决问题。 - Martin
2个回答

4

您缺少元数据块。请使用:

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

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

太棒了!!!它起作用了。我在哪里可以看到此metadataMap下的所有其他属性? - yathirigan
metaDataMap是属性哈希映射表。您可以在其中放置任何内容,并且其他使用您的服务的服务将能够看到它。instanceId在这里很特殊 - 它需要是唯一的,以便能够在eureka中找到此服务实例。很难用几句话解释 :) 请查看文档:http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html - hi_my_name_is
如果您需要查找某个类与 .yml 文件映射的情况,可以在 Spring Cloud 源代码中搜索 "@ConfigurationProperties("eureka.instance")"。在这种情况下,它是 EurekaInstanceConfigBean.java。 - hi_my_name_is
除非在Cloud Foundry上运行,否则spring.application.instance_id仅会自动设置。否则,您应该使用其他值,${spring.application.instance_id:$ {server.port}}就是这样 - 如果已设置属性,请使用该属性,否则回退到server.port - paulchapman

2

使用Spring表达式语言与@Value注解

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

//...

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

//...

我收到一个错误信息,其中说“在字符串值中无法解析占位符'eureka.instance.instanceId' ${spring.application.instance_id}”。这可能是什么原因?或者如何修复此错误? - yathirigan
我认为应该是 eureka.instance.metadataMap.instanceId - paulchapman
还有其他想法吗?我也遇到了“在值中无法解析占位符'spring.application.instance_id'${spring.application.instance_id}”。我将其放在了我的控制器中。 - chrips

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接