`

SpringBoot:Actuator 应用监控管理

阅读更多

编写不易,转载请注明(http://shihlei.iteye.com/blog/2433665)!

一 概述

SpringBoot Actuator 是SpringBoot提供的 “应用监控管理” 工具,可以方便的获取系统的各种信息,配合其他的监控工具定时采集这些信息完成系统监控。

 

Endpoint 是 Actuator 暴露的信息端点,Actuator 内建了众多的Endpoint 覆盖了常用的监控管理点,同时开发一套框架,方便应用定义自己的Endpoint。

 

应用获得 Actuator 只需要添加“spring-boot-starter-actuator”依赖即可。

 

doc:https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#production-ready

 

注:文章基于 SpringBoot 版本 : 2.0.5.RELEASE

 

二 Actuator 使用

1)Actuator 使用初步

(1)添加依赖

        <!-- 监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

 

(2)添加配置

 

# actuator 监控
management:
  # 监控开放的端口,可以区别于 应用web服务端口
  server:
    port: 8088
  endpoints:
    # 配置Http 暴露的 Endpoint
    web:
      exposure:
        # include 用于开放指定Endpoint,只开放需要的,默认["info","health"]
        include: ["*"]
        # exclude 用于排除指定的Endpoint,只开放需要的
        exclude: []
      # web 访问路径
      base-path: /actuator


    # 配置Http 暴露的 Endpoint
    jmx:
      exposure:
        # include 用于开放指定Endpoint,只开放需要的,默认["*"]
        include: ["*"]
        # exclude 用于排除指定的Endpoint,只开放需要的
        exclude: []

    # enable 用于 开启禁用 某个 Endpoint, "shutdown" 为 Endpoint id
    shutdown:
      enabled: true

  # headlth Endpoint 配置
  endpoint:
    health:
      show-details: always

 

(3)启动 SpringBoot App

  【1】Idea SpringBoot RunDashboard 查看



 

 【2】web访问:Http://localhost:8088/actuator

 

 

2)内建Endpoint 说明:

(1)概述:

Actuator 默认内建了大量的Endpoint,提供了常用的监控管理功能。按照Endpoint的职能,一般分为如下三类:

(a)配置类:查看应用启动时的某些配置信息

(b)指标类:查看应用运行时的实时信息

(c)控制类:操作应用,例如重启,关闭等

 

(2)SpringBoot2.0 内建Endpoint举例说明

【1】应用信息

info:显示yml配置的应用信息。

env,env-toMatch:环境信息

 

【2】配置:可用于检查系统的注入情况是否正确

beans:应用的 bean 列表

conditions:显示Conguration或AutoCongration创建的Bean及创建是Condition的匹配程度

loggers,loggers-name:日志Logger的信息

 

mappings:web应用@RequestMapping信息

 

【3】实时信息

health:健康信息

heapdump,threaddump:堆dump,线程栈dump

httptrace:最近该应用的http请求跟踪

metrics,metrics-requiredMetricName:监控信息

 

【4】操作:比较危险,一般不开放操作

shutdown:关闭应用

 

(3)开启关闭Endpoint配置:

management.endpoints.enabled-by-default=false
management.endpoint.[endpoint id].enabled=true

 

三 自定义 Endpoint

1)概述

Actuator 内建很多Endpoint ,基本涵盖各个分类,如果业务需要,某些监控管理功能不是和复用或扩展已有的Endpoint,可以自定义Endpoint。

 

2)自定义 Demo

(1)概述:

自定义Endpoint 需要2步:添加@Endpoint 注解修饰的Endpoint类,通过 @ReadOperation ,@WriteOperation,@DeleteOperation。注解方法。通过 Conguration 注册到容器中。

 

@ReadOperation:注解的方法提供获取信息的能力

@WriteOperation:注解的方法提供修改能力

@DeleteOperation:注解的方法某些删除能力

 

Demo 定义一个名叫 app 的endpoint ,提供name,platform信息

 

(2)Demo实现

依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

 

【1】定义Endpoint 类

App:

package x.demo.springboot.actuator.app;

/**
 * App 信息
 *
 */
public class App {
    private String name;
    private String platform;

    public App() {

    }

    public App(String name, String platform) {
        this.name = name;
        this.platform = platform;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }
}

 

Endpoint:

package x.demo.springboot.actuator.app;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;

@Endpoint(id = "app")
public class AppEndpoint {
    App app = new App("actuator-test", "ios");

    @ReadOperation
    public App read() {
        return app;
    }

    @WriteOperation
    public String write(String name, String platform) {
        app.setName(name);
        app.setPlatform(platform);
        return "success";
    }
}

  

【2】添加Configuration

package x.demo.springboot.actuator.app;

import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AppEndpointAutoConfiguration
 */
@Configuration
public class AppEndpointConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    public AppEndpoint appEndpoint() {
        return new AppEndpoint();
    }

} 

 

如果是自定义Starter里的Endpoint 可以配置下 AutoConfiguration:在META-INF/spring.factories 添加

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
x.demo.springboot.actuator.app.AppEndpointConfiguration

 

【3】启动

 

【4】JMX修改:jconsole --> MBean --> Endpoint --> read/write



 

 四 Health Endpoint

1)概述:

Health Endpoint 如前所述,可以提供应用的健康信息,包括应用自己和应用依赖的中间件或服务(如Redis,db等),所有组件工作正常,应用的status才是up状态。

 

HealthIndicator是 Health Endpoint 设计的组件的探测器,亦是扩展口,可以通过提供HealthIndicator,通知 Health Endpoint 探测我们关注的依赖服务的健康情况。

 

提供 HealthIndicator 主要两种方式:实现 HealthIndicator 或 继承 AbstractHealthIndicator 等

 

2)Demo

(1)方式一:实现 HealthIndicator

package x.demo.springboot.actuator.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

/**
 * AppHealthIndicator
 *
 */
@Component("app")
public class AppHealthIndicator implements HealthIndicator {

    /**
     * Return an indication of health.
     *
     * @return the health for
     */
    @Override
    public Health health() {
        int errorCode = check();
        if (errorCode != 0) {
            // 状态不正常
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        // 健康
        return Health.up().build();
    }

    private int check() {
        return -1;
    }
}

 

(2)方式二:继承 AbstractHealthIndicator

package x.demo.springboot.actuator.health;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

/**
 * AppExtraHealthIndicator
 *
 */
@Component("app-extra")
public class AppExtraHealthIndicator extends AbstractHealthIndicator {

    /**
     * Actual health check logic.
     *
     * @param builder the {@link Builder} to report health status and details
     * @throws Exception any {@link Exception} that should create a {@link Status#DOWN}
     *                   system status.
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        // 如果不正常,抛异常接口,AbstractHealthIndicator 完成异常捕获,down
        builder.up().withDetail("app-extra", "app-extra");
    }
}

 

(3)提供Configuration 通知框架

package x.demo.springboot.actuator.health;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AppHealthIndicatorConfiguration
 */
@Configuration
public class AppHealthIndicatorConfiguration {

    // 指定名字
    @Bean("app-state")
    AppHealthIndicator appHealthIndicator() {
        return new AppHealthIndicator();
    }

    @Bean("app-state-extra")
    AppExtraHealthIndicator appExtraHealthIndicator() {
        return new AppExtraHealthIndicator();
    }
}

 

(4)验证

 

 

  • 大小: 273.7 KB
  • 大小: 355 KB
  • 大小: 135.2 KB
  • 大小: 79 KB
  • 大小: 184.1 KB
  • 大小: 80.8 KB
  • 大小: 259.5 KB
分享到:
评论

相关推荐

    一个使用springboot actuator监控应用的实战项目例子

    一个使用springboot actuator监控应用的实战项目例子,对于想使用actuator来监控应用的初级程序员来说是一个不错的学习例子! 关注我!给我留言或者私信发邮箱,看到的话可以给你们发资源哦~

    11springboot+actuator监控1

    springboot(十九):使用Spring Boot Actuator监控应用 - 纯洁的微笑博客http://www.ityouknow.com/spri

    使用SpringBoot Actuator监控应用示例

    Actuator是Spring Boot提供的对应用系统的自省和监控的集成功能,可以对应用系统进行配置查看、相关功能统计等。这篇文章主要介绍了使用SpringBoot Actuator监控应,有兴趣的可以了解一下

    Springboot actuator应用后台监控实现

    主要介绍了Springboot actuator应用后台监控实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    springboot 使用Spring Boot Actuator监控应用小结

    本篇文章主要介绍了springboot 使用Spring Boot Actuator监控应用小结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    boot-actuator:基于SpringBoot2.0 实现的jvm远程监工图形化工具,可以同时监控多个web应用,支持远程监控

    就可以实现远程监控,和用户管理模块,动态定时任务支付windows服务器和Linux服务监控,Mac还未测试 应该也支持参考项目地址:项目框架SpringBoot 2.0.3.RELEASEmybatis-plus 3.6MySqlJdk1.8目录说明actuator-...

    xmljava系统源码-spring-boot-2.x-scaffold:Spring-boot-2.x脚手架,组件包括:Actuator,S

    应用监控:Actuator Swagger 2.8 统一异常处理 数据库连接池:HikariCP Mybatis Redis MongoDB 基本概念 spring-boot-starter-parent 使用 Maven 可以继承 spring-boot-starter-parent 项目来获得一些合理的默认配置...

    SpringBoot新手学习手册

    Actuator监控应用 38 Maven依赖 38 YML配置 39 Actuator访问路径 40 Admin-UI分布式微服务监控中心 40 Admin-UI-Server 40 Admin-UI-Client 41 十、 性能优化 43 组件自动扫描带来的问题 43 将Servlet容器...

    Dashing-for-Spring-Boot-Actuator:ShopifyDashing 监控 Spring Boot Actuator 应用程序的实现

    按照本教程启动 Spring Boot Actuator 应用程序。 然后,查看以获取有关如何启动此仪表板的更多信息。

    springboot学习思维笔记.xmind

    提供运行时的应用监控 极大地提高了开发,部署效率 与云计算的天然集成 缺点 书籍文档较少,且不够深入 SpringBoot版本 SpringBoot快速搭建 http://start.spring.io SpringTool...

    如何监控springboot的健康状况.docx

    使用Actuator检查与监控 actuaotr是spring boot项目中非常强大的一个功能,有助于对应用程序进行监控和管理,通过restful api请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节。

    springboot教程.doc

    了解Spring Boot的核心特性,如自动配置、起步依赖、Actuator监控等。 创建Spring Boot项目: 使用Spring Initializr(https://start.spring.io/)或者IDE(如IntelliJ IDEA、Eclipse等)中的Spring Boot项目模板来...

    springboot学习

    在传统Spring应用中使用spring-boot-actuator模块提供监控端点 Spring Boot应用的后台运行配置 Spring Boot自定义Banner Dubbo进行服务治理 chapter9-2-1:Spring Boot中使用Dubbo进行服务治理 chapter9-2-2:Spring...

    SpringBoot实战(第4版)

    7.3 通过 JMX 监控应用程序 ....................... 126 7.4 定制 Actuator......................................... 128 7.4.1 修改端点 ID ............................... 128 7.4.2 启用和禁用端点 ...........

    基于springboot的web项目最佳实践+源代码+文档说明

    + [actuator应用监控](#actuator) + [lombok](#lombok) + [baseEntity](#baseEntity) + [统一响应返回值](#result) + [异常](#exception) + [数据校验](#validation) + [日志](#log) + [swagger](#swagger) + ...

    springBoot实战4.0 高清版

    7.3 通过 JMX 监控应用程序 ....................... 126 7.4 定制 Actuator ......................................... 128 7.4.1 修改端点 ID ............................... 128 7.4.2 启用和禁用端点 ..........

    spring boot 全面的样例代码

    - [在传统Spring应用中使用spring-boot-actuator模块提供监控端点](http://blog.didispace.com/spring-boot-actuator-without-boot/) - [Spring Boot应用的后台运行配置]...

    spring-boot-starter.xmind

    所有的 spring-boot-starter 都有约定俗成的默认配置,但允许我们调整这些配置以改变默认...spring-boot-starter-actuator与应用监控 资料是spring-boot-starter 常用模块进行详细内容的思维导图,整理好的,望采纳。

    Spring Boot Admin:用于管理 Spring Boot 应用程序的管理 UI-开源

    这个社区项目为 Spring Boot 应用程序提供了一个管理界面。 应用程序向我们的 Spring Boot Admin Client 注册(通过 HTTP)或使用 Spring Cloud(例如 Eureka、Consul)发现。 UI 只是一个位于 Spring Boot Actuator...

Global site tag (gtag.js) - Google Analytics