关于prometheus:Prometheus基于Eureka的服务发现

5次阅读

共计 2022 个字符,预计需要花费 6 分钟才能阅读完成。

一、背景

目前咱们的我的项目是应用 Spring Cloud 构建的微服务,应用的是 Eureka 作为注册核心,且我的项目中应用到 Prometheus 做服务监控。此处简略记录一下 eureka 作为 prometheus 的服务发现。

二、实现步骤

1、eureka 客户端注册到 prometheus 中

批改 application.yml 文件


spring:
  application:
    name: order-provider-10004
server:
  port: 10004

management:
  endpoints:
    web:
      exposure:
        include: 'prometheus,metrics,info,health' # 暴露出 prometheus 端口
  metrics:
    tags:
      application: ${spring.application.name} # 减少每个指标的全局的 tag,及给每个指标一个 application 的 tag, 值是 spring.application.name 的值
  server:
    port: 10005

# 注册到 eureka 上
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10003/eureka/   #连贯到服务注册核心的地址,如果服务注册核心开启了权限须要设置 http://username:password@ip:port/eureka/ 格局
  instance:
    prefer-ip-address: true
    metadata-map:
      # 集成到 prometheus, 以下的这些数据都会退出到 prometheus 的从新标记之前的标签中,比方 sys.module 会变成
      "prometheus.scrape": "true"
      "prometheus.path": "/actuator/prometheus"
      "prometheus.port": "${management.server.port}"
      "sys.module": "order"

次要是上方 metadata-map 中的配置。

2、prometheus 中的写法

批改 prometheus.yml 文件

  - job_name: 'eureka'
    eureka_sd_configs:
    # 指定 eureka 的服务发现地址
      - server: 'http://localhost:10003/eureka'
    relabel_configs:
    # 重写 metrics 的门路
      - source_labels: ["__meta_eureka_app_instance_metadata_prometheus_path"]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
        # 减少一个自定义 label sys_model 它的值从配置 eureka 中获取
      - source_labels: ["__meta_eureka_app_instance_metadata_sys_module"]
        action: replace
        target_label: sys_module
        regex: (.+)
        # 重写治理端口
      - source_labels: [__address__, __meta_eureka_app_instance_metadata_prometheus_port]
        action: replace
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
        target_label: __address__

1、重写 metrics 的 path。
2、重写 治理端口。
3、减少一个自定义的标签。
实现成果查看下方的效果图。

最终实现的抓取端口的门路 http://10.1.129.254:10005/actuator/prometheus

留神:
1、/actuator/prometheus 由上方的 metrics_path 重写。
2、10.1.129.254:10005 由上方的 address 重写。

3、实现成果

上图展现的短裤是 10004 而不是 10005,是因为我没有在 application.yml 中指定 management.server.port 时截的图。

三、残缺代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/prometheus/eureka-prometheus-parent

四、参考链接

1、https://github.com/prometheus/prometheus/blob/release-2.25/documentation/examples/prometheus-eureka.yml
2、https://prometheus.io/docs/prometheus/latest/configuration/configuration/#eureka_sd_config

正文完
 0