1. Introduction
Monitoring is often one of the most neglected parts of the system development process, but it is also one of the most important parts of the application life cycle. With monitoring, you can know your system like the back of your hand. This paper makes a brief introduction from four parts: Actuator, Micrometer, Prometheus and Grafana.
2.Actuator
2.1 Adding a Monitoring Dependency
To monitor SpringBoot applications, add the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code
2.2 Enabling a Monitoring endpoint
After adding the rely on, start the project, visit http://localhost:8080/actuator/ you can see the following data
{
"_links": {
"self": {
"href":"http://localhost:8080/actuator".
"templated":false
},
"health-path": {
"href":"http://localhost:8080/actuator/health/{*path}".
"templated":true
},
"health": {
"href":"http://localhost:8080/actuator/health".
"templated":false
},
"info": {
"href":"http://localhost:8080/actuator/info".
"templated":false
}
}
}
Copy the code
By default, only the health and INFO endpoints are exposed below. If you want to expose all endpoints, you need to add the enable configuration in YML
management:
endpoint:
health:
show-details: ALWAYS
endpoints:
web:
exposure:
include: The '*'
Copy the code
Visit http://localhost:8080/actuator/ you can see more of the endpoint information again
{
"_links": {
"self": {
"href":"http://localhost:8080/actuator".
"templated":false
},
"custom": {
"href":"http://localhost:8080/actuator/custom".
"templated":false
},
"beans": {
"href":"http://localhost:8080/actuator/beans".
"templated":false
},
"caches": {
"href":"http://localhost:8080/actuator/caches".
"templated":false
},
"caches-cache": {
"href":"http://localhost:8080/actuator/caches/{cache}".
"templated":true
},
"health-path": {
"href":"http://localhost:8080/actuator/health/{*path}".
"templated":true
},
"health": {
"href":"http://localhost:8080/actuator/health".
"templated":false
},
"info": {
"href":"http://localhost:8080/actuator/info".
"templated":false
},
"conditions": {
"href":"http://localhost:8080/actuator/conditions".
"templated":false
},
"configprops": {
"href":"http://localhost:8080/actuator/configprops".
"templated":false
},
"env-toMatch": {
"href":"http://localhost:8080/actuator/env/{toMatch}".
"templated":true
},
"env": {
"href":"http://localhost:8080/actuator/env".
"templated":false
},
"loggers-name": {
"href":"http://localhost:8080/actuator/loggers/{name}".
"templated":true
},
"loggers": {
"href":"http://localhost:8080/actuator/loggers".
"templated":false
},
"heapdump": {
"href":"http://localhost:8080/actuator/heapdump".
"templated":false
},
"threaddump": {
"href":"http://localhost:8080/actuator/threaddump".
"templated":false
},
"metrics-requiredMetricName": {
"href":"http://localhost:8080/actuator/metrics/{requiredMetricName}".
"templated":true
},
"metrics": {
"href":"http://localhost:8080/actuator/metrics".
"templated":false
},
"scheduledtasks": {
"href":"http://localhost:8080/actuator/scheduledtasks".
"templated":false
},
"startup": {
"href":"http://localhost:8080/actuator/startup".
"templated":false
},
"mappings": {
"href":"http://localhost:8080/actuator/mappings".
"templated":false
}
}
}
Copy the code
3.Micrometer
The above steps allow you to monitor system metrics in the browser, which is obviously not elegant and complex. Third party tools were used to monitor the system, exposing data in Prometheus format through Micrometer and adding dependencies
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Copy the code
http://localhost:8080/actuator/ will find much information one endpoint
Visit http://localhost:8080/actuator/prometheus to see foreign exposure system indicators data
4.Prometheus
Since the system exposed the indicator data, it needed a system to pull the indicator data, and that system was Prometheus
4.1 Docker pulls Prometheus
docker pull prom/prometheus
Copy the code
4.2 Editing a Configuration File
Create the promethe. yml file on the host and edit it as follows:
global:
scrape_interval: 10s
scrape_configs:
- job_name: 'spring_micrometer'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['10.100.177.183:8080']
Copy the code
Note: Since Prometheus is deployed in a container, the targets option in the configuration file requires the IP address of the host. Setting localhost will result in data not being pulled
4.3 run the Prometheus
Docker run - name Prometheus - p 9090-9090 - v local configuration file path: / etc/Prometheus/Prometheus yml 3-d image id
Copy the code
4.4 visit Prometheus
Enter http://localhost:9090 in the address box of the browser. If the page is displayed, Prometheus is started successfully
Then choose Status >Targets. If the following information is displayed, Prometheus can obtain system application indicator data normally
4.5 Viewing Monitoring Indicators
5.Grafana
To enrich panels for visual monitoring, you need to introduce Grafana
5.1 Docker pulls Grafana
docker pull grafana/grafana
Copy the code
5.2 run Grafana
Docker run --name grafana -p 3000:3000-d Grafana run --name grafana -p 3000:3000-d grafana run --name grafana -p 3000:3000-d
Copy the code
5.3 access Grafana
Enter http://localhost:3000/ in the browser
5.4 Configuring a Data Source
Select the Prometheus data source
Enter related configuration information
5.5 Adding a Monitoring Panel
Adding panels, such as the JVM monitor panel and SpringBoot Monitor panel, can be done by looking at grafana.com and copying the corresponding ID by clicking the Load button
Select Prometheus, the previously configured data source