Spring Cloud应用监控与管理Actuator

  由于我们把一个复杂高耦合的单体系统拆分成了多个小型服务,所以部署应用的数量在不断增长,造成维护复杂度大大提升。所以我们需要一套自动化的监控运维机制,这套运维机制可以不间断的获取每个服务应用的各种指标,并根据这些指标信息来制定监控预警规则。

       Spring Boot提供了一个依赖模块:spring-boot-starter-actuator,这个模块可以自动为Spring Boot创建的应用构建一系列的用于监控的端点,而且Spring Cloud还在这个基础上进行了扩展,当然在不满足我们业务需求时也需要对这个模块进行扩展。

        接下来创建一个Spring Boot项目命名actuator,勾选Actuator依赖

或者在你现有的Spring Boot项目里加上依赖

 
  1. <dependency>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-actuator</artifactId>

  4. </dependency>

项目创建完毕后的pom文件:

 
  1. <parent>

  2. <groupId>org.springframework.boot</groupId>

  3. <artifactId>spring-boot-starter-parent</artifactId>

  4. <version>2.0.2.RELEASE</version>

  5. <relativePath/> <!-- lookup parent from repository -->

  6. </parent>

  7.  
  8. <properties>

  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  10. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

  11. <java.version>1.8</java.version>

  12. </properties>

  13.  
  14. <dependencies>

  15. <dependency>

  16. <groupId>org.springframework.boot</groupId>

  17. <artifactId>spring-boot-starter-actuator</artifactId>

  18. </dependency>

  19. <dependency>

  20. <groupId>org.springframework.boot</groupId>

  21. <artifactId>spring-boot-starter-web</artifactId>

  22. </dependency>

  23.  
  24. <dependency>

  25. <groupId>org.springframework.boot</groupId>

  26. <artifactId>spring-boot-starter-tomcat</artifactId>

  27. <scope>provided</scope>

  28. </dependency>

  29. <dependency>

  30. <groupId>org.springframework.boot</groupId>

  31. <artifactId>spring-boot-starter-test</artifactId>

  32. <scope>test</scope>

  33. </dependency>

  34. </dependencies>

  35.  
  36. <build>

  37. <plugins>

  38. <plugin>

  39. <groupId>org.springframework.boot</groupId>

  40. <artifactId>spring-boot-maven-plugin</artifactId>

  41. </plugin>

  42. </plugins>

  43. </build>

说一下我的版本:jdk1.8、Spring Boot 2.0.2。

接下来就可以启动应用了,发现控制台打印如下信息:

/actuator/health和/actuator/info以及/actuator这三个就是actuator提供的端点,注意以前的版本是没有/actuator前缀的,2.0以后的版本都加了/actuator前缀,而且看官方文档actuator提供了如下端点:

我们只有health和info是因为actuator默认只暴露了health和info端点,我们可以选择全部暴露或者指定暴露部分端点,修改application.yml

 
  1. management:

  2. endpoints:

  3. web:

  4. exposure:

  5. include: "*" #暴露所有端点 默认是info,health

 重新启动应用,控制台打印

发现有其余的端点了,下面是对部分端点的简要说明

 

 

auditevents公开当前应用程序的审计事件信息。
beans显示应用程序中所有Spring bean的完整列表。
configprops显示应用中配置的属性信息报告。
env显示应用中所有可用的环境属性报告,包括环境变量、JVM属性、应用的配置属性、命令行的参数。
health显示应用健康信息。
httptrace显示HTTP跟踪信息(默认情况下为最后100个HTTP请求 - 响应交换)。
info显示应用的自定义信息,默认是空。
metrics显示当前应用程序的“指标”信息,如内存信息、线程信息。
mappings显示所有url映射。
scheduledtasks显示应用程序中的计划任务。
shutdown让应用程序正常关机。
threaddump程序运行中的线程信息。

详细说明查看actuator-api文档点击打开链接,注意这是Spring Boot2.0.2的文档,其余版本请去官网自行查找。

开启和关闭端点

使用management.endpoint.<id>.enabled来修改端点的开启关闭状态,如以关闭health端点为例

management.endpoint.health.enabled=false

如果您希望端点启用选择加入而不是选择退出,请将management.endpoints.enabled-by-default属性设置 为false并使用各个端点 enabled属性重新加入。以下示例启用info端点并禁用所有其他端点:

 

management.endpoints.enabled-by-default = false
management.endpoint.info.enabled = true

修改路径

现在所有端点的前缀默认是/actuator,如果想修改的话用management.endpoints.web.base-path属性

如果想修改端点的路径,可以用 management.endpoints.web.path-mapping属性

比如我们想把/autuator/health修改为/healthcheck,

 

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck

重启后所有端点都去掉了/actuator前缀,并且health端点的路径变成了healthcheck

当然,如果你想修改端点的端口,也是可以的,可以通过以下属性修改

 

management.server.port = 8081

如果您不想通过HTTP公开端点,则可以将管理端口设置为-1

 

management.server.port = -1

关于shutdown端点

shutdown端点可以用来远程关闭应用,此端点默认是关闭的,如果使用的话,需要开启,使用以下属性

management.endpoint.shutdown.enabled = true

你就可以在应用启动后远程通过调用/actuator/shutdown来关闭应用,注意只能POST请求调用。

关于health端点

 

我们尝试访问/actuator/health端点,返回

{"status":"UP"}

只有status一个属性,查看官方文档

health端点的management.endpoint.health.show-details属性默认不展示细节,我们可以修改一下

 
  1. management:

  2. endpoints:

  3. web:

  4. exposure:

  5. include: "*" #暴露所有端点 默认是info和health

  6. endpoint:

  7. health:

  8. show-details: always #默认是never

重新启动再次请求,会发现多了一个磁盘空间的状态信息,返回

{"status":"UP","details":{"diskSpace":{"status":"UP","details":{"total":169917878272,"free":138603999232,"threshold":10485760}}}}

health端点默认自带了一些常用资源的健康指标检测器,只要你引入了以下依赖就会自动添加到health里

我们也可以自己扩展一个健康指标检测器

 
  1. /**

  2. * 1.实现HealthIndicator接口

  3. * 2.类名要求 xxxHealthIndicator xxx将会是你自定义得健康指标名称

  4. * 3.@Component注入到容器内

  5. * 4.重写health()方法

  6. * @author Administrator

  7. *

  8. */

  9. @Component

  10. public class MyAppHealthIndicator implements HealthIndicator{

  11.  
  12. @Override

  13. public Health health() {

  14. if(check()!=0){

  15. return Health.up().build();

  16. }

  17. return Health.down().withDetail("error", "出错了").build();

  18. }

  19.  
  20. private int check(){

  21. // 检测是否健康的自定义逻辑

  22. return 0;

  23. }

  24. }

然后重启应用发现多了自定义的健康指标

关于info端点

info端点默认是空的,我们可以在application配置文件中配置info前缀的属性来完善

 
  1. info:

  2. app:

  3. version: 1.1

  4. name: aut #/actuator/info 自定义的info端点 否则是空的

访问/actuator/info

我们也可以用info端点描述Git版本信息。

在application.yml或者application.properties同级目录创建git.properties,添加属性git.branch=master

再次重启访问/actuator/info

git.后面的属性是来自于GitProperties类,eclipse中使用ctrl+shift+t输入GitProperties就可以查看了,前提是你下载了源码,当然你也可以引入git的插件,具体我就不介绍了,想了解的可以看下这篇文章

http://blog.didispace.com/spring-boot-actuator-info-git/,总的来说info端点用途并不大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值