前言

本文介绍如何集成actuator,以及集成针对actuator接口数据提供UI美化封装的监控工具spring-boot-admin

使用的依赖版本:
spring-boot-starter-parent: 2.3.8.RELEASE,
spring-boot-admin-starter-server: 2.3.1


一. 新建springboot项目,引入actuator

  1. 创建springboot项目
    略,我配置的端口是9998
  2. 引入actuator依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

引入依赖之后启动项目,并访问:http://localhost:9998/actuator
因为actuator默认仅开启health,info端点,所以将看到下面的返回信息.

{
    "_links": {
        "self": {
            "href": "http://localhost:9998/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:9998/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:9998/actuator/health/{*path}",
            "templated": true
        },
        "info": {
            "href": "http://localhost:9998/actuator/info",
            "templated": false
        }
    }
}
  1. 配置监控端点
    在第二步的基础上,配置更多的监控端点,在yml配置中添加监控所有端点的配置项:
management:
  endpoints:
    web:
      exposure:
        include: "*"

再次访问:http://localhost:9998/actuator
返回信息:

{
"_links": {
    "self": {
        "href": "http://localhost:9998/actuator",
        "templated": false
    },
    "beans": {
        "href": "http://localhost:9998/actuator/beans",
        "templated": false
    },
    "caches-cache": {
        "href": "http://localhost:9998/actuator/caches/{cache}",
        "templated": true
    },
    "caches": {
        "href": "http://localhost:9998/actuator/caches",
        "templated": false
    },
    "health": {
        "href": "http://localhost:9998/actuator/health",
        "templated": false
    },
    "health-path": {
        "href": "http://localhost:9998/actuator/health/{*path}",
        "templated": true
    },
    "info": {
        "href": "http://localhost:9998/actuator/info",
        "templated": false
    },
    "conditions": {
        "href": "http://localhost:9998/actuator/conditions",
        "templated": false
    },
    "configprops": {
        "href": "http://localhost:9998/actuator/configprops",
        "templated": false
    },
    "env": {
        "href": "http://localhost:9998/actuator/env",
        "templated": false
    },
    "env-toMatch": {
        "href": "http://localhost:9998/actuator/env/{toMatch}",
        "templated": true
    },
    "loggers": {
        "href": "http://localhost:9998/actuator/loggers",
        "templated": false
    },
    "loggers-name": {
        "href": "http://localhost:9998/actuator/loggers/{name}",
        "templated": true
    },
    "heapdump": {
        "href": "http://localhost:9998/actuator/heapdump",
        "templated": false
    },
    "threaddump": {
        "href": "http://localhost:9998/actuator/threaddump",
        "templated": false
    },
    "metrics-requiredMetricName": {
        "href": "http://localhost:9998/actuator/metrics/{requiredMetricName}",
        "templated": true
    },
    "metrics": {
        "href": "http://localhost:9998/actuator/metrics",
        "templated": false
    },
    "scheduledtasks": {
        "href": "http://localhost:9998/actuator/scheduledtasks",
        "templated": false
    },
    "mappings": {
        "href": "http://localhost:9998/actuator/mappings",
        "templated": false
    }
}}
  1. 获取系统监控信息
    各个监控端点的访问方式已返回在对应端点的href中;
    举例:获取系统cpu使用率信息
    从第3步的返回信息中找到:
"metrics-requiredMetricName": {
    "href": "http://localhost:9998/actuator/metrics/{requiredMetricName}",
    "templated": true
},
"metrics": {
    "href": "http://localhost:9998/actuator/metrics",
    "templated": false
},

访问http://localhost:9998/actuator/metrics:

{
    "names": [
        "http.server.requests",
        "jvm.buffer.count",
        "jvm.buffer.memory.used",
        "jvm.buffer.total.capacity",
        "jvm.classes.loaded",
        "jvm.classes.unloaded",
        "jvm.gc.live.data.size",
        "jvm.gc.max.data.size",
        "jvm.gc.memory.allocated",
        "jvm.gc.memory.promoted",
        "jvm.gc.pause",
        "jvm.memory.committed",
        "jvm.memory.max",
        "jvm.memory.used",
        "jvm.threads.daemon",
        "jvm.threads.live",
        "jvm.threads.peak",
        "jvm.threads.states",
        "logback.events",
        "process.cpu.usage",
        "process.start.time",
        "process.uptime",
        "system.cpu.count",
        "system.cpu.usage",
        "tomcat.sessions.active.current",
        "tomcat.sessions.active.max",
        "tomcat.sessions.alive.max",
        "tomcat.sessions.created",
        "tomcat.sessions.expired",
        "tomcat.sessions.rejected"
    ]
}

names中system.cpu.usage表示系统cpu的使用率,
因此将system.cpu.usage代入metrics-requiredMetricName的href中,
得到获取cpu使用率的url并访问:
http://localhost:9998/actuator/metrics/system.cpu.usage 返回信息:

{
    "name": "system.cpu.usage",
    "description": "The \"recent cpu usage\" for the whole system",
    "baseUnit": null,
    "measurements": [
        {
            "statistic": "VALUE",
            "value": 0.13861874171047217
        }
    ],
    "availableTags": []
}

cpu使用率为: 0.13861874171047217
说明:上述请求url均为get请求

二. 集成Spring Boot Admin

介绍:
Spring Boot Admin 是一个管理和监控Spring Boot 应用程序的开源软件。每个应用都认为是一个客户端,通过HTTP或者使用 Eureka注册到admin server中进行展示,Spring Boot Admin UI部分使用AngularJs将数据展示在前端。
springbootadmin需要客户端及服务端两个项目。

2.1 spring-boot-admin-server

  1. 创建springboot项目
    略,并配置端口为9999
  2. 引入spring-boot-admin-server依赖
<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-server</artifactId>
	<version>2.3.1</version>
</dependency>
  1. 开启spring-boot-admin-server
    在项目启动类上添加注解:@EnableAdminServer
@SpringBootApplication
@EnableAdminServer
public class ActuatorServerApplication {

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

}
  1. 访问admin server http://localhost:9999
    由于还没有配置admin-client,目前应用为0

2.2 spring-boot-admin-client

在第一步(一. 新建springboot项目,引入actuator)的项目中集成spring-boot-admin-client

  1. 引入spring-boot-admin-client依赖
<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-client</artifactId>
	<version>2.3.1</version>
</dependency>
  1. 配置文件增加admin-client配置信息
spring:
  boot:
    admin:
      client:
        enabled: true
        url: http://localhost:9999
  1. 再次访问admin server http://localhost:9999
    将可以看到此项目已在列表中:

    按照图中说明进行操作将看到监控页面:

新增特性:

3. 启用httptrace, 跟踪http请求

  • springboot版本<2.2.0.RELEASE时,httptrace默认启用
  • springboot版本>=2.2.0.RELEASE时,httptrace置为不启用,
    就算配置management.endpoint.httptrace.enabled=true也是不会启用,
    需要将接口HttpTraceRepository的实现类添加到spring ioc容器, 重新启动admin-client项目.(InMemoryHttpTraceRepository是springboot提供的实现类)
    代码示例:
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringBootAdminConfig {
    @Bean
    public InMemoryHttpTraceRepository getInMemoryHttpTrace(){
        return new InMemoryHttpTraceRepository();
    }
}
  • 再次访问: http://localhost:9999
    可以看到已经出现Http追踪