栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

在传统Spring应用中使用spring-boot-actuator模块提供监控端点

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在传统Spring应用中使用spring-boot-actuator模块提供监控端点

我们介绍了Spring Boot Actuator模块为应用提供的强大监控能力。在Spring Boot应用中,我们只需要简单的引入spring-boot-starter-actuator依赖就能为应用添加各种有用的监控端点。其中,/health端点能够全面检查应用的健康状态,该端点也被Spring Cloud中的服务治理(Eureka、Consul)用来检查应用的健康状态。所以,在使用Spring Cloud构建微服务架构的时候,如果还存在一些遗留的传统Spring应用时,我们就需要为这些应用也加入/health端点。那么在传统的Spring应用中我们是否也能引入该模块来提供这些有用的监控端点呢?下面我们就来介绍整合的详细步骤:

第一步:引入相关依赖

由于在传统Spring应用中,我们不能直接使用Starter POMs。所以,我们需要拆解了来引入到传统Spring应用的pom.xml中,主要有如下两个依赖:


    org.springframework.boot
    spring-boot-actuator
    1.4.3.RELEASE
    jar
    org.hibernate
    hibernate-validator
    4.3.2.Final
第二部:手工引入配置

由于在传统Spring应用中没有自动化配置功能,所以我们需要手工的来创建配置并启用Spring Boot Actuator的监控端点。比如,我们先来创建一个实现/health端点的配置,具体如下:

@Configuration@import({ EndpointAutoConfiguration.class,
        HealthIndicatorAutoConfiguration.class})public class MyAppSpringConfig {    @Bean
    public EndpointHandlerMapping endpointHandlerMapping(
            Collection endpoints) {        return new EndpointHandlerMapping(endpoints);
    }    @Bean
    public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate) {        return new HealthMvcEndpoint(delegate, false);
    }

}

其中,@import中引入的org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration类是Spring Boot Actuator的基础配置类。org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration类是/health端点的基础配置,具体内容本文不做详细展开,读者可自行查看。而在该配置类中,还创建了两个Bean,其中EndpointHandlerMapping是org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping的子类,它用来加载所有的监控端点;而HealthMvcEndpoint是具体的/health端点实现。

在完成上面配置之后,我们就可以启动Spring应用,此时就可以看控制台中看到打印出了/health端点,我们可以尝试访问该端点来获取当前实例的健康状况。

除了在传统应用中可以加载/health端点之外,我们也可以如法炮制地创建其他端点,比如:获取各个度量指标的/metrics端点,可以通过如下配置实现:

@Configuration@import({ EndpointAutoConfiguration.class,
        PublicMetricsAutoConfiguration.class,
        HealthIndicatorAutoConfiguration.class})public class MyAppSpringConfig {    @Bean
    public EndpointHandlerMapping endpointHandlerMapping(
            Collection endpoints) {        return new EndpointHandlerMapping(endpoints);
    }    @Bean
    public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate) {        return new HealthMvcEndpoint(delegate, false);
    }    @Bean
    public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {        return new EndpointMvcAdapter(delegate);
    }
  
}

这里,我们主要增加了两个内容:

  • @import中增加引入PublicMetricsAutoConfiguration配置类

  • 创建/metrics端点的实现Bean

到这里,本文的内容就介绍完了,更多关于传统Spring应用与Spring Boot/Cloud的配合使用。



作者:程序猿DD
链接:https://www.jianshu.com/p/8e1ede4ca972


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号