Spring项目改造
maven依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Spring配置
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
搭建prometheus
下载Prometheus镜像
docker pull prom/prometheus
创建prometheus配置
/opt/docker/prometheus/config目录下创建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: '/actuator/prometheus'# scheme defaults to 'http'.static_configs:- targets: ["10.10.10.10:9090"]
targets配置目标java应用的ip端口,例如10.10.10.10:9090
启动Prometheus
docker run -d -p 9090:9090 --name prom \
-v /opt/docker/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
可以查看容器启动日志确认是否启动成功:
docker logs xxxx
访问http://xxxxxxxxxx:9090即可
搭建Grafana
docker pull grafana/grafana
docker run -d --name grafana -p 3000:3000 grafana/grafana
访问http://localhost:3000
配置prometheus数据源
配置Dashboards
下载模板
Dashboards模板下载地址:https://grafana.com/grafana/dashboards/