当前位置: 代码网 > it编程>编程语言>Java > SpringBoot Admin与Prometheus集成监控

SpringBoot Admin与Prometheus集成监控

2026年01月23日 Java 我要评论
摘要本文深入探讨spring boot admin与prometheus的集成监控方案,包括指标收集、数据导出、可视化展示等关键技术点。通过详细的技术解析和实践示例,帮助开发者构建完整的微服务监控体系

摘要

本文深入探讨spring boot admin与prometheus的集成监控方案,包括指标收集、数据导出、可视化展示等关键技术点。通过详细的技术解析和实践示例,帮助开发者构建完整的微服务监控体系。

1. 引言

prometheus作为开源的系统监控和报警工具包,已经成为云原生监控的标准。spring boot admin结合prometheus可以提供更强大的监控能力。本文将详细介绍如何实现两者的集成。

2. prometheus基础

2.1 prometheus架构

prometheus生态系统包含以下组件:

  • prometheus server:收集和存储指标数据
  • client libraries:用于应用代码中暴露指标
  • pushgateway:支持批处理作业的指标推送
  • alertmanager:处理报警
  • 可视化工具:如grafana

2.2 数据模型

prometheus使用时间序列数据模型,每个时间序列由指标名称和键值对标签标识。

3. 集成配置

3.1 依赖配置

<dependencies>
    <!-- spring boot admin server -->
    <dependency>
        <groupid>de.codecentric</groupid>
        <artifactid>spring-boot-admin-starter-server</artifactid>
    </dependency>
    
    <!-- spring boot actuator -->
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-actuator</artifactid>
    </dependency>
    
    <!-- micrometer prometheus registry -->
    <dependency>
        <groupid>io.micrometer</groupid>
        <artifactid>micrometer-registry-prometheus</artifactid>
    </dependency>
    
    <!-- spring boot starter web -->
    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
</dependencies>

3.2 应用配置

# application.yml
server:
  port: 8022

spring:
  application:
    name: admin-server-prometheus

# actuator配置
management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics,prometheus
      base-path: /actuator
  endpoint:
    health:
      show-details: always
    prometheus:
      enabled: true
  metrics:
    export:
      prometheus:
        enabled: true
    distribution:
      percentiles-histogram:
        all: true
      sla:
        http.server.requests: 10ms, 50ms, 100ms, 200ms, 500ms, 1000ms

# spring boot admin配置
boot:
  admin:
    server:
      ui:
        title: 'prometheus集成监控平台'
        brand: '<img src="assets/img/icon-spring-boot-admin.svg"><span>prometheus集成监控平台</span>'

info:
  app:
    name: ${spring.application.name}
    version: ${project.version}
    description: spring boot admin与prometheus集成示例

4. 指标收集与暴露

4.1 自定义指标收集器

package com.springboot.admin.prometheus;

import io.micrometer.core.instrument.counter;
import io.micrometer.core.instrument.gauge;
import io.micrometer.core.instrument.meterregistry;
import io.micrometer.core.instrument.binder.meterbinder;
import org.springframework.stereotype.component;

@component
public class custommetricscollector implements meterbinder {
    
    private counter requestcounter;
    private counter errorcounter;
    
    @override
    public void bindto(meterregistry registry) {
        // 请求计数器
        requestcounter = counter.builder("http_requests_total")
            .description("total number of http requests")
            .register(registry);
            
        // 错误计数器
        errorcounter = counter.builder("http_errors_total")
            .description("total number of http errors")
            .register(registry);
            
        // 系统指标
        gauge.builder("system_cpu_usage")
            .description("system cpu usage")
            .register(registry, this, obj -> getsystemcpuusage());
            
        gauge.builder("system_memory_usage")
            .description("system memory usage")
            .register(registry, this, obj -> getsystemmemoryusage());
    }
    
    public void incrementrequest() {
        if (requestcounter != null) {
            requestcounter.increment();
        }
    }
    
    public void incrementerror() {
        if (errorcounter != null) {
            errorcounter.increment();
        }
    }
    
    private double getsystemcpuusage() {
        // 获取系统cpu使用率
        return math.random() * 100; // 模拟数据
    }
    
    private double getsystemmemoryusage() {
        // 获取系统内存使用率
        long totalmemory = runtime.getruntime().totalmemory();
        long freememory = runtime.getruntime().freememory();
        return (double) (totalmemory - freememory) / totalmemory * 100;
    }
}

4.2 业务指标收集

package com.springboot.admin.prometheus;

