企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
# 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已经将终端暴露,在这里也可以进行相应的读取与修改。 ![image](http://on3eg24oc.bkt.clouddn.com/Jconsole%E7%9B%91%E6%8E%A7SpringBoot.jpg) ## SSH 因为Spring Boot官方说明,远程监控管理可能在Spring Boot2.0后默认取消,所有这里不再提供例子。