actuator是spring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api 请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节,本文只介绍如何集成actuator,及actuator最最简单的使用,对于自定义endPoint及actuator的实现原理后续单独介绍。

  1、导入依赖包

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

  如果要访问info接口访问maven中配置的内容,需要配置如下插件

            <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>

  2、增加相关配置

# 描述信息
info:
blog-url: http://lcl.com
author: lcl
version: @project.version@ # 加载所有的端点/默认只加载了 info / health
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always # 可以关闭制定的端点
shutdown:
enabled: false # 路径映射,将 health 路径映射成 rest_health 那么在访问 health 路径将为404,因为原路径已经变成 rest_health 了,一般情况下不建议使用
# management.endpoints.web.path-mapping.health=rest_health

  此处注意一点,如果使用的是properties配置文件,使用星号时,不需要加单引号,但是使用yml配置文件时,必须要加单引号,否则启动报错。

  3、测试

  输入http://localhost:8080/actuator/info进行测试

  4、自定义检测

  虽然actuator拥有强大的功能,但是仍可能不满足我们的需求,就需要自定义端点进行测试,自定义端点测试主要有两种实现方式,实现HealthIndicator接口或继承

AbstractHealthIndicator类,话不多说,直接上代码。

(1)实现HealthIndicator接口:
package com.example.demo.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component; @Component("myhealth1")
public class MyHealth implements HealthIndicator { private static final String VERSION = "v1.0.0"; @Override
public Health health() {
int code = check();
if(code != 0){
Health.down().withDetail("code",code).withDetail("version", VERSION).build();
}
return Health.up().withDetail("code",code).withDetail("version", VERSION).up().build();
} private int check() {
return 0;
}
}

  测试:http://localhost:8080/actuator/health

  (2)继承AbstractHealthIndicator类

package com.example.demo.health;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component; @Component("myhealth2")
public class MyHealth2 extends AbstractHealthIndicator { private static final String VERSION = "v1.0.0"; @Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
int code = check();
if (code != 0) {
builder.down().withDetail("code", code).withDetail("version", VERSION).build();
}
builder.withDetail("code", code)
.withDetail("version", VERSION).up().build();
} private int check() {
return 0;
}
}

  测试:http://localhost:8080/actuator/health

  由上可以查看系统集成了redis、mysql和rabbitMQ,其中redis和mysql连接正常,rabbitMQ连接异常。

  下列是依赖spring-boot-xxx-starter后相关HealthIndicator的实现(通过management.health.defaults.enabled 属性可以禁用它们),但想要获取一些额外的信息时,自定义的作用就体现出来了…

名称 描述
CassandraHealthIndicator 检查 Cassandra 数据库是否启动。
DiskSpaceHealthIndicator 检查磁盘空间不足。
DataSourceHealthIndicator 检查是否可以获得连接 DataSource
ElasticsearchHealthIndicator 检查 Elasticsearch 集群是否启动。
InfluxDbHealthIndicator 检查 InfluxDB 服务器是否启动。
JmsHealthIndicator 检查 JMS 代理是否启动。
MailHealthIndicator 检查邮件服务器是否启动。
MongoHealthIndicator 检查 Mongo 数据库是否启动。
Neo4jHealthIndicator 检查 Neo4j 服务器是否启动。
RabbitHealthIndicator 检查 Rabbit 服务器是否启动。
RedisHealthIndicator 检查 Redis 服务器是否启动。
SolrHealthIndicator 检查 Solr 服务器是否已启动。

