第一步 添加actuator依赖
1.pom.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>configuration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>configuration</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步 加入配置
1.application.properties 如下:
#web服务端端口
server.port=6000
#监控地址端口
management.server.port=8000
#springboot2.0之后,在Http环境下将默认的endpoint只设置为info和health,要想开启其他的监控功能,需要手动配置
management.endpoints.web.exposure.include=*
#请求连接前缀 默认是/actuator
management.endpoints.web.base-path=/actuator
#开启远程关闭服务
management.endpoint.shutdown.enabled=true
第三步 启动验证
1.启动项目
启动日志信息中包含如下信息:
从上图可以看出我们将要测试的映射连接地址
2.使用浏览器或者Postman
2.1.在地址栏输入 http://127.0.0.1:8000/actuator/shutdown ,如能正常返回 如下则成功访问
{
"message": "Shutting down, bye..."
}
2.2.查看后台日志打印结果
2019-04-22 22:17:21.354 INFO 9704 --- [on(4)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2019-04-22 22:17:21.355 INFO 9704 --- [on(4)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2019-04-22 22:17:21.392 INFO 9704 --- [on(4)-127.0.0.1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 37 ms
2019-04-22 22:20:41.125 INFO 9704 --- [io-8000-exec-10] o.a.c.c.C.[Tomcat-1].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2019-04-22 22:20:41.125 INFO 9704 --- [io-8000-exec-10] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2019-04-22 22:20:41.129 INFO 9704 --- [io-8000-exec-10] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 4 ms
2019-04-22 22:20:41.692 INFO 9704 --- [ Thread-34] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@59e2d8e3: startup date [Mon Apr 22 22:17:12 CST 2019]; root of context hierarchy
2019-04-22 22:20:41.696 INFO 9704 --- [ Thread-34] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5a75ec37: startup date [Mon Apr 22 22:17:18 CST 2019]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@59e2d8e3
2019-04-22 22:20:42.694 INFO 9704 --- [ Thread-34] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Disconnected from the target VM, address: '127.0.0.1:7115', transport: 'socket'
写在最后,如果项目部署在外网环境,请谨慎使用 ~ ~