Prometheus service discovery based on Eureka

Time:2021-7-8

1、 Background

At present, our project is a microservice built with spring cloud, usingEurekaAs a registry, and used in the projectPrometheusDo service monitoring. Here is a brief record of Eureka’s service discovery as Prometheus.

2、 Implementation steps

1. The Eureka client is registered in Prometheus

Modify the application.yml file

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

management:
  endpoints:
    web:
      exposure:
        Include: 'Prometheus, metrics, info, health' #
  metrics:
    tags:
      Application: ${spring. Application. Name} increases the global tag of each indicator and gives each indicator an application tag with the value of spring. Application. Name
  server:
    port: 10005

#Register on Eureka
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:10003/eureka/    #Connect to the address of the service registry. If the service registry is enabled, it needs to be set http://username:password @IP: port / Eureka / format
  instance:
    prefer-ip-address: true
    metadata-map:
      #Integrated into Prometheus, the following data will be added to the tags before the Prometheus re tag, for example, sys. Module will become
      "prometheus.scrape": "true"
      "prometheus.path": "/actuator/prometheus"
      "prometheus.port": "${management.server.port}"
      "sys.module": "order"

Mainly abovemetadata-mapConfiguration in.

2. How to write in Prometheus

Modify the prometheus.yml file

- job_name: 'eureka'
    eureka_sd_configs:
    #Specify Eureka's service discovery address
      - server: 'http://localhost:10003/eureka'
    relabel_configs:
    #Path to rewrite metrics
      - source_labels: ["__meta_eureka_app_instance_metadata_prometheus_path"]
        action: replace
        target_label: __metrics_path__
        regex: (.+)
        #Add a custom label sys_ Its value is obtained from the configuration Eureka
      - source_labels: ["__meta_eureka_app_instance_metadata_sys_module"]
        action: replace
        target_label: sys_module
        regex: (.+)
        #Override management port
      - source_labels: [__address__, __meta_eureka_app_instance_metadata_prometheus_port]
        action: replace
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
        target_label: __address__

1. Rewrite the path of metrics.
2. Override the management port.
3. Add a custom label.
To achieve the effect, see the effect picture below.

The final implementation of the grab port pathhttp://10.1.129.254:10005/actuator/prometheus

be careful:
1、/actuator/prometheusFrom abovemetrics_pathrewrite.
2、10.1.129.254:10005From aboveaddressrewrite.

3. Implementation effect

Prometheus service discovery based on Eureka

The shorts shown above are 10004, not 10005, because I’m not hereapplication.ymlSpecified inmanagement.server.portTime cut graph.

3、 Complete code

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

4、 Reference link

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

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]