SpringBoot--集成actuator的更多相关文章

  1. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  2. SpringBoot集成actuator模块的基本使用

    © 版权声明:本文为博主原创文章,转载请注明出处 1. 版本 SpringBoot:2.0.0.RELEASE 2. 集成 SpringBoot集成actuator模块非常简单,只需要引入actuat ...

  3. SpringBoot集成Actuator监控管理

    1.说明 本文详细介绍Spring Boot集成Actuator监控管理的方法, 基于已经创建好的Spring Boot工程, 然后引入Actuator依赖, 介绍监控管理相关功能的使用. Sprin ...

  4. SpringBoot集成Actuator端点配置

    1.说明 Actuator端点可以监控应用程序并与之交互. Spring Boot包括许多内置的端点, 比如health端点提供基本的应用程序运行状况信息, 并允许添加自定义端点. 可以控制每个单独的 ...

  5. SpringBoot集成Actuator健康指示器health

    1.说明 本文详细介绍Actuator提供的HealthIndicators, 即健康指示器的配置使用, 利用自动配置的健康指标, 检查正在运行的应用程序的状态, 以及自定义健康指标的方法. 监控软件 ...

  6. SpringBoot系列: Actuator监控

    Sprng Boot 2 actuator变动加大, 网上很多资料都都已经过期. ============================配置项============================ ...

  7. SpringBoot之actuator

    在springBoot中集成actuator可以很方便的管理和监控应用的状态. 暴露的Restful接口有: HTTP方法 路径 描述 鉴权 GET /autoconfig 查看自动配置的使用情况 t ...

  8. 0120 springboot集成Mybatis和代码生成器

    在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...

  9. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  10. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

随机推荐

  1. Rocket - tilelink - Xbar

    https://mp.weixin.qq.com/s/UXFHYEQaYotWNEhshro68Q   简单介绍Xbar的实现.   ​​   1. 基本介绍   用于为Xbar的输入和输出连接生成内 ...

  2. python常见面试题讲解(十三)字串的连接最长路径查找

    输入描述: 输入第一行为一个正整数n(1≤n≤1000),下面n行为n个字符串(字符串长度≤100),字符串中只含有大小写字母. 输出描述: 数据输出n行,输出结果为按照字典序排列的字符串. 示例1 ...

  3. java实现复制网站内容

    复制网站内容 复制代码 本程序将网站"www.baidu.com"首页的内容复制保存在文件test.html中.写了如下代码,请完善之: import java.net.*; im ...

  4. java实现第三届蓝桥杯机器人行走

    机器人行走 [编程题](满分18分) 某少年宫引进了一批机器人小车.可以接受预先输入的指令,按指令行动.小车的基本动作很简单,只有3种:左转(记为L),右转(记为R),向前走若干厘米(直接记数字). ...

  5. MMDVM中继板测试软件MMDVMCal

    运行方法: 只支持windows 64位系统 32位下载:https://share.weiyun.com/52uHAO5 64位下载:https://share.weiyun.com/5IgdqvL ...

  6. Markdown编辑器说明

    标题: Markdown编辑器说明 作者: 梦幻之心星 347369787@QQ.com 标签: [Markdown, 编辑器] 目录: [软件] 日期: 2020-6-4 前提说明 在使用Markd ...

  7. Python 在线免费批量美颜,妈妈再也不用担心我 P 图两小时啦

    引言 首先我承认自己标题党了,我就想提升点阅读量我容易么我,前几天的篇纯技术文阅读量都扯着蛋了. 毕竟阅读量太低实在是没有写下去的动力,我只能用点小手段偶尔提升下阅读量. 这篇文章我转换下套路,先放结 ...

  8. 玩转华为物联网IoTDA服务系列三-自动售货机销售分析场景示例

    场景简介 通过收集自动售货机系统的销售数据,EI数据分析售货销量状况. 该场景主要描述的是设备可以通过MQTT协议与物联网平台进行交互,应用侧可以到物联网平台订阅设备侧变化的通知,用户可以在控制台或通 ...

  9. ubuntu12.04 dnw2 fl2440 配置

    1.安装libusb-dev sudo apt-get install libusb-dev 2.dnw2编译配置 源码如下,将其保存为dnw2.c 编译命令 gcc dnw2.c -o dnw2 - ...

  10. Unit3-窝窝社交圈

    全文共4909字,推荐阅读时间15~20分钟. 文章共分五个部分: JML总结 作业分析 评测相关 重构策略 课程体验感受 JML总结 定义 JML是一种对Java程序进行规格化设计的表示语言 JML ...