zl程序教程

您现在的位置是:首页 >  后端

当前栏目

微服务技术系列教程(09) - SpringBoot - 监控管理 - Actuator

2023-09-11 14:15:40 时间

代码已添加至Github,有兴趣的同学可以下载来看看:https://github.com/ylw-github/SpringBoot-Monitor-Demo

1. Actuator监控应用

Actuator 是spring boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。可以使用HTTP的各种请求来监管、审计、收集应用的运行情况,特别对于微服务管理十分有意义。

但是Actuator也有缺点:没有可视化界面。

下面我们来看看如何使用Actuator:

步骤一:添加maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

<dependencies>

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

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

</dependencies>

2.在/src/main/resources/目录下新增application.yml

###通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的;
management:
  endpoints:
    web:
      exposure:
        include: "*"
spring:
  profiles:
    active: prod

3.运行程序,通过actuator/+端点名就可以获取相应的信息
例如访问http://localhost:8080/actuator/env,可以看到陈列了所有的环境变量:
在这里插入图片描述

2. Actuator端点

从上面的讲解,我们知道可以通过actuator/+端点名就可以获取相应的信息。

路径作用
/actuator/beans显示应用程序中所有Spring bean的完整列表。
/actuator/configprops显示所有配置信息。
/actuator/env陈列所有的环境变量。
/actuator/mappings显示所有@RequestMapping的url整理列表。
/actuator/health显示应用程序运行状况信息 up表示成功 down失败
/actuator/info查看应用信息

比如现在想查看应用的信息,可以在application.yml新增info,如下:
在这里插入图片描述
运行http://localhost:8080/actuator/info
在这里插入图片描述

总结

在这里插入图片描述