侧边栏壁纸
博主头像
soulballad博主等级

技术文章记录及总结

  • 累计撰写 169 篇文章
  • 累计创建 26 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录

【从零开始学Spring Boot】-8.Spring Boot Actuator监控

soulballad
2020-07-20 / 0 评论 / 0 点赞 / 55 阅读 / 6,452 字
温馨提示:
本文最后更新于 2022-04-19,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1.简介

1.1 概述

Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.

Spring Boot包含许多附加功能,可在您将应用程序投入生产时帮助您监控和管理应用程序。您可以选择使用HTTP端点或JMX管理和监控您的应用程序。审计,健康状况和指标收集也可以自动应用于您的应用程序。

1.2 特点

Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.

监控端点使您可以监控应用程序并与之交互。 Spring Boot包含许多内置端点,您可以添加自己的端点。例如,运行健康状况端点提供基本的应用程序运行状况信息。

The following technology-agnostic endpoints are available:

IDDescription
auditeventsExposes audit events information for the current application. Requires an AuditEventRepository bean.
beansDisplays a complete list of all the Spring beans in your application.
cachesExposes available caches.
conditionsShows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.
configpropsDisplays a collated list of all @ConfigurationProperties.
envExposes properties from Spring’s ConfigurableEnvironment.
flywayShows any Flyway database migrations that have been applied. Requires one or more Flyway beans.
healthShows application health information.
httptraceDisplays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean.
infoDisplays arbitrary application info.
integrationgraphShows the Spring Integration graph. Requires a dependency on spring-integration-core.
loggersShows and modifies the configuration of loggers in the application.
liquibaseShows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans.
metricsShows ‘metrics’ information for the current application.
mappingsDisplays a collated list of all @RequestMapping paths.
scheduledtasksDisplays the scheduled tasks in your application.
sessionsAllows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session.
shutdownLets the application be gracefully shutdown. Disabled by default.
threaddumpPerforms a thread dump.

If your application is a web application (Spring MVC, Spring WebFlux, or Jersey), you can use the following additional endpoints:

IDDescription
heapdumpReturns an hprof heap dump file.
jolokiaExposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux). Requires a dependency on jolokia-core.
logfileReturns the contents of the logfile (if logging.file.name or logging.file.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content.
prometheusExposes metrics in a format that can be scraped by a Prometheus server. Requires a dependency on micrometer-registry-prometheus.

2.演示环境

  1. JDK 1.8.0_201
  2. Spring Boot 2.2.0.RELEASE
  3. 构建工具(apache maven 3.6.3)
  4. 开发工具(IntelliJ IDEA )

3.演示代码

3.1 代码说明

开启 endpoint,使用 http 进行访问

3.2 代码结构

3.3 maven 依赖

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

3.4 配置文件

info.app.version=1.0.0
info.app.name=spring-boot-actuator
info.app.test=test

# 启动所有endpoint
management.endpoints.web.exposure.include=*
# 显示详细信息
management.endpoint.health.show-details=always

# 关闭应用程序
management.endpoint.shutdown.enabled=true

3.5 java代码

HelloController.java

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello world";
    }
}

3.6 git 地址

spring-boot/spring-boot-05-basis/spring-boot-actuator

4.效果展示

启动 SpringBootActuatorApplication.main 方法,在 spring-boot-actuator.http 访问下列地址,观察输出信息。

### GET /actuator
GET http://localhost:8080/actuator

请求响应信息如下

GET http://localhost:8080/actuator

HTTP/1.1 200 
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Sun, 19 Jul 2020 10:05:32 GMT

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "beans": {
      "href": "http://localhost:8080/actuator/beans",
      "templated": false
    },
    "caches-cache": {
      "href": "http://localhost:8080/actuator/caches/{cache}",
      "templated": true
    },
    "caches": {
      "href": "http://localhost:8080/actuator/caches",
      "templated": false
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    },
    "health-path": {
      "href": "http://localhost:8080/actuator/health/{*path}",
      "templated": true
    },
    "info": {
      "href": "http://localhost:8080/actuator/info",
      "templated": false
    },
    "conditions": {
      "href": "http://localhost:8080/actuator/conditions",
      "templated": false
    },
    "shutdown": {
      "href": "http://localhost:8080/actuator/shutdown",
      "templated": false
    },
    "configprops": {
      "href": "http://localhost:8080/actuator/configprops",
      "templated": false
    },
    "env": {
      "href": "http://localhost:8080/actuator/env",
      "templated": false
    },
    "env-toMatch": {
      "href": "http://localhost:8080/actuator/env/{toMatch}",
      "templated": true
    },
    "loggers": {
      "href": "http://localhost:8080/actuator/loggers",
      "templated": false
    },
    "loggers-name": {
      "href": "http://localhost:8080/actuator/loggers/{name}",
      "templated": true
    },
    "heapdump": {
      "href": "http://localhost:8080/actuator/heapdump",
      "templated": false
    },
    "threaddump": {
      "href": "http://localhost:8080/actuator/threaddump",
      "templated": false
    },
    "metrics": {
      "href": "http://localhost:8080/actuator/metrics",
      "templated": false
    },
    "metrics-requiredMetricName": {
      "href": "http://localhost:8080/actuator/metrics/{requiredMetricName}",
      "templated": true
    },
    "scheduledtasks": {
      "href": "http://localhost:8080/actuator/scheduledtasks",
      "templated": false
    },
    "mappings": {
      "href": "http://localhost:8080/actuator/mappings",
      "templated": false
    }
  }
}

Response code: 200; Time: 48ms; Content length: 1659 bytes

5.参考

  1. 官方文档-Spring Boot Features/Actuator
0

评论区