When dealing with a complex system, we often need monitoring tools to help us solve performance problems. For example, we used before
SpringBoot AdminTo monitor the application in order to obtain
SpringBoot ActuatorIndicator information exposed. Today I’m going to introduce Grafana, a powerful monitoring tool that allows you to visualize everything you need to monitor.

Mall SpringBoot practical electricity project (50 k + star) address: https://github.com/macrozheng/mall

Grafana profile

Grafana is an open source data visualization and analysis tool that allows you to visualize your metrics no matter where they are stored. It also has an alert feature that alerts you when your metrics are out of a specified range.

Prometheus profile

Prometheus is a temporal database, which can be simply understood as a MySQL database with time. Since Grafana only converts data into visual charts and has no storage capability, we need to use it in conjunction with a temporal database like Prometheus.

The installation

InstALLING Grafana and Prometheus using Docker is definitely the easiest, and we’ll do it this way.

  • First download Grafana’s Docker image;
docker pull grafana/grafana
  • After downloading, run Grafana;
docker run -p 3000:3000 --name grafana \
-d grafana/grafana
  • Next, download the Prometheus Docker image;
docker pull prom/prometheus
  • in/mydata/prometheus/Directory to create a configuration file for Prometheusprometheus.yml:
global:
  scrape_interval: 5s
  • Run Prometheus and put the configuration files in the hostprometheus.ymlMount into a container;
docker run -p 9090:9090 --name prometheus \
-v /mydata/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-d prom/prometheus
  • So far the installation is complete, is not very simple! Grafana can be accessed at the following address with a login passwordadmin:admin, access address:http://192.168.5.78:3000/

  • Log in to Grafana and the screen will look like this;

  • Prometheus is actually a visual interface, it is a little humble, access to the address: http://192.168.5.78:9090/

use

Now that Grafana has been installed, it’s time to get started. Let’s talk about using Grafana to monitor Linux systems and SpringBoot applications.

Monitoring system information

use
node_explorerYou can expose the metrics information of your Linux system, and Prometheus can retrieve and store the metrics information through a timed scan.

  • downloadnode_explorerInstallation package, download address:https://prometheus.io/downloa…

  • This time we’re going to just takenode_explorerInstall it on a Linux server (if you use Docker container, you will monitor the Docker container indicator information), unpack the downloaded installation package to the specified directory, and change the folder name:
CD /mydata tar-zxvf node_Exporter -1.1.2.linux-amd64.tar.gz mv node_Exporter -1.1.2.linux-amd64 node_Exporter
  • Go to the unzip directory and run it with the following commandnode_explorer, the service will run on9100Port;
cd node_exporter
./node_exporter >log.file 2>&1 &
  • usecurlCommand access access to the index information interface, access to information indicates successful operation;
curl http://localhost:9100/metrics
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 2175
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0
  • Next, modify the Prometheus configuration fileprometheus.ymlCreate a task timer scannode_explorerIndicator information exposed;
Scrape_configs: -job_name: node static_configs: -targets: ['192.168.5.78:9100']
  • Restart the Prometheus containerPlus - > DashboardTo create a dashboard;

  • Of course you can also choose to download a Dashboard Grafana panel market, market address: https://grafana.com/grafana/d…

  • So it’s selectedNode Exporter FullThis dashboard, remember its ID, access the address:https://grafana.com/grafana/d…

  • Select Import Dashboard and enter ID, then clickLoadCan;

  • Select Prometheus as the data source and clickImport;

  • After the import is successful, you can see the real-time monitoring information in Grafana. Isn’t that cool?

Monitor SpringBoot applications

Monitoring SpringBoot applications requires a dependency
actuatorand
micrometer, through exposure
actuatorPrometheus can retrieve and store metric information on a regular basis.

  • Modify the projectpom.xmlFile, addactuatorandmicrometerRely on;
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <! -- Integrated Micrometer, Save monitoring data to Prometheus --> <dependency> < grouppid >io.micrometer</ grouppid > <artifactId>micrometer-registry-prometheus</artifactId> </dependency> </dependencies>
  • Modify the application configuration fileapplication.ymlThrough theactuatorExposure monitoring port/actuator/prometheus;
Management: Endpoints: Web: Exposure: # exposure/Prometheus' include: 'Prometheus' metrics: tags: Application: ${spring.application.name}
  • Before monitoring the SpringBoot application, we need to run a SpringBoot application using the following command.
docker run -p 8088:8088 --name mall-tiny-grafana \ -v /etc/localtime:/etc/localtime \ -v /mydata/app/mall-tiny-grafana/logs:/var/logs \ -e TZ="Asia/Shanghai" \ -d mall-tiny/mall-tiny-grafana: 1.0-snapshot
  • Modify the Prometheus configuration fileprometheus.ymlCreate a task timer scanactuatorIt is important to note that the SpringBoot application is running in a Docker container and needs to be useddocker inspect mall-tiny-grafana |grep IPAddressTo get the container IP address;
* * * * * * * * * * * * * * * * * * * * * * * ['172.17.0.5:8088'] : # Actuator/Prometheus: Static_configs: - targets: ['172.17.0.5:8088']
  • We can determine whether Prometheus can obtain the indicator information through the visual interface of Prometheus.

  • Similarly, we can import the dashboard from the dashboard market, access to the address: https://grafana.com/grafana/d…

  • After the import is successful, you can now see SpringBoot real-time monitoring information in Grafana. Cool!

conclusion

Through a wave of practice with Grafana, we can see that the process of using Grafana for data visualization is as follows: First we had to expose the metrics to the monitored party, then use Prometheus to retrieve and store the metrics periodically, and finally configure Prometheus to be Grafana’s visual data source.

The resources

  • Grafana official documentation: https://grafana.com/docs/graf…
  • The use of the node – exporter: https://prometheus.io/docs/gu…

Project source address

https://github.com/macrozheng…

In this paper, making
https://github.com/macrozheng/mall-learningHas been included, welcome everyone STAR!