首页 > 编程语言 >Java服务端服务监控:Prometheus与Spring Boot Actuator的集成

Java服务端服务监控:Prometheus与Spring Boot Actuator的集成

时间:2024-09-02 16:48:09浏览次数:6  
标签:Spring Boot Prometheus 监控 import public

Java服务端服务监控:Prometheus与Spring Boot Actuator的集成

大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!

在现代Java服务端开发中,服务监控是确保系统稳定性和性能的关键。Prometheus是一个开源的系统监控和警报工具,而Spring Boot Actuator提供了生产级别的监控功能。将两者集成可以为Java应用提供强大的监控能力。本文将介绍如何将Prometheus与Spring Boot Actuator集成,以及如何配置和使用它们进行服务监控。

1. 添加依赖

首先,需要在Spring Boot项目中添加Prometheus和Actuator的依赖。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>prometheus-client-spring-boot</artifactId>
        <version>0.10.0</version>
    </dependency>
</dependencies>

2. 配置Prometheus

在Spring Boot应用中配置Prometheus,以暴露监控指标。

import cn.juwatech.config.PrometheusConfig;
import org.springframework.context.annotation.Configuration;
import io.prometheus.client.exporter.common.TextFormat;
import io.prometheus.client.hotspot.DefaultExports;
import io.prometheus.client.spring.boot.PrometheusMetricsExportAutoConfiguration;

@Configuration
public class PrometheusConfiguration extends PrometheusMetricsExportAutoConfiguration.MetricsExportConfiguration {

    @Override
    public void configureMetricsExport(io.prometheus.client.exporter.MetricsServlet metricsServlet) {
        super.configureMetricsExport(metricsServlet);
        metricsServlet.setServletPath("/metrics");
    }

    @Override
    public void configureDefaultExports() {
        super.configureDefaultExports();
        DefaultExports.initialize();
    }
}

3. 集成Spring Boot Actuator

Spring Boot Actuator提供了多种监控端点,可以与Prometheus集成以暴露这些端点。

import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.actuate.autoconfigure.web.server.WebManagementContextResolver;
import org.springframework.boot.actuate.endpoint.web.WebEndpointFilter;

@Configuration
@WebManagementContextResolver
public class ActuatorConfiguration extends PrometheusMetricsExportAutoConfiguration {

    @Override
    public void configureWebEndpointFilters(WebEndpointFilter[] filters) {
        super.configureWebEndpointFilters(filters);
    }
}

4. 定义自定义指标

除了内置的监控指标,我们还可以定义自定义指标来满足特定的监控需求。

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import org.springframework.stereotype.Component;

@Component
public class CustomMetrics {

    private static final Counter requestsCounter = Counter.build()
            .name("my_requests_total")
            .help("Total requests.")
            .register();

    private static final Gauge responseSizeGauge = Gauge.build()
            .name("my_response_size_bytes")
            .help("Response size in bytes.")
            .register();

    public void recordRequest(int responseSize) {
        requestsCounter.inc();
        responseSizeGauge.set(responseSize);
    }
}

5. 使用自定义指标

在应用中使用自定义指标来记录监控数据。

import cn.juwatech.service.MyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    private final CustomMetrics customMetrics;
    private final MyService myService;

    public MyController(CustomMetrics customMetrics, MyService myService) {
        this.customMetrics = customMetrics;
        this.myService = myService;
    }

    @GetMapping("/my-service")
    public String myServiceEndpoint() {
        String response = myService.performAction();
        int responseSize = response.getBytes().length;
        customMetrics.recordRequest(responseSize);
        return response;
    }
}

6. 配置Prometheus服务器

配置Prometheus服务器以抓取Spring Boot应用暴露的监控指标。

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'spring-boot-application'
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:8080']

7. 可视化与告警

使用Prometheus的可视化工具,如Grafana,来展示监控数据,并设置告警规则。

import cn.juwatech.monitor.PrometheusAlertManager;

public class MonitoringAndAlerting {
    public static void main(String[] args) {
        PrometheusAlertManager alertManager = new PrometheusAlertManager();
        alertManager.setAlertRule("my_requests_total > 100");
        alertManager.startMonitoring();
    }
}

通过上述步骤,我们可以将Prometheus与Spring Boot Actuator集成,实现对Java服务端应用的监控。这种集成提供了强大的监控能力,帮助我们及时发现和解决潜在的性能问题。

本文著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!

标签:Spring,Boot,Prometheus,监控,import,public
From: https://www.cnblogs.com/szk123456/p/18392984

相关文章

  • Java服务端服务监控:Spring Boot Actuator的实践
    Java服务端服务监控:SpringBootActuator的实践大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在微服务架构中,服务监控是确保系统健康运行的关键。SpringBootActuator提供了一系列的监控和管理功能,使得开发者能够更好地监控和管理SpringBoot应用......
  • springboot 编程事务的封装
    一、创建事务管理工具类java复制importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importorg.springframework.transaction.PlatformTransactionManager;importorg.springframework.transaction.Transact......
  • 基于SpringBoot+MySQL+SSM+Vue.js的学生选课系统
    获取见最下方名片获取见最下方名片获取见最下方名片演示视频基于SpringBoot+MySQL+SSM+Vue.js的学生选课系统(附论文)技术描述开发工具:Idea/Eclipse数据库:MySQLJar包仓库:Maven前端框架:Vue/ElementUI后端框架:Spring+SpringMVC+Mybatis+SpringBoot文字描......
  • Spring框架之IOC介绍
    Spring之IOC简介首先,官网中有这样一句话:SpringFrameworkimplementationoftheInversionofControl(IoC)principle.这句话翻译过来就是:Spring实现控制反转(IOC)原理,由此可以得出,InversionofControl(IOC)是一个名为控制反转的原理,而Spring实现了他。而实现这个原理或者说设......
  • SpringCloud Gateway鉴权
    参考:https://blog.csdn.net/weixin_43296313/article/details/121126811基于从前的项目:https://www.cnblogs.com/xsj1989/p/18350213在网关项目下创建全局过滤器packagecom.xcg.filters;importcom.auth0.jwt.interfaces.Claim;importcom.auth0.jwt.interfaces.DecodedJWT;......
  • Java服务端服务编排:Spring Boot与Spring Cloud的整合
    Java服务端服务编排:SpringBoot与SpringCloud的整合大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在微服务架构中,服务编排是一个关键的环节,它涉及到服务的协调、管理和监控。SpringBoot和SpringCloud是Java生态中广泛使用的框架,它们提供了强大的......
  • H5 Morvin高颜值后台管理模板、html5+bootstrap5后台管理前端模板网站模板
    介绍推荐一个高颜值的应用模板,Morvin是一个基于Bootstrap5实现的后台管理系统模板。基于简单的和模块化的设计,这使得它很容易定制。这套后台模板有大量的可重用的和漂亮的UI元素,小部件等。帮助你的团队移动更快,节约开发成本,可以创建任何网站的后台数据管理,或者WEB应用系统的界......
  • springboot足球社区管理系统
    指南......
  • springboot基于Hadoop的物品租赁系统的设计与实现
    指南......
  • springboot大学生选修选课系统的设计与实现
    指南......