springboot研究:springboot自带监控actuator

简介: springboot研究:springboot自带监控actuator

springboot中自带监控工具actuator,在对监控要求不高的情况下,使用actuator就可以满足系统监控要求了。使用actuator,需要添加依赖

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

本文使用2.1.6版本,配置文件如下

management.server.address=127.0.0.1
management.server.port=18082
management.endpoints.web.exposure.include=*
management.endpoints.web.base-path=/actuator
management.endpoint.shutdown.enabled=true
graceful.shutdown.wait=30
management.endpoint.health.show-details=always点击并拖拽以移动

上面配置指定了actuator对外暴露的地址是127.0.0.1,端口是18082,对外暴露服务的所有访问点,具体见下面地址:

https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/actuator-api/html/#sh

基础路径是/actuator,允许关闭应用(一般不开启),优雅关闭等待30s,总是允许查看详细的应用健康信息。

下面依次介绍一下各个信息的详情

1.info主要展示一些系统发布的版本信息,配置一下info信息

info.app.name=springboot-mybatis
info.app.description=two-datasource-test
info.app.version=1.0
info.app.spring-boot-version=2.1.6

启动工程后,浏览器输入:http://127.0.0.1:18082/actuator/info,输出如下:

{
  "app": {
    "name": "springboot-security",
    "description": "springboot-security-test",
    "version": "1.0",
    "spring-boot-version": "2.1.6"
  }
}

2. beans主要展示所有的bean,包括自定义的,以及这些bean之间的关系

输入:http://127.0.0.1:18082/actuator/beans,输入所有的bean,下图截取了一部分

微信图片_20221212102809.png

3.health介绍系统的健康状态,

输入:http://127.0.0.1:18082/actuator/health,输出如下结果:

{
  "status": "UP",
  "details": {
    "db": {
      "status": "UP",
      "details": {
        "firstDataSource": {
          "status": "UP",
          "details": {
            "database": "MySQL",
            "hello": 1
          }
        },
        "secondDataSource": {
          "status": "UP",
          "details": {
            "database": "MySQL",
            "hello": 1
          }
        }
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 499972567040,
        "free": 396368695296,
        "threshold": 10485760
      }
    }
  }
}

4.Configuration Properties 主要展示有@ConfigurationProperties注解的bean

浏览器输入:http://127.0.0.1:18082/actuator/configprops,输出部分截图如下,黄色的bean就是我在程序中定义的微信图片_20221212102858.png

5.environment获取应用的所有环境变量

输入http://127.0.0.1:18082/actuator/env,返回结果部分截图如下:

微信图片_20221212102916.png

也可以查看具体某一个环境变量的值,比如输入:

http://localhost:18082/actuator/env/server.port,

输出如下:

{
    "property": {
        "source": "applicationConfig: [classpath:/application.properties]",
        "value": "8080"
    },
    "activeProfiles": [],
    "propertySources": [{
        "name": "server.ports"
    }, {
        "name": "servletConfigInitParams"
    }, {
        "name": "servletContextInitParams"
    }, {
        "name": "systemProperties"
    }, {
        "name": "systemEnvironment"
    }, {
        "name": "random"
    }, {
        "name": "applicationConfig: [classpath:/application.properties]",
        "property": {
            "value": "8080",
            "origin": "class path resource [application.properties]:28:13"
        }
    }]
}

6. Audit Events提供应用的审核事件信息

这个需要配合spring-security使用,具体源码见:

https://github.com/jinjunzhu/spring-boot-security

浏览器输入http://127.0.0.1:18082/login,页面如下:

微信图片_20221212103015.png

输入用户名:user,密码:password,登录成功后就可以使用了

浏览器输入url:http://127.0.0.1:18082/actuator/auditevents

