zoukankan      html  css  js  c++  java
  • SpringBoot学习之SpringBoot执行器

        在以往的分布式开发当中,各个服务节点的监控必不可少。监控包含有很多方面,比如说:内存占用情况,节点是否健康等。在spring-boot会给我们提供相关资源监控叫做spring-boot-actuator通过执行器可以帮我管理和监控生产环境下的应用服务。

     一。添加SpringBoot执行器的依赖(版本2.0.0.RELEASE)

       添加gradle配置依赖:

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-actuator')
    }
    View Code

     二。关于SpringBoot的端点

         端点是一个提供给我们监控应用程序的功能点,SpringBoot提供了一系列内置端点叫我们使用,举个例子:health端点为我们提供了一个对我们基础程序的一个健康状态的监控

    每个端点都可以打开或关闭,绝大多数的应用都可以通过http请求进行访问,springboot包含很多内置内置端点。tips:参考官网的

    IDDescriptionEnabled by default

    auditevents

    Exposes audit events information for the current application.

    Yes

    beans

    Displays a complete list of all the Spring beans in your application.

    Yes

    conditions

    Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.

    Yes

    configprops

    Displays a collated list of all @ConfigurationProperties.

    Yes

    env

    Exposes properties from Spring’s ConfigurableEnvironment.

    Yes

    flyway

    Shows any Flyway database migrations that have been applied.

    Yes

    health

    Shows application health information.

    Yes

    httptrace

    Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges).

    Yes

    info

    Displays arbitrary application info.

    Yes

    loggers

    Shows and modifies the configuration of loggers in the application.

    Yes

    liquibase

    Shows any Liquibase database migrations that have been applied.

    Yes

    metrics

    Shows ‘metrics’ information for the current application.

    Yes

    mappings

    Displays a collated list of all @RequestMapping paths.

    Yes

    scheduledtasks

    Displays the scheduled tasks in your application.

    Yes

    sessions

    Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications.

    Yes

    shutdown

    Lets the application be gracefully shutdown.

    No

    threaddump

    Performs a thread dump.

    Yes

      

    有几点要补充说明一下:

      1). 我们访问的端点监控的地址规范是:/actuator/{ID}的方式访问,比如说:health端点默认被映射的路径就是/actuator/health

          2) 并不是所有端点都可以通过http请求访问,以下表格列举了各个端点的状态值:

    IDJMXWeb

    auditevents

    Yes

    No

    beans

    Yes

    No

    conditions

    Yes

    No

    configprops

    Yes

    No

    env

    Yes

    No

    flyway

    Yes

    No

    health

    Yes

    Yes

    heapdump

    N/A

    No

    httptrace

    Yes

    No

    info

    Yes

    Yes

    jolokia

    N/A

    No

    logfile

    N/A

    No

    loggers

    Yes

    No

    liquibase

    Yes

    No

    metrics

    Yes

    No

    mappings

    Yes

    No

    prometheus

    N/A

    No

    scheduledtasks

    Yes

    No

    sessions

    Yes

    No

    shutdown

    Yes

    No

    threaddump

    Yes

    No

      
        我们可以发现默认情况下只有health与info端点才能通过http请求访问,当然我们可以在属性文件中配置 management.endpoints.web.exposure.include 来开放可以访问的端点:
        
    management.endpoints.web.exposure.include=*
    management.endpoints.web.exposure.exclude=env,beans

    3)当我们访问端点时,响应的数据会缓存一定的时间,我们可以通过这个属性进行配置:

      

    management.endpoint.<id>.cache.time-to-live=10s
    4) 我们也可以自己定义监视路径,默认情况是:/actuator/xxx,通过如下属性可以设置:
    management.endpoints.web.base-path=/

    5) 关于保护敏感端点,首先我们要添加对spring-security的依赖,并设置进行安全验证的用户名,密码以及角色,如果不使用spring-security就要慎重考虑暴露端口的端点了

       三。关于Health端点

    1.  health端点用于检测我们运行程序的健康状态,当程序宕机时,可以提供给开发人员相关的提示信息
    2. 默认情况下该端点只是显示简略的监控信息,不过我们可以通过management.endpoint.health.show-details属性来让其显示详细的监控信息
    3. 端点有如下几种状态: UP DOWN UNKNOW-SERVICE 从字面上我们很好理解
    4. 实现自定义健康监控:
    package com.bdqn.lyrk.springboot.study.monitor;
    
    import org.springframework.boot.actuate.health.Health;
    import org.springframework.boot.actuate.health.HealthIndicator;
    import org.springframework.stereotype.Component;
    
    @Component
    public class SelfMonitor implements HealthIndicator {
        @Override
        public Health health() {
            return Health.up().withDetail("health","next").build();
        }
    }
    View Code

        例子很简单,主要是实现HealthIndicator接口,当我们访问:http://localhost:8080/actuator/health时,我们可以看到相关监控信息,如下图:

        

  • 相关阅读:
    【SQL】通过Sql实现根据分组合并指定列内容的查询 SamWang
    [转]SQLServer中全文搜索与Like的差异分析
    【笔记】《C#高效编程改进C#代码的50个行之有效的办法》第1章C#语言习惯(1)属性的特性以及索引器(SamWang)
    [转]SqlServer全文索引实例
    [转]visual studio2005中文版无法打开项目文件:*.csproj,提示“此安装不支持该项目类型”的解决办法
    [转]李天平:程序人生成长发展中的一些感悟
    【改进】C# WinForm捕获全局异常 SamWang
    Windows 7 虚拟无线网卡(microserof virtual wifi miniport adapter)功能的使用
    VB.Net 串口通信示例
    VB.Net 串口通信用法
  • 原文地址:https://www.cnblogs.com/niechen/p/8072722.html
Copyright © 2011-2022 走看看