Spring Boot应用监控
Spring Boot提供了运行时的应用监控和管理功能,我们可以通过HTTP,JMX,SSH协议进行操作。审计,健康以及指标信息将会自动得到。
1.通过使用http实现对应用的监控和管理
1.1演示监控端点
因为是通过http进行监控管理,所以必然需要web的依赖,所以新建的spring boot项目依赖要选择Actuator,Web,HATEOAS
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
直接启动项目,可以看到输出信息:
2020-06-05 22:19:32.461 INFO 14092 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-05 22:19:32.974 INFO 14092 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-05 22:19:33.124 INFO 14092 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2020-06-05 22:19:33.126 INFO 14092 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-05 22:19:33.127 INFO 14092 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-06-05 22:19:33.159 INFO 14092 --- [ main] o.a.c.c.C.[Tomcat-1].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-05 22:19:33.159 INFO 14092 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 176 ms
2020-06-05 22:19:33.172 INFO 14092 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 13 endpoint(s) beneath base path '/actuator'
2020-06-05 22:19:33.214 INFO 14092 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2020-06-05 22:19:33.225 INFO 14092 --- [ main] com.actuator.demo.DemoApplication : Started DemoApplication in 2.982 seconds (JVM running for 5.343)
2020-06-05 22:19:33.353 INFO 14092 --- [)-192.168.137.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-05 22:19:33.353 INFO 14092 --- [)-192.168.137.1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-06-05 22:19:33.358 INFO 14092 --- [)-192.168.137.1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
2020-06-05 22:20:51.503 INFO 14092 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat-1].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-05 22:20:51.503 INFO 14092 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-06-05 22:20:51.504 INFO 14092 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
按照提示,浏览器访问:http://localhost:8081/actuator,得到如下信息:
{"_links":{“self”:{“href”:“http://localhost:8081/actuator”,“templated”:false},“beans”:{“href”:“http://localhost:8081/actuator/beans”,“templated”:false},“caches-cache”:{“href”:“http://localhost:8081/actuator/caches/{cache}”,“templated”:true},“caches”:{“href”:“http://localhost:8081/actuator/caches”,“templated”:false},“health-path”:{“href”:“http://localhost:8081/actuator/health/{*path}”,“templated”:true},“health”:{“href”:“http://localhost:8081/actuator/health”,“templated”:false},“info”:{“href”:“http://localhost:8081/actuator/info”,“templated”:false},“conditions”:{“href”:“http://localhost:8081/actuator/conditions”,“templated”:false},“configprops”:{“href”:“http://localhost:8081/actuator/configprops”,“templated”:false},“env”:{“href”:“http://localhost:8081/actuator/env”,“templated”:false},“env-toMatch”:{“href”:“http://localhost:8081/actuator/env/{toMatch}”,“templated”:true},“loggers”:{“href”:“http://localhost:8081/actuator/loggers”,“templated”:false},“loggers-name”:{“href”:“http://localhost:8081/actuator/loggers/{name}”,“templated”:true},“heapdump”:{“href”:“http://localhost:8081/actuator/heapdump”,“templated”:false},“threaddump”:{“href”:“http://localhost:8081/actuator/threaddump”,“templated”:false},“metrics-requiredMetricName”:{“href”:“http://localhost:8081/actuator/metrics/{requiredMetricName}”,“templated”:true},“metrics”:{“href”:“http://localhost:8081/actuator/metrics”,“templated”:false},“scheduledtasks”:{“href”:“http://localhost:8081/actuator/scheduledtasks”,“templated”:false},“mappings”:{“href”:“http://localhost:8081/actuator/mappings”,“templated”:false}}}
可以看到输出日志**Exposing 13 endpoint(s) beneath base path ‘/actuator’**包含的13的endpoints,如下图:
可以根据上面提供的链接去访问对应的监控端点。
1.2自定义监控端点
示例:演示当改变一个变量状态时,我们可以通过端点监控变量的状态。
步骤:
(1)创建一个状态服务类,用来改变状态值
(2)创建端点。在springboot2.1中,只需要3个注解即可完成。
@Endpoint(id = “stat”) 自定义端点 /actuator/stat,注解于类
@ReadOperation 读取数据操作,注解于方法
@Selector 端点注入参数 ,注解于参数
(3)具体实现:
//(1)创建一个状态服务类,用来改变状态值
public class StatusService {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
//创建端点
@Endpoint(id = "stat")
@Component
public class StatusEndpoint {
@Resource
StatusService statusService;
@ReadOperation
public String getStatus(@Selector String status){
return status;
}
}
测试:
浏览器输入http://localhost:8081/actuator,可以发现新增的自定义端点,并且按照格式去访问,可以获取到监控端点的结果信息。
2.通过使用JMX实现对应用的监控和管理
启动程序,打开cmd命令行,在命令行输入jconsole即可调起Java监视和管理控制台, 找到程序所在线程点击就可进入界面。进入界面后,在MBean标签的org.springframework.boot域下可对我们的程序进行监控和管理,如下:
具体使用需要深入学习。。。
3.通过使用SSH实现对应用的监控和管理
我们还可以通过SSH或TELNET监控和管理我们的应用,这一点spring boot是借助CraSH(http://www.crashub.org)来实现。在应用中,需在spring boot项目中添加spring-boot-starter-shell依赖。但是springboot2.0以后已经废弃,所以不做学习研究。