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

Actuator 监控实验

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

Actuator 监控实验

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

添加依赖:


	org.springframework.boot
	>spring-boot-starter-actuator

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 getMySystemInfo() {
        Map result = new HashMap<>();
        Map map = System.getenv();
        result.put("computername", map.get("COMPUTERNAME"));
        return result;
    }
}

5. 监控

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


    io.micrometer
    >micrometer-registry-prometheus

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

@Configuration
public class ActuatorDemoApplication {
    @Bean
    MeterRegistryCustomizer 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=` 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

复制文件夹 安装目录 grafanagrafana-8.5.1conf下的sample.ini,并重命名为custom.ini,修改口号为9900

双击安装目录下的 grafanagrafana-8.5.1bingrafana-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 监控

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

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

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