spring-boot 速成(3) actuator

actuator 通过暴露一系列的endpoints可以让开发者快速了解spring boot的各项运行指标,比如:线程数,jvm剩余内存等一系列参数。

启用方法很简单,参考下面:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-devtools')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-test')
    compileOnly('org.projectlombok:lombok')
}  

关键是添加spring-boot-starter-actuator依赖项即可,下表是actuator提供的endpoints列表(从官网文档上抄过来的)

ID Description Sensitive Default

actuator


Provides a hypermedia-based “discovery page” for the other endpoints.Requires Spring HATEOAS to be on the classpath.


true


auditevents


Exposes audit events information for the current application.


true


autoconfig


Displays an auto-configuration report showing all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied.


true


beans


Displays a complete list of all the Spring beans in your application.


true


configprops


Displays a collated list of all @ConfigurationProperties.


true


dump


Performs a thread dump.


true


env


Exposes properties from Spring’s ConfigurableEnvironment.


true


flyway


Shows any Flyway database migrations that have been applied.


true


health


Shows application health information (when the application is secure,

a simple ‘status’ when accessed over an unauthenticated connection or

full message details when authenticated).


false


info


Displays arbitrary application info.


false


loggers


Shows and modifies the configuration of loggers in the application.


true


liquibase


Shows any Liquibase database migrations that have been applied.


true


metrics


Shows ‘metrics’ information for the current application.


true


mappings


Displays a collated list of all @RequestMapping paths.


true


shutdown


Allows the application to be gracefully shutdown (not enabled by default).


true


trace


Displays trace information (by default the last 100 HTTP requests).


true

这张表中,有很多信息其实是敏感信息,并不适合匿名访问(特别是在公网环境下),所以默认情况下,如果想访问类似 http://localhost:8081/metrics 会看到以下错误:

比较好的做法是,将这些endpoints的端口,包括访问路径与常规应用的端口分开,application.yml可以参考下面的配置:

server:
  port: 8081
spring:
  main:
    banner-mode: "off"
  devtools:
    restart:
      trigger-file: .trigger
  thymeleaf:
    cache: false
management:
  security:
    enabled: false #关掉安全认证
  port: 1101 #管理端口调整成1101
  context-path: /admin #actuator的访问路径  

如果在公网环境,建议在防火墙上做下限制,仅允许8081进来,1101用于内网访问即可,这样相对比较安全,也不用繁琐的输入密码。

访问下http://localhost:1101/admin/metrics 可以看到类似以下输出:

{
	mem: 466881,
	mem.free: 289887,
	processors: 4,
	instance.uptime: 10947,
	uptime: 18135,
	systemload.average: 3.12646484375,
	heap.committed: 411648,
	heap.init: 131072,
	heap.used: 121760,
	heap: 1864192,
	nonheap.committed: 56192,
	nonheap.init: 2496,
	nonheap.used: 55234,
	nonheap: 0,
	threads.peak: 27,
	threads.daemon: 19,
	threads.totalStarted: 32,
	threads: 22,
	classes: 6755,
	classes.loaded: 6755,
	classes.unloaded: 0,
	gc.ps_scavenge.count: 8,
	gc.ps_scavenge.time: 136,
	gc.ps_marksweep.count: 2,
	gc.ps_marksweep.time: 193,
	httpsessions.max: -1,
	httpsessions.active: 0
}

jvm的内存,cpu核数,线程数,gc情况一目了然。其它指标大概含义如下(网上抄来的)

系统信息:
    包括处理器数量processors、运行时间uptime和instance.uptime、系统平均负载systemload.average。
mem.*:
    内存概要信息,包括分配给应用的总内存数量以及当前空闲的内存数量。这些信息来自java.lang.Runtime。
heap.*:
    堆内存使用情况。这些信息来自java.lang.management.MemoryMXBean接口中getHeapMemoryUsage方法获取的java.lang.management.MemoryUsage。
nonheap.*:
    非堆内存使用情况。这些信息来自java.lang.management.MemoryMXBean接口中getNonHeapMemoryUsage方法获取的java.lang.management.MemoryUsage。
threads.*:
    线程使用情况,包括线程数、守护线程数(daemon)、线程峰值(peak)等,这些数据均来自java.lang.management.ThreadMXBean。
classes.*:
    应用加载和卸载的类统计。这些数据均来自java.lang.management.ClassLoadingMXBean。
gc.*:
    垃圾收集器的详细信息,包括垃圾回收次数gc.ps_scavenge.count、垃圾回收消耗时间gc.ps_scavenge.time、标记-清除算法的次数gc.ps_marksweep.count、标记-清除算法的消耗时间gc.ps_marksweep.time。这些数据均来自java.lang.management.GarbageCollectorMXBean。
httpsessions.*:
    Tomcat容器的会话使用情况。包括最大会话数httpsessions.max和活跃会话数httpsessions.active。该度量指标信息仅在引入了嵌入式Tomcat作为应用容器的时候才会提供。
gauge.*:
    HTTP请求的性能指标之一,它主要用来反映一个绝对数值。比如上面示例中的gauge.response.hello: 5,它表示上一次hello请求的延迟时间为5毫秒。
counter.*:
    HTTP请求的性能指标之一,它主要作为计数器来使用,记录了增加量和减少量。如上示例中counter.status.200.hello: 11,它代表了hello请求返回200状态的次数为11