{
  "events": [{
    "timestamp": "2020-05-06T02:52:25.880Z",
    "principal": "anonymousUser",
    "type": "AUTHORIZATION_FAILURE",
    "data": {
      "details": {
        "remoteAddress": "0:0:0:0:0:0:0:1",
        "sessionId": null
      },
      "type": "org.springframework.security.access.AccessDeniedException",
      "message": "Access is denied"
    }
  }, {
    "timestamp": "2020-05-06T02:56:09.375Z",
    "principal": "user",
    "type": "AUTHENTICATION_SUCCESS",
    "data": {
      "details": {
        "remoteAddress": "0:0:0:0:0:0:0:1",
        "sessionId": "8B61F133E87B51975CF5BA2D00EAD790"
      }
    }
  }, {
    "timestamp": "2020-05-06T03:05:42.352Z",
    "principal": "anonymousUser",
    "type": "AUTHORIZATION_FAILURE",
    "data": {
      "details": {
        "remoteAddress": "127.0.0.1",
        "sessionId": null
      },
      "type": "org.springframework.security.access.AccessDeniedException",
      "message": "Access is denied"
    }
  }, {
    "timestamp": "2020-05-06T03:07:54.183Z",
    "principal": "user",
    "type": "AUTHENTICATION_SUCCESS",
    "data": {
      "details": {
        "remoteAddress": "127.0.0.1",
        "sessionId": "52D3515D26413A408FB53158A52520FE"
      }
    }
  }]
} {
  "property": {
    "source": "applicationConfig: [classpath:/application.properties]",
    "value": "8080"
  },
  "activeProfiles": [],
  "propertySources": [{
    "name": "server.ports"
  }, {
    "name": "servletConfigInitParams"
  }, {
    "name": "servletContextInitParams"
  }, {
    "name": "systemProperties"
  }, {
    "name": "systemEnvironment"
  }, {
    "name": "random"
  }, {
    "name": "applicationConfig: [classpath:/application.properties]",
    "property": {
      "value": "8080",
      "origin": "class path resource [application.properties]:28:13"
    }
  }]
}

从上面可看到user用户认证成功。


7.Caches用于获取应用的缓存

浏览器输入url:http://localhost:8080/actuator/caches可以返回应用中配置的缓存

也可以向应用发送一个DELETE请求清除缓存,

curl 'http://localhost:8080/actuator/caches' -i -X DELETE点击并拖拽以移动

也可以按照名称清除缓存

curl 'http://localhost:8080/actuator/caches/countries?cacheManager=anotherCacheManager' -i -X DELETE

8.Conditions Evaluation Report提供配置和自动配置的条件值,浏览器输入:http://127.0.0.1:18082/actuator/conditions,输出结果如下:

微信图片_20221212103200.png

9.Flyway提供数据库使用Flyway迁移数据的情况

curl 'http://localhost:18082/actuator/flyway' -i -X GET


10.Heap Dump提供应用jvm的堆dump信息

url:curl 'http://localhost:18082/actuator/heapdump' -O


11.HTTP Trace提供应用的请求-响应的交互信息

url:curl 'http://localhost:18082/actuator/httptrace' -i -X GET


12.Spring Integration graph用来查看spring集成的所有组件

url:curl 'http://localhost:18082/actuator/integrationgraph' -i -X GET


13.liquibase用来查看使用liquibase工具进行数据库迁移和重构的工具

url:curl 'http://localhost:18082/actuator/liquibase' -i -X GET


14.Log File获取应用日志文件的内容

url:curl 'http://localhost:18082/actuator/logfile' -i -X GET


15.Loggers获取应用的日志配置以及日志级别

url:curl 'http://localhost:18082/actuator/loggers' -i -X GET


16.Mappings用来查看应用的request mapping

url:curl 'http://localhost:44209/actuator/mappings' -i -X GET


17.Metrics用来监控应用的各项指标

url:curl 'http://localhost:18082/actuator/metrics' -i -X GET

返回结果如下:

{
    "names": ["jvm.memory.max", "jvm.threads.states", "http.server.requests", "jvm.gc.memory.promoted", "jvm.memory.used", "jvm.gc.max.data.size", "jdbc.connections.max", "jdbc.connections.min", "jvm.gc.pause", "jvm.memory.committed", "system.cpu.count", "logback.events", "tomcat.global.sent", "jvm.buffer.memory.used", "tomcat.sessions.created", "jvm.threads.daemon", "system.cpu.usage", "jvm.gc.memory.allocated", "tomcat.global.request.max", "hikaricp.connections.idle", "hikaricp.connections.pending", "tomcat.global.request", "tomcat.sessions.expired", "hikaricp.connections", "jvm.threads.live", "jvm.threads.peak", "tomcat.global.received", "hikaricp.connections.active", "hikaricp.connections.creation", "process.uptime", "tomcat.sessions.rejected", "process.cpu.usage", "tomcat.threads.config.max", "jvm.classes.loaded", "hikaricp.connections.max", "hikaricp.connections.min", "jvm.classes.unloaded", "tomcat.global.error", "tomcat.sessions.active.current", "tomcat.sessions.alive.max", "jvm.gc.live.data.size", "hikaricp.connections.usage", "tomcat.threads.current", "hikaricp.connections.timeout", "jvm.buffer.count", "jvm.buffer.total.capacity", "tomcat.sessions.active.max", "hikaricp.connections.acquire", "tomcat.threads.busy", "process.start.time"]
}点击并拖拽以移动

18.Prometheus以Prometheus格式提供springboot应用监控指标值

url: curl 'http://localhost:18082/actuator/prometheus' -i -X GET

19.Scheduled Tasks监控定时任务信息

