Springboot内置了对Prometheus得支持,可以监测得点有:
- JVM各指标参数(GC,堆,非堆等)
- 接口调用次数,延时
- 系统内存,IO,CPU使用率
部署Prometheus和Grafana
准备一台2核4G或以上配置得linux系统(centos7.9、ubuntu18.04或更高版本),按下面得方式部署Prometheus和Grafana
#下载镜像
docker pull prom/prometheus
docker pull grafana/grafana
#将docker里的配置拷出来
docker run --name=grafana -d -p 3000:3000 -itd grafana/grafana
mkdir -p /docker/prometheus/grafana/data
mkdir -p /docker/prometheus/grafana
docker cp grafana:/etc/grafana/grafana.ini /docker/prometheus/grafana/
#写Prometheus配置文件
vim prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: "ProvideAPIServices"
scrape_interval: 5s
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["172.16.0.88:8789","172.16.0.69:8789"]
#启动Prometheus
docker run -d --name prometheus -p 9090:9090 -v /root/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
#启动grafana
docker run -d -p 3000:3000 --name grafana --restart=always -v /docker/prometheus/grafana/grafana.ini:/etc/grafana/grafana.ini -v /docker/prometheus/grafana/data:/var/lib/grafana -e "GF_SECURITY_ADMIN_PASSWORD=admin" grafana/grafana
注意两个地方
metrics_path: "/actuator/prometheus" #这里填java服务得请求路径+/actuator/prometheus,如果加了前缀,如 context-path: /abc,则为/abc/actuator/prometheus
["172.16.0.88:8789","172.16.0.69:8789"] #172.16.0.88:8789 为java服务ip+断开
修改pom.xml
加入下面依赖
jdk<=11
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
jdk>=17
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
</dependency>
修改application.xml
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
prometheus:
enabled: true #激活prometheus
health:
show-details: always
metrics:
export:
prometheus:
enabled: true
Grafana dashboards | Grafana Labs
在grafna导入模板,建议 12464 14430