1. actuator介绍
actuaotr是spring boot项目中非常强大的一个功能,有助于对应用程序进行监控和管理,通过restful api请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节。
2. Endpoints
actuator的核心部分,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的Endpoints(health、info、beans、httptrace、shutdown)等等,同时也允许我们自己扩展自己的端点。
注意:spring boot 2.0中的端点和之前版本有较大不同,另外端点的监控机制也有很大不同,启用了不代表可以直接访问,还需要将其暴露出来,传统的management.security管理已被标记为不推荐。
actuator内置端点endPoints id
描述 默认启用 auditevents 显示当前应用程序的审计事件信息 Yes beans 显示应用Spring Beans的完整列表 Yes caches 显示可用缓存信息 Yes conditions 显示自动装配类的状态及及应用信息 Yes configprops 显示所有 @ConfigurationProperties 列表 Yes env 显示 ConfigurableEnvironment 中的属性 Yes flyway 显示 Flyway 数据库迁移信息 Yes health 显示应用的健康信息(未认证只显示 status
,认证显示全部信息详情)No info 显示任意的应用信息(在资源文件写info.xxx即可) No liquibase 展示Liquibase 数据库迁移 Yes metrics 展示当前应用的 metrics 信息 Yes mappings 显示所有 @RequestMapping 路径集列表 Yes scheduledtasks 显示应用程序中的计划任务 Yes sessions 允许从Spring会话支持的会话存储中检索和删除用户会话。 Yes shutdown 允许应用以优雅的方式关闭(默认情况下不启用) Yes threaddump 执行一个线程dump Yes httptrace 显示HTTP跟踪信息(默认显示最后100个HTTP请求 - 响应交换) Yes
3.springBoot 整合 actuator
(1)导入pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
(2)配置properties,启动端点
#info信息 info.blog-url=http://xiaolan info.author=qiuqiu info.version=@project.version@ #加载所有的断点,一般情况下默认的是health,info management.endpoints.web.exposure.include=* #排除所有的断点 #management.endpoints.web.exposure.exclude=* #一直显示health详细信息 management.endpoint.health.show-details=always #禁用其他端点 #management.endpoints.enabled-by-default=false #启动shutdown(可以是任一个合适的端点)端点,优雅的关闭服务(所谓优雅的关闭就是如果有执行的任务会在执行完且响应后关闭任务) management.endpoint.shutdown.enabled=true
注意:在yml中配置的话,所以如果你想包含(或排除)所有的端点,一定要加引号如下:
management: endpoints: web: exposure: include: '*'
(3)启动服务
访问 ip:项目端口/项目名称/actuator/health
会看到一些数据,{ status: "UP", diskSpace: { status: "UP", total: 65, free: 4564565, threshold: 456}, db: { status: "UP", database: "MySQL", hello: 1 } }等等,是配置成功的 . 如果仅有{status:"up"}则是没有配置成功,这里不一一演示了
注意:
(1)shutdown如果开启注意要验证,避免恶性的关闭项目,且访问要发post请求,其他的端点是get请求.
(2)如果需要访问info接口来获取maven中的属性内容请记得添加如下内容
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>build-info</goal> </goals> </execution> </executions> </plugin> </plugins> </build>