SpringBoot 是为了简化 Spring 应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

actuatorspring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api 请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节…

Endpoints

actuator 的核心部分,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的 Endpoints(health、info、beans、httptrace、shutdown等等),同时也允许我们自己扩展自己的端点

Spring Boot 2.0 中的端点和之前的版本有较大不同,使用时需注意。另外端点的监控机制也有很大不同,启用了不代表可以直接访问,还需要将其暴露出来,传统的management.security管理已被标记为不推荐。

皮一下很开心

内置Endpoints

id desc Sensitive
auditevents 显示当前应用程序的审计事件信息 Yes
beans 显示应用Spring Beans的完整列表 Yes
caches 显示可用缓存信息 Yes
conditions 显示自动装配类的状态及及应用信息 Yes
configprops 显示所有 @ConfigurationProperties 列表 Yes
env 显示 ConfigurableEnvironment 中的属性 Yes
flyway 显示 Flyway 数据库迁移信息 Yes
health 显示应用的健康信息(未认证只显示status,认证显示全部信息详情) No
info 显示任意的应用信息(在资源文件写info.xxx即可) No
liquibase 展示Liquibase 数据库迁移 Yes
metrics 展示当前应用的 metrics 信息 Yes
mappings 显示所有 @RequestMapping 路径集列表 Yes
scheduledtasks 显示应用程序中的计划任务 Yes
sessions 允许从Spring会话支持的会话存储中检索和删除用户会话。 Yes
shutdown 允许应用以优雅的方式关闭(默认情况下不启用) Yes
threaddump 执行一个线程dump Yes
httptrace 显示HTTP跟踪信息(默认显示最后100个HTTP请求 - 响应交换) Yes

导入依赖

在 pom.xml 中添加 spring-boot-starter-actuator 的依赖

1
2
3
4
5
6
7
8
 <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>

注意事项

如果要访问info接口想获取maven中的属性内容请记得添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

属性配置

在 application.properties 文件中配置actuator的相关配置,其中info开头的属性,就是访问info端点中显示的相关内容,值得注意的是Spring Boot2.x中,默认只开放了info、health两个端点,剩余的需要自己通过配置management.endpoints.web.exposure.include属性来加载(有include自然就有exclude,不做详细概述了)。如果想单独操作某个端点可以使用management.endpoint.端点.enabled属性进行启用或禁用

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

简单测试

启动项目,访问 http://localhost:8080/actuator/info 看到如下内容代表配置成功

1
2
3
4
5
{
"blog-url": "http://blog.battcn.com",
"author": "Levin",
"version": "0.0.1-SNAPSHOT"
}

自定义 - 重点

上面讲了很多都是配置相关,以及自带的一些端点,在实际应用中有时候默认并不能满足我们的要求,比如Spring Boot默认的健康端点就很有可能不能满足

默认装配 HealthIndicators

下列是依赖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 服务器是否已启动。

健康端点(第一种方式)

