Spring Boot Actuator 有 Java API 吗? [英] Does Spring Boot Actuator have a Java API?

查看:37
本文介绍了Spring Boot Actuator 有 Java API 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们自定义 Spring Boot Actuator Info 端点以包含在 Jenkins 构建期间生成的应用程序版本号.我们正在使用 gradle 来做到这一点:

We customize the Spring Boot Actuator Info endpoint to include the application version number generated during our Jenkins build. We're using gradle to do this:

if (project.hasProperty('BUILD_NUMBER')) {
    version = "${BUILD_NUMBER}"
} else {
    version = "0.0.1-SNAPSHOT"
}

这对于将版本添加到/info 端点非常有用,但我想在应用程序启动时访问它并将其打印到应用程序日志中.

That works great for adding the version to the /info endpoint, but I'd like to access it when the application starts and print it to the application log.

我希望这些值在某些属性值中(类似于 spring.profiles.active)或通过 Java API 公开.这样,我可以做这样的事情:

I'm hoping the values are exposed in some property value (similar to spring.profiles.active) or through a Java API. That way, I could do something like this:

    public class MyApplication{

    public static void main(String[] args) throws Exception {
        SpringApplication.run(MyApplication.class, args);

        ConfigurableEnvironment environment = applicationContext.getEnvironment();

System.out.println(environment.getProperty("spring.fancy.path.to.info.version"));
    }
}

查看文档,我没有找到在代码中轻松访问这些值的方法.有没有其他人有这方面的运气?

Looking through the docs, I'm not finding a way to access these values easily in code. Has anyone else had luck with this?

推荐答案

要获得通过 REST 端点公开的执行器端点的完全相同的属性,您可以在其中一个类中注入相应端点类的实例.在您的情况下,正确"端点类将是 InfoEndpoint.指标、健康状况等也有类似的端点类.

To get exactly the same properties of an actuator endpoint that are exposed through the REST endpoints, you can inject in one of your classes an instance of the respective endpoint class. In your case, the "right" endpoint class would be the InfoEndpoint. There are analogous endpoint classes for metrics, health, etc.

Spring Boot 1.5.x 和 Spring Boot 2.x 之间的界面略有变化.因此,确切的完全限定类名或读取方法名可能因您使用的 Spring Boot 版本而异.在 Boot 1.5.x 中,您可以在 org.springframework.boot.actuate.endpoint 包中找到大多数端点.

The interface has changed a little between Spring Boot 1.5.x and Spring Boot 2.x. So the exact fully qualified class name or read method name may vary based on the Spring Boot version that you are using. In Boot 1.5.x, you can find most of the endpoints in the org.springframework.boot.actuate.endpoint package.

粗略地说,这就是您如何构建一个简单的组件来读取您的版本属性(假设 info 端点内的属性名称只是 build.version):

Roughly, this is how you could build a simple component for reading your version property (assuming that the name of the property inside the info endpoint is simply build.version):

@Component
public class VersionAccessor {
    private final InfoEndpoint endpoint;

    @Autowired
    public VersionAccessor(InfoEndpoint endpoint) {
        this.endpoint = endpoint;
    }

    public String getVersion() {
        // Spring Boot 2.x
        return String.valueOf(getValueFromMap(endpoint.info()));

        // Spring Boot 1.x
        return String.valueOf(getValueFromMap(endpoint.invoke()));
    }

    // the info returned from the endpoint may contain nested maps
    // the exact steps for retrieving the right value depends on 
    // the exact property name(s). Here, we assume that we are 
    // interested in the build.version property
    private Object getValueFromMap(Map<String, Object> info) {
        return ((Map<String, Object>) info.get("build")).get("version");
    }

}

这篇关于Spring Boot Actuator 有 Java API 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