# 性能监控

***

## Actuator监控

### Actuator 简介

在生产环境中，往往需要对系统实际运行的情况（例如 cpu、io、disk、db、业务功能等指标）进行监控运维。在 SpringBoot 项目中 Actuator 模块提供了众多 HTTP 接口端点（Endpoint），来提供应用程序运行时的内部状态信息。

Actuator 模块提供了一个监控和管理生产环境的模块，可以使用 http、jmx、ssh、telnet 等来管理和监控应用。包括应用的审计（Auditing）、健康（health）状态信息、数据采集（metrics gathering）统计等监控运维的功能。同时，提供了可以扩展 Actuator 端点（Endpoint）自定义监控指标。这些指标都是以 JSON 接口数据的方式呈现。

需要注意的是，SpringBoot 1.x 和 2.x 的 Actuator 监控设置差别很大，不仅提供的 endpoint 路径不一樣，连 application.properties 的配置也不一样

***

### 使用 Spring Boot Actuator

如果要使用 Spring Boot Actuator 提供的监控功能，需要先加入相关的 maven dependency依赖

```xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

<!-- WEB 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
```

只要加上了这个 maven dependency，SpringBoot在运行时就会自动开启/actuator/health和/actuator/info这两个 endpoint，然后就可以通过这两个 endpoint查看当前 SpringBoot应用程序的运行情况，例如自动化配置信息、创建的 Spring beans 以及一些环境属性等。

为了保证 actuator 暴露的监控接口的安全性，需要添加安全控制的依赖 spring-boot-start-security 依赖，访问应用监控端点时，都需要输入验证信息。

```xml
<dependency>
    <!--Security 依赖 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
```

如果添加了security安全管理，只需要在配置文件配置用户名和密码就可以了

```yaml
spring:
  security:
    user:
      name: admin
      password: 123456
```

***

### 开启 Actuator 所有 endpoints

* <https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html>

application.properties

```conf
management.endpoints.web.exposure.include=*
```

***

### 配置 jmx 形式的监控

```conf
management.endpoints.jmx.exposure.include=*
```

***

## Prometheus

**添加依赖**

```xml
<dependency>
	<groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
```

打开 Prometheus 监控接口 application.properties

```conf
server.port=8088
spring.application.name=springboot2-prometheus
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
```

访问 /actuator/prometheus

***

## Source & Reference

* <https://blog.51cto.com/u\\_15287666/3109393>
* <https://www.mobaijun.com/posts/283491354.html>
* <https://blog.csdn.net/shuofxz/article/details/118213813>
* <https://juejin.cn/post/6844904052417904653#heading-29>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bergins-organization.gitbook.io/wang-an/develop/java-xue-xi/xing-neng-jian-kong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