实现HealthIndicator接口,根据自己的需要判断返回的状态是UP还是DOWN,功能简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.battcn.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component; /**
* <p>自定义健康端点</p>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Component("my1")
public class MyHealthIndicator 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 看到如下内容代表配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"status": "UP",
"details": {
"my1": {
"status": "UP",
"details": {
"code": 0,
"version": "v1.0.0"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 100944310272,
"free": 55071866880,
"threshold": 10485760
}
}
}
}

健康端点(第二种方式)

继承AbstractHealthIndicator抽象类,重写doHealthCheck方法,功能比第一种要强大一点点,默认的DataSourceHealthIndicator 、 RedisHealthIndicator 都是这种写法,内容回调中还做了异常的处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.battcn.health;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component; /**
* <p>自定义健康端点</p>
* <p>功能更加强大一点,DataSourceHealthIndicator / RedisHealthIndicator 都是这种写法</p>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Component("my2")
public class MyAbstractHealthIndicator 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 看到如下内容代表配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"status": "UP",
"details": {
"my2": {
"status": "UP",
"details": {
"code": 0,
"version": "v1.0.0"
}
},
"my1": {...},
"diskSpace": {...}
}
}

定义自己的端点

上面介绍的 infohealth 都是spring-boot-actuator内置的,真正要实现自己的端点还得通过@Endpoint、 @ReadOperation、@WriteOperation、@DeleteOperation

注解介绍

不同请求的操作,调用时缺少必需参数,或者使用无法转换为所需类型的参数,则不会调用操作方法,响应状态将为400(错误请求)

  • @Endpoint 构建 rest api 的唯一路径
  • @ReadOperation GET请求,响应状态为 200 如果没有返回值响应 404(资源未找到)
  • @WriteOperation POST请求,响应状态为 200 如果没有返回值响应 204(无响应内容)
  • @DeleteOperation DELETE请求,响应状态为 200 如果没有返回值响应 204(无响应内容)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.battcn.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import java.util.HashMap;
import java.util.Map; /**
* <p>@Endpoint 是构建 rest 的唯一路径 </p>
* 不同请求的操作,调用时缺少必需参数,或者使用无法转换为所需类型的参数,则不会调用操作方法,响应状态将为400(错误请求)
* <P>@ReadOperation = GET 响应状态为 200 如果没有返回值响应 404(资源未找到) </P>
* <P>@WriteOperation = POST 响应状态为 200 如果没有返回值响应 204(无响应内容) </P>
* <P>@DeleteOperation = DELETE 响应状态为 200 如果没有返回值响应 204(无响应内容) </P>
*
* @author Levin
* @since 2018/5/24 0024
*/
@Endpoint(id = "battcn")
public class MyEndPoint { @ReadOperation
public Map<String, String> hello() {
Map<String, String> result = new HashMap<>();
result.put("author", "Levin");
result.put("age", "24");
result.put("email", "1837307557@qq.com");
return result;
}
}

以为这就大功告成了吗,现实告诉我的是spring-boot默认是不认识这玩意的,得申明成一个Bean(请看 主函数

皮一下很开心

主函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.battcn;

import com.battcn.endpoint.MyEndPoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author Levin
*/
@SpringBootApplication
public class Chapter13Application { public static void main(String[] args) {
SpringApplication.run(Chapter13Application.class, args);
} @Configuration
static class MyEndpointConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public MyEndPoint myEndPoint() {
return new MyEndPoint();
}
}
}

测试

完成准备事项后,启动Chapter13Application 访问 http://localhost:8080/actuator/battcn 看到如下内容代表配置成功…

1
2
3
4
5
{
"author": "Levin",
"age": "24",
"email": "1837307557@qq.com"
}

总结

参考文档:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready

