Springboot actuator应用后台监控实现

一 前言

springboot 额外的特色是提供了后台应用监控,可以通过 HTTP 或者 JMX的方式管理监控应用,本文主讲HTTP方式;其主要的功能是监控应用的健康状态,查看环境变量等;

二 pom.xml

springboot 2.1.1,主要引入 actuator 依赖,web依赖用于测试;

		 <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>

三 默认开启端点

3.1 默认端点 health

直接编写主程序入口,启动;浏览器输入 http://localhost:8080/actuator/health;结果如下,状态是UP;

翻翻源码heath状态码如下

public OrderedHealthAggregator() {
  this.setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, Status.UNKNOWN);
 }
  • DOWN 服务无法获得,状态码503;
  • .OUT_OF_SERVICE 服务无法获得,状态码503;
  • UP 获得服务,状态码200;
  • UNKNOWN 获得未知服务,状态码200;

在 application.yml 中配置 healthy 信息 示例如下:

management: endpoint: health: show-details: always

打印详细信息:

基本配置如下:

never :默认,表示不显示详细信息;when-authorized:详细信息显示给 认证过的用户;使用

management.endpoint.health.roles 配置always: 显示详细信息给所有用户3.2 默认端点 info

浏览器输入 http://localhost:8080/actuator/info; 展示空信息如下图:

在application.yml 中 配置工程 info 信息 示例如下;

#配置信息info: actuator: name: springboot-actutor version: 1.0.0 author: zszxz

展示结果如下:

四 HTTP端点说明

端点 端点描述 默认值
auditevents 当前应用的审计事件 Yes
beans 显示spring IOC 容器加载的所有bean Yes
caches 显示可获得的缓存 Yes
conditions 显示自动配置通过condition判断匹配或者不匹配的配置信息 Yes
configprops 显示 通过 @ConfigurationProperties 配置的属性信息 Yes
env spring环境变量属性信息 Yes
flyway 显示flyway 配置数据库已经迁移的信息 Yes
health 显示应用的健康信息 Yes
httptrace 显示 HTTP 轨迹信息默认最新的100 HTTP request或response Yes
info 显示自定义的应用信息 Yes
integrationgraph 显示spring 整合 graph 信息 Yes
loggers 显示配置文件中日志修改信息 Yes
liquibase 显示 任意的 Liquibase 数据库已经迁移的信息 Yes
metrics 显示当前应用的指标 Yes
mappings 显示 @RequestMapping paths. 配置的路径信息 Yes
scheduledtasks 显示任务调度信息 Yes
sessions 删除或者恢复Spring Session会话,不支持web响应式编程 Yes
shutdown 关闭应用 No
threaddump 执行一个线程转储 Yes

五 配置开启端点

application.yml 中配置需要开启的端点,其中 * 表示开启所有端点,示例如下:

management:
 endpoints:
 web:
  exposure:
  # 使用通配符 * 表示匹配所有端点
  # 排除的端点
  exclude: caches
  # 包括的端点
  include: info,health,beans,env,shutdown,threaddump

5.1 threaddump示例

http://localhost:8080/actuator/threaddump ;用于返回线程快照,分析线程阻塞,死锁等,部分内容如下

