前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Spring Boot开发:从0到1》第11章 Spring Boot应用监控第11章 Spring Boot Actuator与应用监控

《Spring Boot开发:从0到1》第11章 Spring Boot应用监控第11章 Spring Boot Actuator与应用监控

作者头像
一个会写诗的程序员
发布2018-08-20 10:02:59
3610
发布2018-08-20 10:02:59
举报

第11章 Spring Boot Actuator与应用监控

Spring Boot的Actuator 将应用的很多信息暴露出来,如容器中的 bean,自动配置时的决策,运行时健康状态, metrics等等。Actuator提供了三中方式获取这些信息:

HTTP Endpoints Remote Shell JMX (MBeans) 当然通过继承指定的类来自定义一些 actuator信息,暴露自己想要的信息。

11.1 使用Spring Boot Actuator监控应用

1.Spring boot Actuator Endpoints介绍

Actuator是Spring Boot提供的附加特性,来帮我们监控和管理生产环境下运行时的应用程序。我们可以通过HTTP endpoints、JMX或者SSH来监控和管理应用的健康状况、系统指标、参数信息、内存状况等等。

Spring Boot Actuator所提供的HTTP监控服务如下表:

上面的这些HTTP服务,我们就叫Endpoint。Endpoint允许对应用进行上述健康状况、系统指标、参数信息、内存状况等指标的监控和交互。Spring Boot提供了很多内置的Endpoint,同时支持定制Endpoint。

Endpoint被暴露的方式取决于采用的技术(HTTP、JMX、SSH等),大部分应用采用HTTP的方式, 暴露的方式即通过Endpoint的ID映射成一个URL,例如 id=health 的内置Endpoint映射到URL=/health, 提供应用基础健康检查信息, 为了安全起见,一般不暴露在应用服务端口上,而是暴露在专门的管理端口上。

其中,重点挑两个讲一下。

/health 提供应用程序的健康状态,或者是某个核心模块的健康状态。例如, 数据库连接,磁盘使用情况等指标。

/metrics,这个endpoint显示Metrics 子系统管理的信息。主要是一些度量的值,比如系统的吞吐量,堆栈信息,耗时(timer),接口被触发的次数(meter,count),对象大小(gauge)等。metrics的监控主要分以下几种类型:

2.开启Actuator 很简单,只需要引入官方提供的starter:

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

3.定制Endpoint

Endpoint可以通过application.properties配置文件中的配置项进行定制, 格式如下:

endpoints.[name].[property]

有三个通用的property:

id: id enable: 开关 sensitive: 是否需要权限控制才可以看到 以health为例,/health暴露的监控信息是所有实现了HealthIndicator接口的Bean。

通过自定义HealthIndicator实现定制health endpoint

@Component
public class HealthCheck implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    public int check() {
        // Your logic to check health
        return 0;
    }
}

输出类似如下格式:

{
    "status" : "DOWN",
    "myHealthCheck" : {
        "status" : "DOWN",
        "Error Code" : 1,
        "Description" : "You custom MyHealthCheck endpoint is down"
     },
     "diskSpace" : {
         "status" : "UP",
         "free" : 209047318528,
         "threshold" : 10485760
     }
}

4.创建新的Endpoint

一般通过继承AbstractEndpoint<T>抽象类创建一个新的Endpoint, 或者直接实现Endpoint<T>接口,AbstractEndpoint<T>抽象类也实现了Endpoint<T>接口。

@Component
public class CustomEndpoint implements Endpoint<List<String>> {

    public String getId() {
        return "customEndpoint";
    }

    public boolean isEnabled() {
        return true;
    }

    public boolean isSensitive() {
        return true;
    }

    public List<String> invoke() {
        // Custom logic to build the output
        List<String> messages = new ArrayList<String>();
        messages.add("This is message 1");
        messages.add("This is message 2");
        return messages;
    }
}

id=customEndpoint, 对应的URL为/customEndpoint

输出信息格式如下:

[ "This is message 1", "This is message 2" ]

11.2 Spring Boot远程Shell

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.04.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第11章 Spring Boot Actuator与应用监控
    • 11.1 使用Spring Boot Actuator监控应用
      • 11.2 Spring Boot远程Shell
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档