> For the complete documentation index, see [llms.txt](https://spring-boot.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://spring-boot.shujuwajue.com/sheng-hua/5sheng-chan-zhun-5907-ji-yu-http-de-jian-kong.md).

# 5.生产准备-基于HTTP的监控

## 一、利用Spring Boot的特性进行监控你的应用

spring boot 提供了actuator功能，通过通过HTTP（最简单方便）、JMX、远程shell，来查看spring boot应用的配置，各种指标、健康等等，它能查看 监控以下信息：

* Spring Boot的配置信息
* Spring Boot配置的Bean信息
* 最近请求 HTTP信息
* 数据源，NOSQL等数据状态，
* 在线查看日志内容，在线日志配置修改
* 所有`@RequestMapping`注解的URL路径
* 自动装配信息汇总
* 打印虚拟机的线程栈
* Dump 内存
* 应用的各种指标汇总
* 自定义监控指标

## 二、添加actuator依赖

pom.xml中增加如下依赖：

其中security依赖是为了密码管理

```markup
<!-- actuator -->
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- security -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
```

## 三、端点（通过执行器端点可以监控应用及与应用进行交互）

1.端点暴露的方式取决于你采用的监控方式。如果使用HTTP监控，端点的ID映射到一个URL。例如，默认情况下，health端点将被映射到/health。\
2.端点会默认有敏感度，根据不同的敏感度是否需要提供用户密码认证(endpoints.sensitive)\
3.如果没启用web安全，则敏感度高的会禁用\
4.可以通过配置文件进行配置敏感度\
5.默认情况下，除了shutdown外的所有端点都是启用的。

## 四、配置

```
#端点的配置
endpoints.sensitive=true
endpoints.shutdown.enabled=true

#保护端点
security.basic.enabled=true
security.user.name=admin
security.user.password=admin
management.security.roles=SUPERUSER

#自定义路径
security.basic.path=/manage
management.context-path=/manage
```

**其他配置**

spring boot 2 默认并不启用所有 监控，需要哦如下配置

```
endpoints.default.web.enabled=true
```

考虑到系统监控涉及系统安全，最好修改配置文件，设置系统监控的访问接口

```
management.server.port=8081
management.endpoints.web.base-path=manage
```

然后可以将8081置于防火墙后，保证外部用户u能访问。

## 五、备注

### **health 健康信息**

[http://localhost:8080/manage/health](http://localhost:8080/manage/metrics)

health 信息默认输出了磁盘空间的健康诊断信息。

### **trace 跟踪信息**

追踪：<http://localhost:8080/manage/trace>

能查看最近HTTP请求 响应。

trace 是通过`InMemoryTraceRepository/TraceRepository`类来实现的，默认保留最后100条访问数据，可以自己配置InMemoryTraceRepository或者实现TraceRepository接口，如下代码保留2条数据

```java
```

### **metrics 显示了应用当前的指标信息**

度量：<http://localhost:8080/manage/metrics>

* 系统内存总量（mem），单位:Kb
* 空闲内存数量（mem.free），单位:Kb
* 处理器数量（processors）
* 系统正常运行时间（uptime），单位:毫秒
* 应用上下文（就是一个应用实例）正常运行时间（instance.uptime），单位:毫秒
* 系统平均负载（systemload.average）
* 堆信息（heap，heap.committed，heap.init，heap.used），单位:Kb
* 线程信息（threads，thread.peak，thead.daemon）
* 类加载信息（classes，classes.loaded，classes.unloaded）
* 垃圾收集信息（gc.xxx.count, gc.xxx.time）
* 最大连接数（datasource.xxx.max）
* 最小连接数（datasource.xxx.min）
* 活动连接数（datasource.xxx.active）
* 连接池的使用情况（datasource.xxx.usage）

也可以使用 `/metrics/{name:.*}` 访问单个属性。

### loggers 日志查看

actuator\
允许查看日志配置，还允许修改值等级配置，actuator\
也可以在线查看日志内容。

<http://localhost:8080/manage/loggers>

上面的链接只是查看日志的配置

configuredLevel是指定配置的日志等级，默认 日志等级是INFO

* TRACE
* DEBUG
* INFO
* WARN
* ERROR
* FATAL
* OFF
* null

`null` indicates that there is no explicit configuration.

可以一觉POST片段来改变日志等级

可以通过<http://localhost:8080/manage/logfile> 来查看日志，可以通过如下来配置日志文件

```
logging.file = my.log
```

### dump 线程栈信息

<http://localhost:8080/manage/dump>

可以通过dump来获取某一时刻虚拟机线程信息，该信息类似使用JDK自带的jstack命令的输出结果或者kill -3 的结果。线程栈表示某一时刻虚拟机正在 的事情。

Spring boot的应用较为复杂，从庞大的代码库中找到难题那个不是简单的事情，查看线程栈是个很好的办法，通过观察dump打印出来的线程栈，还能轻易发现应用死锁、系统莫名忙碌等系统故障的原因。

### mappings 查看url映射

<http://localhost:8080/manage/mappings>

可以查看所有通过注释`@RequestMapping`设置的URL映射，可以通过此来查看URL对应的Controller

### beans 查看Spring容器管理的Bean

<http://localhost:8080/manage/beans>

## 资料

[spring boot监控--actuator](https://www.cnblogs.com/myf008/p/7374869.html)

[springboot(十九)：使用Spring Boot Actuator监控应用](https://blog.csdn.net/ityouknow/article/details/79273461)

[springboot(二十)：使用spring-boot-admin对spring-boot服务进行监控](https://blog.csdn.net/ityouknow/article/details/79308237)
