22-springboot应用监控-actuator

发布时间 2023-04-03 14:47:42作者: companion

可以做成页面监控(springboot-admin),而不是json的格式,看起来会更方便。

在生产环境中,有时可能需要监控服务的可用性,spring-boot 的 actuator 就是提供了对应用的配置查看、健康检查、相关功能统计等,可以通过HTTP,JMX来访问这些监控功能;(端点)

如何使用该功能呢?

1、在项目的Maven中添加如下依赖:

<dependency>  

    <groupId>org.springframework.boot</groupId>  

    <artifactId>spring-boot-starter-actuator</artifactId>  

</dependency>  

2、application.properties 或 application.yml 配置文件中指定监控的HTTP端口及路径;

#内嵌服务器运行的端口

server.port=8080

server.servlet.context-path=/25-springboot

#actuator监控的端口(端口可配可不配,如果不配置,则使用和server.port相同的端口)

management.server.port=8800

#actuator监控的访问上下文根路径(路径可配可不配,如果不配置,则使用斜杆根路径)

management.server.servlet.context-path=/25-springboot

如果配置了management.开头的端口和访问根路径,实际上启动了两个tomcat;

#默认只开启了health和info,设置为*,则包含所有的web入口端点

management.endpoints.web.exposure.include=*

在浏览器访问举例:http://localhost:8080/25-springboot/actuator/health

actuator 提供的监控入口:

HTTP方法 路径 描述 是否为web入口

GET  /configprops 查看配置属性,包括默认配置 true

GET  /beans  查看bean及其关系列表 true

GET     /env  查看所有环境变量 true

GET     /mappings 查看所有url映射 true

GET     /httptrace  查看应用信息 false

GET  /health 查看应用健康指标 false

GET  /info 查看应用信息 false

GET  /metrics   查看应用基本指标 true

JMX /shutdown 关闭应用 true

完整端点列表:

https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/production-ready-features.html#production-ready-endpoints 

JMX协议访问的端点,使用jdk安装目录下的bin目录下的 jconsole.exe工具去访问;

其中:

1、/info 需要自己在application.properties配置文件中添加信息:

info.contact.email=bjpowernode@163.com

info.contact.phone=010-84846003

然后请求才会有数据;

2、/shutdown 需要在配置文件中开启才能生效:

management.endpoint.shutdown.enabled=true

 

 

Spring Boot Actuator 提供了对单个 Spring Boot 的监控,信息包含:应用状态、内存、线程、堆栈等等,比较全面的监控了 Spring Boot 应用的整个生命周期;

但是Spring Boot Actuator监控也有一些不足:

1、所有的监控都需要调用固定的接口来查看,如果全面查看应用状态需要调用很多接口,并且接口返回的 Json 信息不方便运营人员理解;

2、如果 Spring Boot 应用集群非常大,每个应用都需要调用不同的接口来查看监控信息,操作非常繁琐低效;

在这样的背景下,诞生了另外一个开源项目:Spring Boot Admin

参考文章: https://blog.csdn.net/javalingyu/article/details/124086259

Github:https://github.com/codecentric/spring-boot-admin 

底层需要Spring Boot Actuator的支持;

 

服务端(是一个springboot web项目):

1、加入依赖

<!-- spring-boot-admin-starter-server -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.6</version>
</dependency>

<!-- spring-boot-admin-server-ui -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>2.1.6</version>
</dependency>

(注意版本,有些版本下可能启动异常,admin版本2.1.6与boot版本一致)

2、入口类上添加注解

@EnableAdminServer
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

3、启动服务端,浏览器访问;

客户端(要被监控的springboot项目)

1、加入依赖

<!-- spring-boot-admin-starter-client -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.1.6</version>
</dependency>

 

2、配置文件

spring.application.name=Admin Client

#server端的ip和端口
spring.boot.admin.client.url=http://localhost:8080

#开启访问的端点

management.endpoints.web.exposure.include=*