spring cloud 集成 prometheus 源码分析

说明

本文针对prometheus官方的spring cloud集成client
maven 依赖如下:

<dependency>
            <groupId>io.prometheus</groupId>
            <artifactId>simpleclient_spring_boot</artifactId>
            <version>0.0.26</version>
        </dependency>

Metrics 输出

首先从metrics输出入手,http://localhost:8080/prometheus
在工程中使用注解 @EnablePrometheusEndpoint 的时候,程序启用配置,通过注册MvcEndpoint,暴露出访问路径为”prometheus”的http请求服务.
关键代码片段:
注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({PrometheusEndpointConfiguration.class})
public @interface EnablePrometheusEndpoint {
   
}

配置类,配置PrometheusMvcEndpoint

@Configuration
class PrometheusEndpointConfiguration {
    PrometheusEndpointConfiguration() {
    }

    @Bean
    public PrometheusEndpoint prometheusEndpoint() {
        return new PrometheusEndpoint(CollectorRegistry.defaultRegistry);
    }

    @Bean
    &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个用于构建分布式系统的开发工具包,而Prometheus是一个开源的监控和警报系统。它们可以结合使用,以实现对Spring Cloud应用程序的监控和指标收集。 在Spring Cloud中,你可以使用Spring Boot Actuator模块来暴露应用程序的运行时指标。这些指标包括HTTP请求的计数、错误率、内存使用情况等。Prometheus可以通过轮询这些指标的端点来收集数据,并将其存储在自己的时间序列数据库中。 要将Spring CloudPrometheus集成,你需要做以下几个步骤: 1. 添加Prometheus依赖:在你的Spring Boot项目中,添加Prometheus相关的依赖,如`micrometer-registry-prometheus`。 2. 配置Prometheus端点:在`application.properties`或`application.yml`文件中,配置Spring Boot Actuator的端点路径和Prometheus的路径。 3. 启用Prometheus收集器:通过添加`@EnablePrometheusEndpoint`注解,启用Prometheus收集器。 4. 配置Prometheus收集器:通过添加`@EnableSpringBootMetricsCollector`注解,配置Prometheus收集器的行为。 5. 启动Prometheus服务器:在你的环境中启动Prometheus服务器,并配置它来定期抓取Spring Cloud应用程序的指标。 一旦配置完成,Prometheus就可以定期从你的Spring Cloud应用程序中收集指标,并将其存储在自己的数据库中。你可以使用Prometheus的查询语言(PromQL)来查询和分析这些指标,并创建自定义的监控仪表盘和警报规则。 希望这些信息能对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值