监控
SpringBoot自带监控功能插件Actuator,可以帮助实现对程序内部运行情况的监控,如监控状态、Bean加载情况、配置属性和日志信息等。
使用步骤:
导入依赖坐标(在搭建项目时,可以直接勾选
Ops中的Spring Boot Actuator)xml<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>访问
http://localhost:8080/acruator基本使用:可以查看到健康相关(包括引入的第三方包也能健康检查)(
{"status": "up"}表示程序运行状态健康)和信息info相关的功能(查看配置文件中以info开头的配置信息)如果要开启健康检查的完整信息,我们需要在项目的配置文件中进行配置:
properties# info相关的信息 info.name=jlc info.age=25 # 开启健康检查的完整信息 management.endpoint.health.show-details=always
高级使用需要我们进行配置的开启,才能使用
# 将所有的监控endpoint暴露出来
management.endpoint.web.exposure.include=*开启所有的功能后,我们可以看到Bean加载情况、配置文件的属性信息和当前所有的url路径信息等
图形化方式监控
开源项目Spring Boot Admin提供了图形化的监控方式
Spring Boot Admin有两个角色:客户端和服务端
- 应用程序作为
Spring Boot Admin Client向Spring Boot Admin Server注册 Spring Boot Admin Server的UI界面展示Spring Boot Admin Client的Actuator Endpoint上的一些监控信息
使用步骤:
admin-server创建
admin-server模块导入依赖坐标
admin-starter-server(直接在创建SpringBoot项目时勾选Web中的Spring Web和Ops中的Spring Boot Admin (Server))在引导类上启用监控功能
@EnableAdminServerjavapackage com.jlc.springbootadminserver; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.SpringApplication; @SpringBootApplication @EnableAdminServer // 启动监控功能 public class SpringbootAdminserverApplication { public static void main(String[] args) { SpringApplication.run(SpringbootInitApplication.class, args); } }
admin-client创建
admin-client模块导入依赖坐标
admin-starter-client(直接在创建SpringBoot项目时勾选Web中的Spring Web和Ops中的Spring Boot Admin (Client))配置相关信息:
server地址等在
application.properties中进行配置:properties# 执行admin.server的地址 spring.boot.admin.client.url=http://localhost:9000 # 具体的端口地址具体声明 # 开启健康检查的完整信息 management.endpoint.health.show-details=always # 将所有的监控endpoint暴露出来 management.endpoint.web.exposure.include=*启动
server和client服务,访问server,访问http://localhost:9000