1、建立應用 內建 micrometer
建立一個最簡的 springboot 應用,添加 micrometer 依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.yml
## prometheus配置
management:
endpoint:
metrics:
enabled: true
prometheus:
prometheus: true
## 配置為開啟 Actuator 服務
endpoints:
web:
exposure:
include: '*'
metrics:
export:
export:
enabled: true
spring:
application:
name: springboot-prometheus
server:
port: 8888
在啟動類中添加Bean,用于監控JVM性能名額:
@SpringBootApplication
public class SpringbootPrometheusApplication {
@Value("${spring.application.name}")
private String application;
public static void main(String[] args) {
SpringApplication.run(SpringbootPrometheusApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer() {
return (registry) -> registry.config().commonTags("application", application);
}
啟動服務。
檢視監控端點資訊:
2. 部署 Prometheus
官網:
https://prometheus.io/
可以下載下傳安裝包來安裝,但下載下傳速度極其慢,幾乎下載下傳不了。
可以使用 docker 部署,因為國内有docker鏡像,是以速度很快。
docker 方式啟動:
docker run --name prometheus -d -p 9090:9090 prom/prometheus
執行完成後就OK了,可以看一下 Prometheus 的界面。
http://localhost:9090/targets 是監控目标清單頁:
3. Prometheus + Springboot應用
監控應用,需要在 Prometheus 配置檔案中添加應用的相關資訊。
配置檔案在容器中的路徑:/etc/prometheus。
檢視一下配置檔案的預設内容:
docker exec -it [容器ID] cat /etc/prometheus/prometheus.yml
紅框内是我們要關注的部分,按照這個形式添加我們的應用即可。
需要添加的内容為:
- job_name: 'springboot_prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['ip:應用端口']
}
metrics_path 指定監控端點的路徑。
targets 指定應用的IP端口,這裡使用了IP,沒有使用localhost,因為 Prometheus 是容器運作的,如果使用 localhost 就會通路容器内部。
配置不是直接在容器内部修改,可以把容器内部的配置檔案拷貝出來一份,修改後,重新開機啟動容器,挂載本地修改過的配置檔案。
拷貝容器中的配置檔案:
docker cp [容器ID]:/etc/prometheus/prometheus.yml .
修改配置檔案,添加配置,最終的内容:
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
- job_name: 'springboot_app'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['ip:應用端口']
}
注意:由于測試的機器公網會一直變,是以每次都要重新配置
停掉之前的容器,重新啟動:
docker run --name prometheus -d \
-p 9090:9090 \
-v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
通路監控清單頁 http://localhost:9090/targets 就可以看到我們的應用了:
點選端點連結,可以看到監控資料,例如:
進入查詢控制台頁面 http://localhost:9090/graph,可以查詢一個名額,例如 http_server_requests_seconds_sum,效果:
4. 部署 Grafana
docker方式運作:
docker run -d -p 3000:3000 --name=grafana grafana/grafana
啟動後,通路:http://localhost:3000,預設使用者名密碼 admin/admin。
5. 添加 Prometheus 資料源
6. 展示應用的 JVM 資訊
Grafana 中已經有現成的 JVM 儀表盤,我們直接導入使用即可。
這個儀表盤的編号為 4701。
至此,Prometheus + Grafana + Springboot 的整體流程已經跑通了。
但是,這些名額都是底層通用名額,在業務層面一定會有個性需求,下面我們自己定義一些監控名額。