强大的 actuator 服务监控与管理的更多相关文章

  1. Spring Boot (27) actuator服务监控与管理

    actuaotr是spring boot项目中非常强大的一个功能,有助于对应用程序进行监控和管理,通过restful api请求来监管.审计.收集应用的运行情况,针对微服务而言它是必不可少的一个环节. ...

  2. spring boot 2.x 系列 —— actuator 服务监控与管理

    文章目录 一.概念综述 1.1 端点 1.2 启用端点 1.3 暴露端点 1.4 健康检查信息 二.项目说明 1.1 项目结构说明 1.2 主要依赖 1.3 项目配置 1.4 查看监控状态 三.自定义 ...

  3. spring boot actuator服务监控与管理

    1.引入actuator所需要的jar包 <dependency> <groupId>org.springframework.boot</groupId> < ...

  4. Springboot监控之一:SpringBoot四大神器之Actuator之3-springBoot的监控和管理--指标说明

    Spring Boot包含很多其他的特性,它们可以帮你监控和管理发布到生产环境的应用.你可以选择使用HTTP端点,JMX或远程shell(SSH或Telnet)来管理和监控应用.审计(Auditing ...

  5. 监控与管理dubbo服务

    Dubbo是阿里多年前开源的一套服务治理框架,在众多互联网企业里应用广泛.本文介绍了一些如何监控与管理dubbo服务.使用的工具与<dubbox 的各种管理和监管>大致相同,本文更侧重于命 ...

  6. Spring Cloud系列(三) 应用监控与管理Actuator

    Spring Cloud系列(二) 应用监控与管理Actuator 前言:要想使用Spring Cloud ,Spring Boot 提供的spring-boot-starter-actuator模块 ...

  7. SpringCloud微服务实战——搭建企业级开发框架(四十四):【微服务监控告警实现方式一】使用Actuator + Spring Boot Admin实现简单的微服务监控告警系统

      业务系统正常运行的稳定性十分重要,作为SpringBoot的四大核心之一,Actuator让你时刻探知SpringBoot服务运行状态信息,是保障系统正常运行必不可少的组件.   spring-b ...

  8. SpringCloud微服务实战——搭建企业级开发框架(四十五):【微服务监控告警实现方式二】使用Actuator(Micrometer)+Prometheus+Grafana实现完整的微服务监控

      无论是使用SpringBootAdmin还是使用Prometheus+Grafana都离不开SpringBoot提供的核心组件Actuator.提到Actuator,又不得不提Micrometer ...

  9. 笔记:Spring Boot 监控与管理

    在微服务架构中,我们将原本庞大的单体系统拆分为多个提供不同服务的应用,虽然,各个应用的内部逻辑因分解而简化,但由于部署的应用数量成倍增长,使得系统的维护复杂度大大提升,为了让运维系统能够获取各个为服务 ...

随机推荐

  1. Java实现 蓝桥杯VIP 算法提高 计算器

    算法提高 计算器 时间限制:1.0s 内存限制:256.0MB [问题描述] 王小二的计算器上面的LED显示屏坏掉了,于是他找到了在计算器维修与应用系学习的你来为他修计算器. 屏幕上可以显示0~9的数 ...

  2. Java实现 蓝桥杯VIP 算法提高 促销购物

    算法提高 促销购物 时间限制:1.0s 内存限制:256.0MB 问题描述 张超来到了超市购物. 每个物品都有价格,正好赶上商店推出促销方案.就是把许多东西一起买更便宜(保证优惠方案一定比原价便宜). ...

  3. java实现机器人行走

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

  4. 使用dotnet Cli向nuget发布包

    长话短说, 今天分享如何在nuget.org创建并发布.NET Standard package. 前置 安装勾选.NET Core开发套件的Visual Studio; 安装dotnet Cli 从 ...

  5. vue+jquery使用FormData向后端传递数据和文件,express如何获取

    使用multiparty 模块 下载 cnpm install multiparty --save 前端代码: <template> <div class="add-are ...

  6. cuda基础

    一:cuda编程模型 1:主机与设备 主机---CPU 设备/处理器---GPU CUDA编程模型如下: GPU多层存储空间结构如图: 2:Kernel函数的定义与调用 A:运行在GPU上,必须通过_ ...

  7. Java虚拟机性能调优(一)

    Java虚拟机监控与调优,借助Java自带分析工具. jps:JVM Process Status Tool,显示指定系统内所有的HotSpot虚拟机进程 jstat:JVM Statistics M ...

  8. 2、react-生命周期1※※※

    生命周期: 一个人的生命周期:从出生到去世 出生得那一刻就是当前这一个人特性固定下来得那一刻:实例化期 出生了之后生长知道死的那一刻:生存期 去世了:销毁期 所以对于一个组件来说它的生命周期是三个时期 ...

  9. [转] Linux下用文件IO的方式操作GPIO(/sys/class/gpio)

    点击阅读原文 一.概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户 ...

  10. 面试官:线程池如何按照core、max、queue的执行循序去执行?(内附详细解析)

    前言 这是一个真实的面试题. 前几天一个朋友在群里分享了他刚刚面试候选者时问的问题:"线程池如何按照core.max.queue的执行循序去执行?". 我们都知道线程池中代码执行顺 ...