import io.micrometer.core.instrument.meterregistry;
import io.micrometer.core.instrument.tag;
import io.micrometer.core.instrument.timer;
import org.springframework.stereotype.service;

import java.util.concurrent.timeunit;

@service
public class businessmetricsservice {
    
    private final meterregistry meterregistry;
    private final timer businessoperationtimer;
    
    public businessmetricsservice(meterregistry meterregistry) {
        this.meterregistry = meterregistry;
        
        // 业务操作计时器
        this.businessoperationtimer = timer.builder("business.operation.duration")
            .description("business operation duration")
            .register(meterregistry);
    }
    
    public <t> t recordbusinessoperation(string operationname, java.util.function.supplier<t> operation) {
        return businessoperationtimer
            .tag("operation", operationname)
            .recordcallable(operation::get);
    }
    
    public void recordbusinessevent(string eventtype, string service) {
        meterregistry.counter("business.events.total", 
            tag.of("type", eventtype),
            tag.of("service", service))
            .increment();
    }
    
    public void recordbusinessgauge(string name, double value, string service) {
        meterregistry.gauge("business." + name, 
            tag.of("service", service), 
            value);
    }
}

5. prometheus配置

5.1 prometheus配置文件

# prometheus.yml
global:
  scrape_interval: 15s
  evaluation_interval: 15s

rule_files:
  - "alert_rules.yml"

scrape_configs:
  - job_name: 'admin-server'
    static_configs:
      - targets: ['localhost:8022']
    metrics_path: '/actuator/prometheus'
    scrape_interval: 10s
    scrape_timeout: 5s
    honor_labels: true
    relabel_configs:
      - source_labels: [__address__]
        target_label: instance

  - job_name: 'microservices'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 10s
    scrape_timeout: 5s
    static_configs:
      - targets: ['service1:8080', 'service2:8081', 'service3:8082']
    relabel_configs:
      - source_labels: [__address__]
        target_label: instance

alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']

5.2 告警规则配置

# alert_rules.yml
groups:
  - name: admin_server_alerts
    rules:
      - alert: adminserverdown
        expr: up{job="admin-server"} == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "admin server is down"
          description: "admin server {{ $labels.instance }} has been down for more than 1 minute."

      - alert: highcpuusage
        expr: system_cpu_usage > 80
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "high cpu usage on {{ $labels.instance }}"
          description: "cpu usage is above 80% on instance {{ $labels.instance }}."

      - alert: highmemoryusage
        expr: system_memory_usage > 85
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "high memory usage on {{ $labels.instance }}"
          description: "memory usage is above 85% on instance {{ $labels.instance }}."

      - alert: higherrorrate
        expr: rate(http_errors_total[5m]) > 10
        for: 2m
        labels:
          severity: warning
        annotations:
          summary: "high error rate on {{ $labels.instance }}"
          description: "error rate is above 10 requests per minute on instance {{ $labels.instance }}."

6. 数据导出与格式化

6.1 自定义指标格式化

package com.springboot.admin.prometheus;

import io.micrometer.core.instrument.meterregistry;
import io.micrometer.prometheus.prometheusmeterregistry;
import org.springframework.stereotype.component;

@component
public class prometheuscustomformatter {
    
    public void customizeprometheusregistry(prometheusmeterregistry registry) {
        // 自定义指标格式化
        registry.config()
            .meterfilter(new io.micrometer.core.instrument.config.meterfilter() {
                @override
                public io.micrometer.core.instrument.meter.id map(io.micrometer.core.instrument.meter.id id) {
                    // 添加自定义标签
                    if (id.getname().startswith("http.")) {
                        return id.withtags(io.micrometer.core.instrument.tag.of("application", "admin-server"));
                    }
                    return id;
                }
            });
    }
}

6.2 指标数据处理

package com.springboot.admin.prometheus;

import io.micrometer.core.instrument.meterregistry;
import io.micrometer.core.instrument.tag;
import org.springframework.stereotype.component;

import java.util.concurrent.concurrenthashmap;
import java.util.concurrent.atomic.atomiclong;

@component
public class metricsprocessor {
    
    private final meterregistry meterregistry;
    private final concurrenthashmap<string, atomiclong> customcounters = new concurrenthashmap<>();
    
    public metricsprocessor(meterregistry meterregistry) {
        this.meterregistry = meterregistry;
    }
    
    public void incrementcustomcounter(string name, string... tags) {
        string key = buildkey(name, tags);
        atomiclong counter = customcounters.computeifabsent(key, k -> new atomiclong(0));
        long value = counter.incrementandget();
        
        // 同步到prometheus
        meterregistry.counter("custom." + name, 
            buildtags(tags))
            .increment();
    }
    
