Actuator 实验

Actuator通过一系列HTTP端点提供监控能力。本文首先尝试了定义已有的endpoint,以及自定义endpoint,并实验通过prometheus对暴露的endpoint进行数据收集,Grafana进行数据展示。

添加依赖:

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

1. 端点

应用配置类端点
/beans获取应用程序中所创建的所有JavaBean信息
/info返回应用自定义信息
度量指标类端点
/health获取应用的各类健康指标信息

2. 访问

HTTP访问

/actuator/

端口与路径

management.server.address

management.server.port //用于将actuator端口与应用程序端口分开,避免外部访问

暴露Endpoint

management.endpoint.web.exposure.exclude=

management.endpoint.web.exposure.include= info, health

3. 自定义已有endpoint显示信息

定义类并使其实现HealthIndicator接口,重写health()方法

package com.abc.healthcheck;

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

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        try {
            URL url = new URL("http://localhost:8080/health/");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            int responseCode = conn.getResponseCode();
            if (responseCode >= 200 && responseCode < 300) {
                return Health.up().build();
            }else {
                return Health.down().withDetail("HTTP Status Code", responseCode).build();
            }
        } catch (IOException e) {
            e.printStackTrace();
            Health.down().withDetail("HTTP Status Code", e.getMessage()).build();
        }
        return Health.down().withDetail("HTTP Status Code", "Unknown").build();
    }
}

4. 自定义endpoint

package com.abc.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Endpoint(id = "computername", enableByDefault = true)
public class CustomEndpoint {
    @ReadOperation
    public Map<String, Object> getMySystemInfo() {
        Map<String, Object> result = new HashMap<>();
        Map<String, String> map = System.getenv();
        result.put("computername", map.get("COMPUTERNAME"));
        return result;
    }
}

5. 监控

使用micrometer,对接prometheus实现监控功能 , 引入依赖,spring boot会自动装配相关bean

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

为了给Grafana(监控仪表盘)展示,需要定义如下bean

@Configuration
public class ActuatorDemoApplication {
    @Bean
    MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> registry.config().commonTags("application", "actuator-demo");
    }
}

5.1 数据获取 - prometheus

架构图

在这里插入图片描述

工作流程

  1. prometheus server可以定期从活跃的目标主机上拉取监控指标数据;也可通过pushgateway把采集的数据上报到prometheus server中;还可通过一些组件自带的exporter采集相应组件的数据
  2. prometheus server把采集到的监控指标数据保存到本地磁盘或者数据库;
  3. Prometheus采集的监控指标数据按时间序列存储,通过配置报警规则,把触发的报警发发送到alertmanager
  4. Alertmanager通过配置报警接收方,发送警报到邮件,微信或钉钉等
  5. prometheus自带的web ui提供PromQL查询语言,可查询监控数据
  6. Grafana可接入prometheus数据源,把监控数据以图形化形式展示出
配置prometheus

配置job用于拉取监控端点的数据

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "actuator-demo"
    metrics_path: "actuator/prometheus"
    static_configs:
      - targets: ["localhost:8080"]

启动prometheus,后输入url: localhost:9090

在这里插入图片描述

5.2 数据展示 - Grafana

5.2.1 下载安装Grafana

官网:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1&platform=windows

复制文件夹 安装目录 \grafana\grafana-8.5.1\conf下的sample.ini,并重命名为custom.ini,修改口号为9900
在这里插入图片描述
双击安装目录下的 \grafana\grafana-8.5.1\bin\grafana-server.exe 启动Grafana
登录:admin/admin

5.2.2 创建数据源prometheus

在这里插入图片描述

5.2.3 Importing dashboard

import dashboard from grafana.com using id: 4701 (https://grafana.com/grafana/dashboards/4701) and data sources created above

5.2.4 JVM Metrics

I/O & Memory & CPU & Threads
在这里插入图片描述

Eden space& Old Gen & Survior space & Metaspace & GC Time
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值