qinfengge

qinfengge

醉后不知天在水,满船清梦压星河
github
email
telegram

Monitor JVM in Spring Boot

In large programs, the importance of monitoring increases as the user base grows. A clear and concise monitoring system can greatly help developers and operations personnel identify issues or deficiencies in the program.

For spring boot programs, we can use the following tools for system-level monitoring:

  • Grafana is a cross-platform open-source metric analysis and visualization tool.
  • Prometheus is a SoundCloud open-source monitoring and alerting solution that stores time series data.
  • Spring Boot Actuator can monitor and measure spring boot applications.

Program Configuration#

First, you need to add dependencies:

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

<!--Convert actuator metrics to Prometheus format-->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.7.1</version>
</dependency>

Declare the exposed monitoring metrics in the configuration file:

management:
  endpoints:
    web:
        # Endpoints that need to be exposed. By default, only the health and info endpoints are open. Use "*" to open all endpoints.
        # It is not recommended to open all endpoints in production environment. Open only as needed based on project requirements.
      exposure:
        include: "*"

It is best to start the program for verification. Access http://localhost:8080/actuator/prometheus to view the monitoring information.

image

Installing Prometheus + Grafana#

Using Ubuntu as an example, first install docker-compose:

apt install docker-compose

Create the prometheus.yml configuration file:

scrape_configs:
  # Job name
  - job_name: 'actuator-springboot'
  # Monitoring path
    metrics_path: '/actuator/prometheus'
    static_configs:
    # Target path
    # If using WSL or a server, please fill in the IP address
      - targets: ['localhost:8080']

Write the docker-compose.yaml file:

version: '3'
services:
  grafana:
    container_name: grafana
    image: grafana/grafana-oss:10.0.0-ubuntu
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 3000:3000
    volumes:
      - ./grafanaplugin:/var/lib/grafana/plugins/grafanaplugin
    privileged: true
    restart: always
  prom:
    image: quay.io/prometheus/prometheus:latest
    volumes:
      - ./monitor/prometheus.yml:/etc/prometheus/prometheus.yml
    command: "--config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus"
    ports:
     - "23333:9090"
    depends_on:
     - exporter
  exporter:
    image: prom/node-exporter:latest
    ports:
     - "19100:9100"

Note:
The prometheus.yml configuration file should be placed in the monitor folder at the same level as the docker-compose.yaml file.

image

The Grafana image uses the latest version 10.0, which is based on Ubuntu. You can also customize it on the official download page. It is recommended to use the latest version, which includes some partially translated Chinese.

Once everything is ready, use the command docker-compose up -d to start these containers.

Verify Prometheus Collection#

Open http://localhost:23333 in your browser and switch to the Targets page through the menu. You can see our monitoring task in the targets.

image

image

Configure Grafana#

Access http://localhost:3000 to enter the Grafana management panel. The default username and password are admin/admin. You can also choose the Chinese language by selecting Home --> Administration --> Default preferences from the sidebar.

image

Add Data Source#

Click on Add new data source in Data sources and select the first Prometheus.

image

Fill in the address of Prometheus.

image

Select GET as the request method at the bottom.

image

Add Monitoring Style#

After configuring the data source, you need to set up a monitoring panel for it. Grafana supports importing styles directly from the market based on IDs, such as https://grafana.com/grafana/dashboards/4701.
Select Dashboards --> New --> Import.

image

Enter 4701 and click Load.

image

After completing this step, you can see detailed monitoring information.

image

Explanation of Monitoring Metrics#

You can refer to this article for an explanation.

  • Configure Grafana monitoring alerts

Monitoring JVM Data with Spring Boot Actuator + Prometheus + Grafana
Monitoring Java Services with Prometheus, Visualizing JVM Parameters through Grafana, Style 4701 Parameter Interpretation

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.