【问题标题】:How do I add things to the /info endpoint in spring boot programmatically?如何以编程方式将内容添加到 Spring Boot 中的 /info 端点?
【发布时间】:2014-07-14 20:44:25
【问题描述】:

如何以编程方式向Spring Boot 中的/info 端点添加内容? documentation 声明通过使用HealthIndicator 接口,/health 端点是可能的。 /info 端点也有什么东西吗?

我想在此处添加操作系统名称和版本以及其他运行时信息。

【问题讨论】:

  • 虽然这不是您要寻找的答案,但操作系统信息以及大量其他运行时信息都存在于/env 映射中
  • 添加到我之前的评论中,如果您将属性放在 application.properties 中,例如 info.os = ${os.name},则表达式 被评估并正确显示在 info 端点中。跨度>
  • 我发现这个链接很有帮助:mrhaki.blogspot.ie/2016/12/spring-sweets-add-extra-build.html。没有自定义逻辑,只需几行配置。

标签: spring-boot


【解决方案1】:

在 Spring Boot 1.4 中,您可以声明 InfoContributer bean 以简化此操作:

@Component
public class ExampleInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("example",
                Collections.singletonMap("key", "value"));
    }

}

请参阅http://docs.spring.io/spring-boot/docs/1.4.0.RELEASE/reference/htmlsingle/#production-ready-application-info-custom 了解更多信息。

【讨论】:

  • 这应该是答案。接受的答案现在在 Spring 中已弃用。
  • 如何让它与健康贡献者一起工作! HealthContributor 接口为空!
  • 仍然可以使用最新的春季版本谢谢:)
【解决方案2】:

接受的答案实际上破坏了 InfoEndpoint 并且没有添加到它。

我发现添加到信息的一种方法是,在@Configuration 类中,添加一个@Autowired 方法,该方法根据info.* 约定向环境添加额外的属性。然后InfoEndpoint 将在其调用时将它们拾取。

您可以执行以下操作:

@Autowired
public void setInfoProperties(ConfigurableEnvironment env) {
    /* These properties will show up in the Spring Boot Actuator /info endpoint */
    Properties props = new Properties();

    props.put("info.timeZone", ZoneId.systemDefault().toString());

    env.getPropertySources().addFirst(new PropertiesPropertySource("extra-info-props", props));
}

【讨论】:

  • 感谢您的想法,但这对我不起作用,这些附加属性在 /env 上可见,但在 /info 上不可见。另外我注意到您将时区放在那里,我也在重置我的默认时区,但发现在我重置它们之前调用了将它们放入属性中的这段代码。
  • 您确定在它们前面加上“信息”。如此处所述? docs.spring.io/spring-boot/docs/current/reference/html/… 。此外,由于 Properties 可以将 Object 作为值,因此您可以将其设置为覆盖 toString() 的类的实例,以提供您想要的任何动态信息
  • 只有这个对我有用!无法获得接受的答案
【解决方案3】:

做你想做的事情的一种方法(如果你有完全自定义的属性需要显示)是声明一个 InfoEndpoint 类型的 bean,它将覆盖默认值。

@Bean
public InfoEndpoint infoEndpoint() {
     final LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
     map.put("test", "value"); //put whatever other values you need here
     return new InfoEndpoint(map);
}

从上面的代码可以看出,地图可以包含您需要的任何信息。

如果您要显示的数据可以由环境检索并且不是自定义的,则不需要覆盖InfoEndpoint bean,但您可以简单地将属性添加到属性文件中,前缀为info。评估操作系统名称的一个示例如下:

info.os = ${os.name}

在上面的代码中,Spring Boot 将在返回 /info 端点中的属性之前评估右手表达式。

最后一点是,/env 端点中已有大量环境信息

更新

正如@shabinjo 所指出的,在较新的 Spring Boot 版本中,没有接受映射的 InfoEndpoint 构造函数。 但是,您可以使用以下 sn-p:

@Bean
public InfoEndpoint infoEndpoint() {
     final Map<String, Object> map = new LinkedHashMap<String, Object>();
     map.put("test", "value"); //put whatever other values you need here
     return new InfoEndpoint(new MapInfoContributor(map));
}

上面的代码将完全覆盖/info 中的默认信息。 为了克服这个问题,可以添加以下 bean

@Bean
public MapInfoContributor mapInfoContributor() {
    return new MapInfoContributor(new HashMap<String, Object>() {{
        put("test", "value");
    }});
}

【讨论】:

  • 感谢这些非常有用的提示。我想 add 东西到默认 /info (因为它已经包含有用的信息,如 GIT 提交 ID、Maven 版本等)。返回我自己的 bean 可能不包括默认值?
  • @WimDeblauwe 您是对的,如果您定义自己的 bean,您将有效地排除其他信息。要了解如何重新启用它,请查看 org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration 的源代码
  • 我检查了源代码,但我不能 @Autowire InfoPropertiesConfiguration 类,因为它受到保护。 :(
  • @geo 我遇到了奇怪的 /info 行为。我在我的属性中声明了 info.os = ${os.name} 但有时 /info 和 /env 显示 os.name 的不同值,而 info one 是错误的。你知道吗?
【解决方案4】:

应该可以在 ApplicationListener 中添加自定义 PropertySource 以向环境添加自定义 info.* 属性(请参阅此答案以获取示例:How to Override Spring-boot application.properties programmatically

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 2012-07-19
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多