添加依赖,开启监控功能
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
在properties文件中开启完整健康信息和埋点(由于安全等因素,默认只显示不完整的health信息)
#开启完整健康信息 management.endpoint.health.show-details=always #将所有endpoint暴露 management.endpoints.web.exposure.include=*
启动SpringBoot项目,启动时注意查看启动日志,会发现有这行:
2022-01-22 16:36:12.697 INFO 11064 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 13 endpoint(s) beneath base path '/actuator'
因此访问的路径为IP:port/actuator,访问后得到一串json,可以在json.cn中解析出来查看:
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-path":{"href":"http://localhost:8080/actuator/health/{*path}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},"configprops-prefix":{"href":"http://localhost:8080/actuator/configprops/{prefix}","templated":true},"env":{"href":"http://localhost:8080/actuator/env","templated":false},"env-toMatch":{"href":"http://localhost:8080/actuator/env/{toMatch}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},"metrics-requiredMetricName":{"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}","templated":true},"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}
beans可以查看创建的所有Bean
//这是我之前重写的初始化类
"myApplicationContextInitializer":{ "aliases":[], "scope":"singleton", "type":"com.yz.sprintbootmyun.listener.MyApplicationContextInitializer", "resource":"file [E:\\Work\\Workspace\\sprintboot-my-un\\target\\classes\\com\\yz\\sprintbootmyun\\listener\\MyApplicationContextInitializer.class]", "dependencies":[] },
mappings可查查看当前所有url路径
"handler":"com.yz.sprintbootmyun.UserController#findAll()", "predicate":"{ [/user/findAll]}", "details":{ "handlerMethod":{ "className":"com.yz.sprintbootmyun.UserController", "name":"findAll", "descriptor":"()Ljava/lang/String;" },
这些都是json串,看起来尤其不直观,如果想图形化更直观地查看,可以使用IDEA自带的插件,可以查看常用的几个功能
如果这还不能狗满足监控使用,那么就可以用SpringBoot-Admin了,admin由一个server和一个或若干个client组成
在client中,需要在配置文件中指定server的路径,以及actuator开启的功能
spring.boot.admin.client.url=http://localhost:9000 management.endpoint.health.show-details=always management.endpoints.web.exposure.include=*
在server中,只需在主类上添加@EnableAdminServer注解即可。如果在同一机器上启动server或client,注意端口不要重复
访问指定的网址:
v