    public void recordcustomgauge(string name, double value, string... tags) {
        meterregistry.gauge("custom." + name, 
            java.util.arrays.aslist(buildtags(tags)), 
            value);
    }
    
    private string buildkey(string name, string... tags) {
        stringbuilder key = new stringbuilder(name);
        for (string tag : tags) {
            key.append(".").append(tag);
        }
        return key.tostring();
    }
    
    private iterable<tag> buildtags(string... tags) {
        java.util.list<tag> taglist = new java.util.arraylist<>();
        for (int i = 0; i < tags.length; i += 2) {
            if (i + 1 < tags.length) {
                taglist.add(tag.of(tags[i], tags[i + 1]));
            }
        }
        return taglist;
    }
}

7. 监控面板集成

7.1 grafana配置

{
  "dashboard": {
    "id": null,
    "title": "admin server dashboard",
    "tags": ["admin", "spring-boot", "prometheus"],
    "timezone": "browser",
    "panels": [
      {
        "id": 1,
        "title": "http requests",
        "type": "graph",
        "targets": [
          {
            "expr": "rate(http_requests_total[1m])",
            "legendformat": "requests per second"
          }
        ]
      },
      {
        "id": 2,
        "title": "system cpu usage",
        "type": "singlestat",
        "targets": [
          {
            "expr": "system_cpu_usage",
            "legendformat": "cpu usage"
          }
        ]
      },
      {
        "id": 3,
        "title": "system memory usage",
        "type": "graph",
        "targets": [
          {
            "expr": "system_memory_usage",
            "legendformat": "memory usage"
          }
        ]
      }
    ]
  }
}

7.2 监控数据api

package com.springboot.admin.api;

import io.micrometer.core.instrument.meterregistry;
import io.micrometer.prometheus.prometheusmeterregistry;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.http.responseentity;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
@requestmapping("/api/metrics")
public class metricsapicontroller {
    
    @autowired
    private meterregistry meterregistry;
    
    @autowired
    private prometheusmeterregistry prometheusregistry;
    
    @getmapping("/prometheus")
    public responseentity<string> getprometheusmetrics() {
        return responseentity.ok(prometheusregistry.scrape());
    }
    
    @getmapping("/summary")
    public responseentity<metricssummary> getmetricssummary() {
        metricssummary summary = new metricssummary();
        
        // 获取各种指标的摘要信息
        summary.settotalmeters(meterregistry.getmeters().size());
        summary.setgauges(meterregistry.find("gauge").meters().size());
        summary.setcounters(meterregistry.find("counter").meters().size());
        summary.settimers(meterregistry.find("timer").meters().size());
        
        return responseentity.ok(summary);
    }
    
    public static class metricssummary {
        private int totalmeters;
        private int gauges;
        private int counters;
        private int timers;
        
        // getter和setter方法
        public int gettotalmeters() { return totalmeters; }
        public void settotalmeters(int totalmeters) { this.totalmeters = totalmeters; }
        public int getgauges() { return gauges; }
        public void setgauges(int gauges) { this.gauges = gauges; }
        public int getcounters() { return counters; }
        public void setcounters(int counters) { this.counters = counters; }
        public int gettimers() { return timers; }
        public void settimers(int timers) { this.timers = timers; }
    }
}

8. 高级功能

8.1 指标过滤与转换

package com.springboot.admin.prometheus;

import io.micrometer.core.instrument.config.meterfilter;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
public class metricsfilterconfig {
    
    @bean
    public meterfilter metricsfilter() {
        return meterfilter.maximumallowablemetrics(1000)
            .andthen(meterfilter.maximumallowabletags("http.server.requests", "uri", 100))
            .andthen(meterfilter.denyunless(meter -> 
                meter.getname().startswith("http.") || 
                meter.getname().startswith("jvm.") ||
                meter.getname().startswith("system.") ||
                meter.getname().startswith("tomcat.")
            ));
    }
    
    @bean
    public meterfilter namingconventionfilter() {
        return meterfilter.renametag("http.server.requests", "method", "httpmethod")
            .andthen(meterfilter.renametag("http.server.requests", "uri", "httpuri"));
    }
}

8.2 指标聚合

package com.springboot.admin.prometheus;

import io.micrometer.core.instrument.meterregistry;
import io.micrometer.core.instrument.tag;
import io.micrometer.core.instrument.timer;
import org.springframework.stereotype.component;

