转载 springboot 监控 Actuator使用

目录

添加依赖与配置

Actuator监控项

Actuator监控管理

打开或关闭

端口与地址


Actuator是Springboot提供的用来对应用系统进行自省和监控的功能模块,借助于Actuator开发者可以很方便地对应用系统某些监控指标进行查看、统计等。本文将通过示例来对如何在Springboot中使用Actuator监控做一个简单介绍,更多内容请移步官方文档。  

添加依赖与配置

在Springboot中使用Actuator监控非常简单,只需要在工程POM文件中引入spring-boot-starter-actuator依赖即可。 


 
 
  1. <!-- 引入Actuator监控依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.boot </groupId>
  4. <artifactId>spring-boot-starter-actuator </artifactId>
  5. </dependency>

在本项目中,除了引入Actuator依赖之外还额外引入了JDBC依赖(用来对数据源健康状态进行示例)和WEB依赖(启用WEB方式查看监控信息),完整的依赖配置如下。  


 
 
  1. <parent>
  2. <groupId>org.springframework.boot </groupId>
  3. <artifactId>spring-boot-starter-parent </artifactId>
  4. <version>1.5.6.RELEASE </version>
  5. </parent>
  6. <dependencies>
  7. <!-- 添加MySQL依赖 -->
  8. <dependency>
  9. <groupId>mysql </groupId>
  10. <artifactId>mysql-connector-java </artifactId>
  11. </dependency>
  12. <!-- 添加JDBC依赖 -->
  13. <dependency>
  14. <groupId>org.springframework.boot </groupId>
  15. <artifactId>spring-boot-starter-jdbc </artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot </groupId>
  19. <artifactId>spring-boot-starter-web </artifactId>
  20. </dependency>
  21. <!-- 引入Actuator监控依赖 -->
  22. <dependency>
  23. <groupId>org.springframework.boot </groupId>
  24. <artifactId>spring-boot-starter-actuator </artifactId>
  25. </dependency>
  26. </dependencies>

同时,application.properties核心配置文件中除了定义数据源外,还需要添加 management.security.enabled=false 配置。  

#########################################################
###   Actuator Monitor  --   Actuator configuration   ###
#########################################################
management.security.enabled=false

#########################################################
###  Spring DataSource  -- DataSource configuration   ###
#########################################################
spring.datasource.url=jdbc:mysql://localhost:3306/dev1?useUnicode=true&characterEncoding=utf8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

注意:若在核心配置文件中未添加 management.security.enabled=false 配置,将会导致用户在访问部分监控地址时访问受限,报401未授权错误。  

Actuator监控项

下表包含了Actuator提供的主要监控项。 

下面逐个对每一个监控项进行举例介绍。

/autoconfig用来查看自动配置的使用情况,包括:哪些被应用、哪些未被应用以及它们未被应用的原因、哪些被排除。 

/configprops可以显示一个所有@ConfigurationProperties的整理列表。

/beans可以显示Spring容器中管理的所有Bean的信息。 

/dump用来查看应用所启动的所有线程,每个线程的监控内容如下图所示。 

/env用来查看整个应用的配置信息,使用/env/[name]可以查看具体的配置项。 

/health用来查看整个应用的健康状态,包括磁盘空间使用情况、数据库和缓存等的一些健康指标。 

此外,Springboot还允许用户自定义健康指标,只需要定义一个类实现HealthIndicator接口,并将其纳入到Spring容器的管理之中。  


 
 
  1. @Component
  2. public class MyHealthIndicator implements HealthIndicator{
  3. @Override
  4. public Health health() {
  5. return Health.down().withDetail( "error", "spring boot error").build();
  6. }
  7. }

/info可以显示配置文件中所有以info.开头或与Git相关的一些配置项的配置信息。

/mappings用来查看整个应用的URL地址映射信息。  

/metrics用来查看一些监控的基本指标,也可以使用/metrics/[name]查看具体的指标。 

此外,Springboot还为提供了CounterService和GaugeService两个Bean来供开发者使用,可以分别用来做计数和记录double值。 


 
 
  1. @RestController
  2. public class ActuatorController {
  3. @Autowired
  4. private CounterService counterService;
  5. @Autowired
  6. private GaugeService gaugeService;
  7. @GetMapping( "/home")
  8. public String home() {
  9. //请求一次浏览数加1
  10. counterService.increment( "home browse count");
  11. //请求时将app.version设置为1.0
  12. gaugeService.submit( "app.version", 1.0);
  13. return "Actuator home";
  14. }
  15. }

/shutdown是一个POST请求,用来关闭应用,由于操作比较敏感,默认情况下该请求是被禁止的,若要开启需在配置文件中添加以下配置:  

endpoints.shutdown.enabled: true

/trace用来监控所有请求的追踪信息,包括:请求时间、请求头、响应头、响应耗时等信息。 

Actuator监控管理

打开或关闭

Actuator监控的所有项目都定义在spring-boot-actuator-x.x.x.RELEASE.jar的org.springframework.boot.actuate.endpoint包中,包含以下Endpoint。 

这些Endpoint都继承自AbstractEndpoint,AbstractEndpoint中定义了两个重要的属性:enabled和sensitive。

其中,enabled用来打开或关闭该监控项,语法为:endpoints.[endpoint_name].enabled=false/true,以关闭/autoconfig监控项为例,其配置如下。 

endpoints.autoconfig.enabled=false

sensitive用来配置该监控项是否属于敏感信息,访问敏感信息需要用户具有ACTUATOR角色权限,或者使用以下配置关闭安全限制。  

management.security.enabled=false

端口与地址

除了使用与应用相同的端口访问监控地址外,我们还可以在配置文件中增加 management.port 配置项来自己指定监控的请求端口。 

management.port=9090

还可以通过 management.address 配置项来指定可以请求监控的IP地址,比如只能通过本机监控,可以设置 management.address = 127.0.0.1 。  

        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/pengjunlee">
                <img src="https://profile.csdnimg.cn/8/B/D/3_pengjunlee" class="avatar_pic" username="pengjunlee">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit "><a href="https://blog.csdn.net/pengjunlee" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">pengjunlee</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog8.png">                                                    <span class="flag expert">
                            <a href="https://blog.csdn.net/home/help.html#classicfication" target="_blank">
                            <img src="https://csdnimg.cn/release/phoenix/template/new_img/identityExpert.png" alt="">
                                博客专家
                            </a>
                        </span>
                                        </div>
                <div class="text"><span>原创文章 256</span><span>获赞 1051</span><span>访问量 1276万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}">关注</a>
                                                            <a href="https://bbs.csdn.net/topics/395528935" target="_blank" class="btn btn-sm bt-button personal-messageboard">他的留言板
                    </a>
                                </div>
                        </div>
                    
    </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值