Grafana+Prometheus technical documentation - advanced use - monitoring spring-boot project

Adam:

        The use of Prometheus to monitor the server and create dashboards has been implemented before, and now these monitoring methods need to be used in spring-boot.

Implementation idea:

        1. Integrated Actuator

        2. Add Prometheus dependency

        3. Configure open ports and open monitoring

        4. Configure the configuration in Prometheus

        5. Use the SpringBoot Actuator in Grafana to display the template

1. Integrated Actuator

 Introduce dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Spring Actuator is a module in the Spring framework, designed to provide monitoring and management functions for applications. It provides developers with a series of HTTP endpoints that can be used to view application health, performance metrics, log information, and more. Actuator can help developers monitor, diagnose and manage applications, so as to better understand the operation of applications.

Spring Actuator provides some commonly used endpoints, including:

  1. /health: Used to check the health status of the application, such as whether the database connection is normal, whether the disk space is sufficient, etc.

  2. /info: Used to view general information about the application, such as the name, version, description, etc. of the application.

  3. /metrics: Used to view the running indicators of the application, such as CPU usage, memory usage, number of requests, etc.

  4. /logfile: Used to view the application's log file.

  5. /env: Used to view the environment variables of the application.

In addition to these commonly used endpoints, Actuator also provides some other functions, such as configuration endpoints, thread dump endpoints, hot restart endpoints, etc. Developers can custom configure and use these endpoints according to their needs.

Integrating Actuator in a Spring project is very simple. You only need to add the relevant Actuator module to the dependencies of the project and perform the necessary configuration to enable the Actuator function.

Dependency description:

When using Prometheus for monitoring in a Spring Boot application, spring-boot-starter-actuatordependencies provide the ability to integrate with Prometheus. By adding this dependency, you can make the Spring Boot application expose the required endpoints for Prometheus to scrape and collect monitoring metrics.

Specifically, spring-boot-starter-actuatorthe Actuator module in enables the following Prometheus-related features:

  1. Automatic configuration: The Actuator automatic configuration module will automatically enable the relevant functions integrated with Prometheus for the application according to the configuration properties defined in the application. This enables integration with Prometheus without manual configuration.
  2. Endpoint exposure: Actuator provides a set of HTTP endpoints, including those related to Prometheus monitoring. These endpoints expose monitoring data to Prometheus for scraping.
  3. Metrics and Health information: Through the endpoint of Actuator, you can get the Metrics information and Health status of the application. This information will be captured and stored by Prometheus for further analysis and alerts.

In short, spring-boot-starter-actuatordependencies enable Spring Boot applications to integrate with Prometheus, allowing applications to be monitored and measured through Prometheus.

2. Introduce Prometheus dependencies micrometer-registry-prometheus.

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

micrometer-registry-prometheusThis dependency provides the ability to expose application metrics to Prometheus when using Prometheus for monitoring in a Spring Boot project.

Specifically, micrometer-registry-prometheusa Micrometer implementation of the Prometheus Registrar. Micrometer is a metrics library for Java applications that provides a standardized way to collect and report metrics data. By adding this dependency, you can integrate Micrometer with Prometheus, thereby exposing the application's metrics data to Prometheus for scraping.

When integrated with Prometheus, you can send the measurement data generated by Micrometer to Prometheus through configuration, and then use PromQL for query and analysis. In this way, you can get application performance metrics, such as request processing time, database query rate, etc., for monitoring and problem diagnosis.

In addition, micrometer-registry-prometheussome additional functions are provided, such as customizing the naming and labeling of metric data, and integrating with Prometheus's auto-discovery function, etc.

In summary, micrometer-registry-prometheusdependencies enable Spring Boot projects to integrate with Prometheus for better monitoring of application performance.

3. Add yml configuration

Because the endpoint is not available by default, it must be public, so the following configuration needs to be added

management:
  endpoints:
    web:
      exposure:
        include: prometheus,health,info

And specify the service name

spring:
  application:
    name: adn-ask

4. Add Prometheus configuration

scrape_configs:
  - job_name: 'adn-ask'   #prometheus任务名称
    scrape_interval: 5s  #每五秒抓取一次数据
    metrics_path: '/actuator/prometheus'   #数据来源的地址
    static_configs:
      - targets: ['localhost:8081']   #拉去数据的地址

 

 

 Note that the service name must correspond to the following port number and ip address when using it.

5. Import the display template of SpringBoot Actuator

The template id used is: 12900 

The official website that provides exhibition boards: 

Dashboards | Grafana Labs

Guess you like

Origin blog.csdn.net/weixin_72186894/article/details/132217818