结合其它一些工具把这些信息采集到grafana里,就有得到一系列很实用的监控图表数据,比如:  

其它endpoint,就不一一展示了,大家有兴趣可以自行研究,最后要提一下的是shutdown这个endpoint,它可以实现优雅停机,这在线上部署时很有用,发布前先调用这个url,让应用优雅停掉,再部署新的代码,这样就不会导致正在处理的请求被中断,不过默认该功能是关闭的,可参考下面的设置启用:

endpoints:
  shutdown:
    enabled: true 

而且出于安全考虑,该url只能以post方式访问,下图是用postman模拟post访问 http://locahost:1101/admin/shutdown的效果:

同时在日志里也能看到应用确实被关闭:

 

 

参考文章:

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready

时间: 2024-08-20 02:40:54

spring-boot 速成(3) actuator的相关文章

Spring Boot中使用Actuator的/info端点输出Git版本信息

对于Spring Boot的Actuator模块相信大家已经不陌生了,尤其对于其中的/health./metrics等强大端点已经不陌生(如您还不了解Actuator模块,建议先阅读<Spring Boot Actuator监控端点小结>).但是,其中还有一个比较特殊的端点/info经常被大家所忽视,因为从最初的理解,它主要用来输出application.properties配置文件中通过info前缀来定义的一些属性,由于乍看之下可能想不到太多应用场景,只是被用来暴露一些应用的基本信息,而基本

spring boot actuator专题

spring-boot-starter-actuator模块的实现对于实施微服务的中小团队来说,可以有效地减少监控系统在采集应用指标时的开发量.当然,它也并不是万能的,有时候我们也需要对其做一些简单的扩展来帮助我们实现自身系统个性化的监控需求.下面,在本文中,我们将详解的介绍一些关于spring-boot-starter-actuator模块的内容,包括它的原生提供的端点以及一些常用的扩展和配置方式. /autoconfig:该端点用来获取应用的自动化配置报告,其中包括所有自动化配置的候选项.同

深入学习微框架:Spring Boot

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以 及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者. 多年以来,Spring IO平台饱受非议的一点就是大量的XML配置以及复杂的依赖管理.在去年的SpringOne 2GX会议上,Pivotal的CTO Adrian Co

Spring Boot 项目构建 之 使用 Spring Boot 构建应用(Building an Application with Spring Boot)

Table of contents What you'll build What you'll need How to complete this guide Build with Gradle Build with Maven Build with Spring Tool Suite Learn what you can do with Spring Boot Create a simple web application Create an Application class Run the

Spring boot 通用配置文件模板

Spring boot 通用配置文件模板   001 # ===================================================================002 # COMMON SPRING BOOT PROPERTIES003 #004 # This sample file is provided as a guideline. Do NOT copy it in its005 # entirety to your own application.  

通过JMX监控Spring Boot应用

在Spring Boot应用的健康监控一文中,我们通过Spring Boot Actuator对外暴露应用的监控信息,除了使用HTTP获取JSON格式 的数据之外,还可以通过JMX监控应用,Spring Boot也提供了对JMX监控的支持.JMX监控对外暴露的信息相同,不过是使用MBeans容器将应用数据封装管理. 接下来我们看下如何利用JMX获取应用状态信息,以及如何使用Jolokia JMX库对外暴露MBeans的HTTP访问URL. Get Ready 在BookPub应用的pom文件中添

《Spring Boot官方指南》翻译邀请

学技术并发网始终建议同学们阅读原版官方文档,所以并发网每月都会组织大家翻译各种官方技术文档.9月份并发网组织大家翻译<Spring Boot官方指南>,欢迎有兴趣的同学参与. 如何领取  通过评论领取想要翻译的文章,每次领取一章,如Spring Boot Documentation,翻译完后再领取其他章节.领取完成之后,建议在一个星期内翻译完成,如果不能完成翻译,也欢迎你邀请其他同学和你一起完成翻译. 如何提交? 翻译完成之后请登录到并发网提交成待审核状态,会有专门的编辑校对后进行发布.校对完

用Kotlin写一个基于Spring Boot的RESTful服务

Spring太复杂了,配置这个东西简直就是浪费生命.尤其在没有什么并发压力,随便搞一个RESTful服务 让整个业务跑起来先的情况下,更是么有必要纠结在一堆的XML配置上.显然这么想的人是很多的,于是就 有了Spring Boot.又由于Java 8太墨迹于是有了Kotlin. 数据源使用MySql.通过Spring Boot这个基本不怎么配置的,不怎么微的微框架的Spring Data JPA和Hibernate 来访问数据. 处理依赖 这里使用Gradle来处理依赖. 首先下载官网给的初始项

Spring Boot POM 详解

        正如这幅图所展示的那样,在Spring IO Framework体系中,Spring Boot处在Execution layer,来看看官方对这层的解释:     The Spring IO Execution layer provides domain-specific runtimes (DSRs) for applications built on the IO Foundation modules. A DSR may run standalone without req

Spring Boot Admin的使用

上一篇文章中了解了Spring Boot提供的监控接口,例如:/health./info等等,实际上除了之前提到的信息,还有其他信息业需要监控:当前处于活跃状态的会话数量.当前应用的并发数.延迟以及其他度量信息.这次我们了解如何利用Spring-boot-admin对应用信息进行可视化,如何添加度量信息. 准备 spring-boot-admin的Github地址在:https://github.com/codecentric/spring-boot-admin,它在Spring Boot Ac