{
	"threads": [{
		"threadName": "DestroyJavaVM",
		"threadId": 41,
		"blockedTime": -1,
		"blockedCount": 0,
		"waitedTime": -1,
		"waitedCount": 0,
		"lockName": null,
		"lockOwnerId": -1,
		"lockOwnerName": null,
		"inNative": false,
		"suspended": false,
		"threadState": "RUNNABLE",
		"stackTrace": [],
		"lockedMonitors": [],
		"lockedSynchronizers": [],
		"lockInfo": null
	}

5.2 beans示例

http://localhost:8080/actuator/beans ; 用于返回 spring 容器加载的所有bean,部分内容如下;

{
	"contexts": {
		"application": {
			"beans": {
				"endpointCachingOperationInvokerAdvisor": {
					"aliases": [],
					"scope": "singleton",
					"type": "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
					"resource": "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
					"dependencies": ["environment"]
				},
				"defaultServletHandlerMapping": {
					"aliases": [],
					"scope": "singleton",
					"type": "org.springframework.web.servlet.HandlerMapping",
					"resource": "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]",
					"dependencies": []
				}

5.3 关闭应用示例

普通情况下是没有开启这个配置,是比较危险的动作,会导致应用停止;修改application.yml配置如下

management:
 endpoints:
 web:
  exposure:
  # 使用通配符 * 表示匹配所有端点
  # 排除的端点
  exclude: caches
  # 包括的端点
  include: info,health,beans,env,shutdown
 endpoint:
 health:
  show-details: always
 # 开启关闭应用 需要post请求
 shutdown:
  enabled: true

访问地址 http://localhost:8080/actuator/shutdown; 注意仅支持使用POST请求,否则 会 405错误;

六 CORS 支持

application.yml 修改配置如下, allowed-origins 中允许跨域的ip地址; allowed-methods 配置 允许通过的请求,还有支持时间等;

management:
 endpoints:
 web:
  exposure:
  # 使用通配符 * 表示匹配所有端点
  # 排除的端点
  exclude: caches
  # 包括的端点
  include: info,health,beans,env,shutdown
  # 跨域处理
  cors:
  allowed-origins: http://localhost:8080/
  allowed-methods: post,delete,get,put
 endpoint:
 health:
  show-details: always
 # 开启关闭应用 需要post请求
 shutdown:
  enabled: true

七 修改默认路径

在 配置文件中添加 base-path , 会修改掉默认路径 actuator/endpoint;

management:
 endpoints:
 web:
  exposure:
  # 使用通配符 * 表示匹配所有端点
  # 排除的端点
  exclude: caches
  # 包括的端点
  include: info,health,beans,env,shutdown
  # 自定义配置监控路径
  base-path: /zszxz
  # 跨域处理
  cors:
  allowed-origins: http://localhost:8080/
  allowed-methods: post,delete,get,put
 endpoint:
 health:
  show-details: always
 # 开启关闭应用 需要post请求
 shutdown:
  enabled: true

示例url: http://localhost:8080/zszxz/info

结果如下

八 其他配置说明

还可以引入 security 依赖 配置 账号密码,角色信息,达到访问控制,详细的可以参照官网;

还可以使用注解进行配置,自定义端点,详细参照官网;

jmx支持,可以使用open jdk 自带的工具 jconsole 进行监控;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • spring boot starter actuator(健康监控)配置和使用教程

    添加POM依赖: <!-- spring-boot-监控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.bo

  • SpringBoot四大神器之Actuator的使用小结

    序 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看.相关功能统计等. spring-boot-starter-actuator模块的实现对于实施微服务的中小团队来说,可以有效地减少监控系统在采集应用指标时的开发量.当然,它也并不是万能的,有时候我们也需要对其做一些简单的扩展来帮助我们实现自

  • SpringBoot 监控管理模块actuator没有权限的问题解决方法

    SpringBoot 1.5.9 版本加入actuator依赖后,访问/beans 等敏感的信息时候报错,如下 Tue Mar 07 21:18:57 GMT+08:00 2017 There was an unexpected error (type=Unauthorized, status=401). Full authentication is required to access this resource. 肯定是权限问题了.有两种方式: 1.关闭权限:application.prop

  • 详解配置spring-boot-actuator时候遇到的一些小问题

    前言 spring-boot-actuator是一个spring-boot提供的用于监控组件,只需要在代码中加入依赖就可以了 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 遇到的一些小问题 1.可以加入依赖 <dependenc

  • 详解spring-boot actuator(监控)配置和使用

    在生产环境中,需要实时或定期监控服务的可用性.spring-boot 的actuator(监控)功能提供了很多监控所需的接口.简单的配置和使用如下: 1.引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 如果使用http调用的

  • springboot 使用Spring Boot Actuator监控应用小结

    微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? 在这种框架下,微服务的监控显得尤为重要.本文主要结合Spring Boot Actuator,跟大家一起分享微服务Spring Boot Actuator的常见用法,方便我们在日常中对我们的微服务进行监控治理. Actuator监控 Spring Boot使用"习惯优于配置的理念",采用包

  • 详解关于springboot-actuator监控的401无权限访问

    今天心血来潮看一下spring监控 访问/beans 等敏感的信息时候报错 Tue Mar 07 21:18:57 GMT+08:00 2017 There was an unexpected error (type=Unauthorized, status=401). Full authentication is required to access this resource. application.properties添加配置参数 management.security.enabled=

  • Springboot actuator应用后台监控实现

    一 前言 springboot 额外的特色是提供了后台应用监控,可以通过 HTTP 或者 JMX的方式管理监控应用,本文主讲HTTP方式:其主要的功能是监控应用的健康状态,查看环境变量等: 二 pom.xml springboot 2.1.1,主要引入 actuator 依赖,web依赖用于测试: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-

  • springboot Actuator的指标监控可视化功能详解

    springboot为我们提供了丰富的指标监控功能SpringBoot Actuator SpringBoot Actuator是springboot为简化我们对微服务项目的监控功能抽取出来的模块,使得我们每个微服务快速引用即可获得生产界别的应用监控.审计等功能. 后序文章会更新使用 我们先来看看怎么可视化 我们可以通过github上的开源项目 这里 我们创建一个springboot项目 作为可视化的服务端 使用新功能首先都是引入依赖 需要web项目 <dependency> <grou

  • SpringBoot Admin 如何实现Actuator端点可视化监控

    目录 SpringBoot Admin 实现Actuator端点可视化监控 简介 Spring Boot Admin Server Spring Boot Admin Client 启动客户端, 在管理端进行可视化端点监控 Spring Boot 监控信息可视化 一.设置Spring Boot Admin Server 二.注册客户端 SpringBoot Admin 实现Actuator端点可视化监控 简介 Actuator可视化监控SpringBoot Admin Note: SpringB

  • SpringBoot Actuator埋点和监控及简单使用

    目录 1. 数据埋点 2. Micrometer 2.1 简单使用 2.2 命名规范 3. SpringBoot Actuator 3.1 添加依赖 3.2 基础配置 3.3 查看可消费的端点 3.4 获取应用的基本信息 3.5 健康指标 3.6 指标端点 metrics 4. 实例 4.1 测试接口 4.2 消费指标端点 5. SpringBoot Admin 5.1 Admin 服务器端 5.1.1 启用功能.添加依赖 5.1.2 选择一个端口 5.1.3 访问 5.2 Client 客户端

  • 如何基于springboot-admin实现后台监控

    一 前言 知识追寻者springboot系列文中又添加一文,springboot后台应用监控,希望广大读者支持,多多关注点赞:如果没有学习过actuator端点暴露文章,建议查询知识追寻者专栏进行学习: 二 springboot admin介绍 Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序:工作方式是 Spring Boot Admin Client向为Spring Boot Admin Server注册(通过HTTP)或使用SpringClo

  • Springboot actuator生产就绪功能实现解析

    Spring Boot包含许多附加功能,可帮助您在将应用程序投入生产时对其进行监视和管理.可以选择使用HTTP端点或JMX管理和监视您的应用程序.审核,运行状况和指标收集可以自动应用于您的应用程序. Springboot Actuator,它提供了很多生产级的特性,比如说监控和度量spring boot应用程序.Actuator的这些特性可以通过众多的REST断点,远程shell和JMX获得. 只有基于Spring MVC的应用程序才可以通过HTTP终端来监控应用程序的运行指标. 使用Sprin

  • Prometheus 入门教程之SpringBoot 实现自定义指标监控

    上篇文章我们已经可以在 Grafana 上看到对应的 SpringBoot 应用信息了,通过这些信息我们可以对 SpringBoot 应用有更全面的监控.但是如果我们需要对一些业务指标做监控,我们应该怎么做呢?这篇文章就带你一步步实现一个模拟的订单业务指标监控. 假设我们有一个订单系统,我们需要监控它的实时订单总额.10 分钟内的下单失败率.请求失败数.那么我们应该怎么做呢? 添加业务监控指标 在 spring-web-prometheus-demo 项目的基础上,我们添加一个 Promethe

  • SpringBoot actuator 健康检查不通过的解决方案

    SpringBoot actuator 健康检查不通过 今天遇到有个服务能够注册成功,但是健康检查不通过,通过浏览器访问健康检查的url,chrome的network一直显示pending,说明这个请求提交了,但是得不到返回,卡住了. 原来以为健康检查就是检查服务端口下的/health这个请求本身是否能正常返回,其实不是. 所谓健康检查是有很多检查项的,springboot中继承AbstractHealthIndicator的类,比如DataSourceHealthIndicator Redis

  • SpringBoot Actuator未授权访问漏洞修复详解

    目录 1.写在前面 2.问题描述 3.安全问题 4.禁止方法 5.完全禁用Actuator 1.写在前面 目前SpringBoot得框架,越来越广泛,大多数中小型企业,在开发新项目得时候.后端语言使用java得情况下,首选都会使用到SpringBoot. 在很多得一些开源得框架中,例如: ruoyi若以,这些. 不知道是出于什么原因?我们都会在这些框架中得pom文件中找到SpringBoot Actuator的依赖. 嘿,这Actuator估计很多人都没有真真实实使用过,但是就会出现在pom文件

  • springboot 在linux后台运行的方法

    首先需要进到自己springboot项目的根目录,然后执行如下linux命令 nohup java -jar 自己的springboot项目.jar >日志文件名.log 2>&1 & 命令详解: nohup:不挂断地运行命令,退出帐户之后继续运行相应的进程. >日志文件名.log:是nohup把command的输出重定向到当前目录的指定的"日志文件名.log"文件中,即输出内容不打印到屏幕上,而是输出到"日志文件名.log"文件中

随机推荐