# Spring Boot 應用監控
Spring Boot通過**Actuator**提供了應用監控和管理功能,有以下幾種方式:
- http
- JMX
- SSH
## 監控和管理終端
| 端點名稱 | 描述 | 敏感 |
| --- | --- | --- |
| actuator | 列出所有端點列表 | 是 |
| auditevents | 暴露出當前應用程序的審核事件的信息 | 是 |
| autoconfig | 當前應用的所有自動配置 | 是 |
| beans | 當前應用中所有的Bean信息 | 是 |
| configprops | 當前應用中所有配置屬性 | 是 |
| dump | 顯示當前應用線程狀態信息 | 是 |
| env | 顯示當前應用當前環境信息 | 是 |
| flyway | 顯示已應用于任何flyway數據庫遷移。 | 是 |
| health | 顯示當前應用健康狀態 | **非** |
| info | 顯示當前應用信息 | **非** |
| loggers | 顯示和修改應用程序記錄器的配置 | 是 |
| liquibase | 顯示已應用于任何Liquibase數據庫遷移 | 是 |
| metrics | 顯示當前應用的各項指標信息 | 是 |
| mappings | 顯示所有的@RequestMapping映射路徑 | 是 |
| shutdown | 關閉當前應用(默認關閉,該功能只能是POST請求) | 是 |
| trace | 顯示追蹤信息(默認最后的100個http請求) | 是 |
### 如果使用的是SpringMVC,那么還可以擴展下面這幾個終端:
| 端點名稱 | 描述 | 敏感 |
| --- | --- | --- |
| docs | 顯示文檔,包括例如請求和響應,執行器的端點。要求`spring-boot-actuator-docs`要在類路徑中。 | **非** |
| heapdump | 返回gzip壓縮hprof堆轉儲文件。 | 是 |
| jolokia | 公開JMX Beans 通過HTTP(當Jolokia是在類路徑) | 是 |
| logfile | 返回日志文件的內容(如logging.file或logging.path屬性已設置)。支持使用的HTTP Range報頭獲取日志文件的內容的一部分。 | 是 |
### 添加依賴
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
```
#### 第一種:通過Spring Security來管理(安全性高)
由于監控信息大多數很敏感,spring官方建議安裝Spring Security來管理,則還要加入一個依賴:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
```
配置文件:
```
#端點的配置
endpoints.sensitive=true
endpoints.shutdown.enabled=true
#保護端點
security.basic.enabled=true,
security.user.name=liuyin
security.user.password=liuyin123
management.security.roles=SUPERUSER
#自定義路徑
security.basic.path=/system
management.context-path=/system
```
訪問路徑:
#### 第二種:只通過配置文件直接管理(安全性低)
所有的終端功能,除了shutdown以外,都是默認打開的,在沒有Spring Security的時候,必須要將敏感信息配置成非敏感才能訪問
```
#關閉敏感屬性
endpoints.dump.sensitive=false
endpoints.auditevents.sensitive=false
endpoints.autoconfig.sensitive=false
endpoints.beans.sensitive=false
endpoints.env.sensitive=false
endpoints.flyway.sensitive=fase
endpoints.loggers.sensitive=fasle
endpoints.liquibase.sensitive=false
endpoints.configprops.sensitive=false
endpoints.metrics.sensitive=false
endpoints.mappings.sensitive=false
endpoints.shutdown.sensitive=false
endpoints.trace.sensitive=false
endpoints.heapdump.sensitive=false
endpoints.jolokia.sensitive=false
endpoints.logfile.sensitive=false
```
默認訪問路徑為:
* metrics: http://localhost:8080/metrics
* trace: http://localhost:8080/trace
* ...以此類推
### http訪問例子
- 系統信息:http://127.0.0.1:8088/metrics
```javascript
{
"mem": 593145,
"mem.free": 303292,
"processors": 4,
"instance.uptime": 4449,
"uptime": 1895126,
"systemload.average": -1.0,
"heap.committed": 516608,
"heap.init": 131072,
"heap.used": 213315,
"heap": 1837056,
"nonheap.committed": 77968,
"nonheap.init": 2496,
"nonheap.used": 76538,
"nonheap": 0,
"threads.peak": 24,
"threads.daemon": 19,
"threads.totalStarted": 64,
"threads": 21,
"classes": 9589,
"classes.loaded": 9591,
"classes.unloaded": 2,
"gc.ps_scavenge.count": 10,
"gc.ps_scavenge.time": 343,
"gc.ps_marksweep.count": 3,
"gc.ps_marksweep.time": 481,
"httpsessions.max": -1,
"httpsessions.active": 0,
"datasource.primary.active": 0,
"datasource.primary.usage": 0.0
}
```
- mvc映射:http://127.0.0.1:8088/mappings
```javascript
{
"{[/test/getmap],methods=[GET]}": {
"bean": "requestMappingHandlerMapping",
"method": "public java.util.Map<java.lang.Long, java.lang.Object> com.luyou.controller.TestController.getMap()"
},
"{[/user/all],methods=[POST],produces=[application/json;charset=utf-8]}": {
"bean": "requestMappingHandlerMapping",
"method": "public java.util.List<com.luyou.domain.User> com.luyou.controller.UserController.all()"
},
"{[/user/update],methods=[POST],produces=[application/json;charset=utf-8]}": {
"bean": "requestMappingHandlerMapping",
"method": "public int com.luyou.controller.UserController.update(com.luyou.domain.User)"
},
"{[/user/save],methods=[POST],produces=[application/json;charset=utf-8]}": {
"bean": "requestMappingHandlerMapping",
"method": "public long com.luyou.controller.UserController.save(com.luyou.domain.User)"
},
"{[/user/{id}/get],methods=[POST],produces=[application/json;charset=utf-8]}": {
"bean": "requestMappingHandlerMapping",
"method": "public com.luyou.domain.User com.luyou.controller.UserController.getById(long)"
},
"{[/user/{id}/delete],methods=[POST],produces=[application/json;charset=utf-8]}": {
"bean": "requestMappingHandlerMapping",
"method": "public boolean com.luyou.controller.UserController.deleteById(long)"
},
"{[/error]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)"
},
"{[/error],produces=[text/html]}": {
"bean": "requestMappingHandlerMapping",
"method": "public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)"
}
省略......
}
```
## JMX進行管理與監控
通過Spring將Bean暴露成Mbean,我們也可做到對系統的監控。
運行Java內置的jconsole,選定啟動的應用,切換到Mbean,可以看到Spring已經將終端暴露,在這里也可以進行相應的讀取與修改。

## SSH
因為Spring Boot官方說明,遠程監控管理可能在Spring Boot2.0后默認取消,所有這里不再提供例子。