import java.util.concurrent.concurrenthashmap;
import java.util.concurrent.timeunit;

@component
public class metricsaggregator {
    
    private final meterregistry meterregistry;
    private final concurrenthashmap<string, timer.sample> activeoperations = new concurrenthashmap<>();
    
    public metricsaggregator(meterregistry meterregistry) {
        this.meterregistry = meterregistry;
    }
    
    public void startoperation(string operationname, string service) {
        timer.sample sample = timer.start(meterregistry);
        string key = operationname + ":" + service;
        activeoperations.put(key, sample);
    }
    
    public void endoperation(string operationname, string service) {
        string key = operationname + ":" + service;
        timer.sample sample = activeoperations.remove(key);
        if (sample != null) {
            sample.stop(timer.builder("operation.duration")
                .description("operation duration")
                .tags(tag.of("operation", operationname), tag.of("service", service))
                .register(meterregistry));
        }
    }
    
    public void recordhistogram(string metricname, double value, string... tags) {
        meterregistry.timer(metricname, buildtags(tags))
            .record((long) value, timeunit.milliseconds);
    }
    
    private iterable<tag> buildtags(string... tags) {
        java.util.list<tag> taglist = new java.util.arraylist<>();
        for (int i = 0; i < tags.length; i += 2) {
            if (i + 1 < tags.length) {
                taglist.add(tag.of(tags[i], tags[i + 1]));
            }
        }
        return taglist;
    }
}

9. 部署配置

9.1 docker配置

# dockerfile
from openjdk:8-jre-alpine

# 创建应用目录
run addgroup -s spring && adduser -s spring -g spring
user spring:spring
workdir /app

# 复制应用文件
copy --chown=spring:spring target/admin-server-prometheus.jar app.jar

# 暴露端口
expose 8022

# 启动命令
entrypoint ["java", "-jar", "app.jar"]

9.2 docker compose配置

# docker-compose.yml
version: '3.8'

services:
  admin-server:
    build: .
    container_name: admin-server-prometheus
    ports:
      - "8022:8022"
    environment:
      - spring_profiles_active=prometheus
    networks:
      - monitoring

  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
    networks:
      - monitoring

  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    environment:
      - gf_security_admin_password=admin
    volumes:
      - grafana-storage:/var/lib/grafana
    networks:
      - monitoring
    depends_on:
      - prometheus

volumes:
  grafana-storage:

networks:
  monitoring:
    driver: bridge

10. 性能优化

10.1 指标收集优化

package com.springboot.admin.optimization;

import io.micrometer.core.instrument.config.meterfilter;
import io.micrometer.core.instrument.distribution.distributionstatisticconfig;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

@configuration
public class metricsoptimizationconfig {
    
    @bean
    public meterfilter metricsfilter() {
        return meterfilter.maximumallowablemetrics(1000)
            .andthen(meterfilter.denyunless(meter -> 
                meter.getname().startswith("http.") || 
                meter.getname().startswith("jvm.") ||
                meter.getname().startswith("system.")
            ));
    }
    
    @bean
    public meterfilter distributionconfigfilter() {
        return new meterfilter() {
            @override
            public distributionstatisticconfig configure(io.micrometer.core.instrument.meter.id id, 
                                                        distributionstatisticconfig config) {
                if (id.getname().startswith("http.server.requests")) {
                    return distributionstatisticconfig.builder()
                        .percentiles(0.5, 0.95, 0.99)
                        .build()
                        .merge(config);
                }
                return config;
            }
        };
    }
}

11. 总结

本文详细介绍了spring boot admin与prometheus的集成监控方案,包括:

  1. prometheus基础概念和架构
  2. 集成配置和依赖管理
  3. 指标收集与暴露
  4. prometheus配置和告警规则
  5. 数据导出与格式化
  6. 监控面板集成
  7. 高级功能实现
  8. 部署配置方案
  9. 性能优化策略

通过合理的集成配置,可以构建一个功能完善的微服务监控体系,为系统运维提供有力支持。

12. 扩展阅读

13. 参考资料

  1. prometheus github: https://github.com/prometheus/prometheus
  2. micrometer github: https://github.com/micrometer-metrics/micrometer
  3. spring boot admin github: https://github.com/codecentric/spring-boot-admin

本文为csdn博主「spring cloud技术栈」原创文章,转载请附上原文出处链接和本声明。

到此这篇关于springboot admin与prometheus集成监控的文章就介绍到这了,更多相关springboot prometheus监控内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com