Spring Boot Customize Actuator Info Endpoint Example Configuration

摘要: In this tutorial, we demonstrate how to customize the spring boot actuator /info endpoint.

In this tutorial, we demonstrate how to customize the spring boot actuator /info endpoint.

Maven Dependencies

We use Apache Maven to manage our project dependencies. Make sure the following dependencies reside on the class-path.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.spring.boot</groupId>
    <artifactId>custom-info-endpoint</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <url>https://memorynotfound.com</url>
    <name>Spring Boot - ${project.artifactId}</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Bootstrap Spring Application

We use spring-boot to bootstrap the application.

package com.memorynotfound.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Spring Boot Customize Actuator Info Endpoint Configuration

We can customize the spring actuator info endpoint using the following properties.

  • static properties – we can add static information using key value pairs.
  • maven build properties – spring boot automatically exposes maven build properties. We can access them using the @[email protected] syntax.
  • environment variables – we can expose environment variables.
  • loading properties from java class – we can load dynamic properties from java classes by implementing the InfoContributor interface.
## Spring Boot Actuator Info Endpoint Customization
info:

  # static properties
  app:
    name: Spring Boot Actuator Info Endpoint Configuration Example
    description: This tutorial demonstrates how to customize the Spring Boot Actuator Info Endpoint.

  # build properties from maven
  build:
    groupId: @[email protected]
    artifact: @[email protected]
    name: @[email protected]
    version: @[email protected]

  # environment variables
  env:
    java:
      vendor: ${java.specification.vendor}
      vm-name: ${java.vm.name}
      runtime-version: ${java.runtime.version}

logging:
  level:
      - ".=info"
      - "com.memorynotfound=debug"
      - "org.springframework=info"

Loading Properties from Java Classes

We can dynamically inject properties from a class by implementing the InfoContributor interface. This allows us to inject static/dynamic properties or even do a database lookup. The possibilities are endless here.

package com.memorynotfound.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class MetaDataContributor implements InfoContributor {

    @Autowired
    private ApplicationContext ctx;

    @Override
    public void contribute(Info.Builder builder) {
        Map<String, Object> details = new HashMap<>();
        details.put("bean-definition-count", ctx.getBeanDefinitionCount());
        details.put("startup-date", ctx.getStartupDate());

        builder.withDetail("context", details);
    }
}

Accessing Spring Boot Actuator Info Endpoint

When the application is started and we go to http://localhost:8080/info we can access the actuator info endpoint. The following is the example output of the application configured earlier.

{
  "app": {
    "name": "Spring Boot Actuator Info Endpoint Configuration Example",
    "description": "This tutorial demonstrates how to customize the Spring Boot Actuator Info Endpoint."
  },
  "build": {
    "groupId": "com.memorynotfound.spring.boot",
    "artifact": "custom-info-endpoint",
    "name": "Spring Boot - custom-info-endpoint",
    "version": "1.0.0-SNAPSHOT"
  },
  "env": {
    "java": {
      "vendor": "Oracle Corporation",
      "vm-name": "Java HotSpot(TM) 64-Bit Server VM",
      "runtime-version": "1.8.0_20-b26"
    }
  },
  "context": {
    "bean-definition-count": 187,
    "startup-date": 1507802588645
  }
}

Here is a screenshot of the actuator endpoint.

Download

上一篇: Spring Cloud Eureka Service Discovery Client Server Example
下一篇: Setting and Reading Spring JMS Message Header Properties Example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号