url:curl 'http://localhost:18082/actuator/scheduledtasks' -i -X GET

返回示例如下:

{
  "cron" : [ {
    "runnable" : {
      "target" : "com.example.Processor.processOrders"
    },
    "expression" : "0 0 0/3 1/1 * ?"
  } ],
  "fixedDelay" : [ {
    "runnable" : {
      "target" : "com.example.Processor.purge"
    },
    "initialDelay" : 5000,
    "interval" : 5000
  } ],
  "fixedRate" : [ {
    "runnable" : {
      "target" : "com.example.Processor.retrieveIssues"
    },
    "initialDelay" : 10000,
    "interval" : 3000
  } ],
  "custom" : [ {
    "runnable" : {
      "target" : "com.example.Processor$CustomTriggeredRunnable"
    },
    "trigger" : "com.example.Processor$CustomTrigger@2c6bb64b"
  } ]
}

20.Sessions提供使用spring session管理的session

url:curl 'http://localhost:18082/actuator/sessions?username=alice' -i -X GET

21.Thread Dump提供应用jvm的线程dump信息

url:curl 'http://localhost:18082/actuator/threaddump' -i -X GET

22.Shutdown用来关闭应用,一般不建议配置

url:curl 'http://localhost:18082/actuator/shutdown' -i -X POST


最后,springboot的健康检查都是实现了接口HealthIndicator,AbstractHealthIndicator做了一个抽象实现,其他实现类继承AbstractHealthIndicator来进行收集健康信息。

当然,我们也可以自己实现健康检查,只要实现HealthIndicator接口或者继承AbstractHealthIndicator抽象类就可以。

状态收集类都实现了HealthAggregator接口,AbstractHealthAggregator是HealthAggregator的抽象实现,OrderedHealthAggregator继承了AbstractHealthAggregator,

定义了状态收集的顺序,可以进行配置,参数management.health.status.order。我们可以实现HealthAggregator接口来增加新的Status,也可以在配置中实现,比如

management.health.status.order=Error, DOWN, OUT_OF_SERVICE, UNKNOWN, UP
management.health.status.http-mapping.FATAL=500

配置类见HealthIndicatorProperties.java。


本文源码地址:

https://github.com/jinjunzhu/spring-boot-security

actuator源码:

https://github.com/spring-projects/spring-boot/tree/v2.0.1.RELEASE/spring-boo

参考:


https://docs.spring.io/spring-boot/docs/2.1.6.RELEASE/actuator-api/html/#shutdown


相关文章
|
2月前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
4月前
|
监控 Java 数据库连接
Spring Boot中的健康检查和监控
Spring Boot中的健康检查和监控
|
1月前
|
监控 Dubbo Java
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
这篇文章详细介绍了如何将Spring Boot与Dubbo和Zookeeper整合,并通过Dubbo管理界面监控服务注册情况。
87 0
dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。
消息中间件 缓存 监控
122 0
|
3月前
|
Java Spring 监控
Spring Boot Actuator:守护你的应用心跳,让监控变得触手可及!
【8月更文挑战第31天】Spring Boot Actuator 是 Spring Boot 框架的核心模块之一,提供了生产就绪的特性,用于监控和管理 Spring Boot 应用程序。通过 Actuator,开发者可以轻松访问应用内部状态、执行健康检查、收集度量指标等。启用 Actuator 需在 `pom.xml` 中添加 `spring-boot-starter-actuator` 依赖,并通过配置文件调整端点暴露和安全性。Actuator 还支持与外部监控工具(如 Prometheus)集成,实现全面的应用性能监控。正确配置 Actuator 可显著提升应用的稳定性和安全性。
139 0
|
4月前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
296 6
|
4月前
|
监控 Java 微服务
Spring Boot微服务部署与监控的实战指南
【7月更文挑战第19天】Spring Boot微服务的部署与监控是保障应用稳定运行和高效维护的重要环节。通过容器化部署和云平台支持,可以实现微服务的快速部署和弹性伸缩。而利用Actuator、Prometheus、Grafana等监控工具,可以实时获取应用的运行状态和性能指标,及时发现并解决问题。在实际操作中,还需根据应用的具体需求和场景,选择合适的部署和监控方案,以达到最佳效果。
|
4月前
|
Prometheus 监控 Cloud Native
使用Spring Boot和Prometheus进行监控
使用Spring Boot和Prometheus进行监控
|
4月前
|
运维 Prometheus 监控
Spring Boot中使用Actuator监控应用状态
Spring Boot中使用Actuator监控应用状态
|
4月前
|
Prometheus 监控 Cloud Native
Spring Boot中使用Micrometer进行指标监控
Spring Boot中使用Micrometer进行指标监控
下一篇
无影云桌面