springboot 版本2.7.4
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>io.micrometergroupId>
<artifactId>micrometer-registry-prometheusartifactId>
<scope>runtimescope>
dependency>
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include:
- prometheus
- health
- info
metrics:
tags:
application: tcoding-demo
@SpringBootApplication
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@GetMapping
public String hello(){
return "hello world";
}
}
docker pull bitnami/prometheus:2.38.0
docker run -itd --name prom -p 9090:9090 bitnami/prometheus:2.38.0
进入容器修改prometheus.yml
docker exec -it -u 0 prom /bin/bash
apt-get update -y
apt-get install -y vim
修改metrics_path和static_configs
host.docker.internal 是容器访问宿主机hostname
scrape_configs:
# The job name is added as a label `job=` to any timeseries scraped from this config.
- job_name: "tcoding-demo"
metrics_path: '/actuator/prometheus'
# scheme defaults to 'http'.
static_configs:
- targets: ["host.docker.internal:8080"]
然后重启 docker restart prom
本地新建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=` to any timeseries scraped from this config.
- job_name: "tcoding-demo"
metrics_path: '/actuator/prometheus'
# scheme defaults to 'http'.
static_configs:
- targets: ["host.docker.internal:8080"]
docker run -itd --name prom -p 9090:9090 \
-v /your path/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus \
bitnami/prometheus:2.38.0
docker pull grafana/grafana:9.1.6
docker run \
-itd \
-p 3000:3000 \
--name=grafana \
--link=prom \
grafana/grafana:9.1.6
访问 http://localhost:3000/
修改数据源url
https://grafana.com/grafana/dashboards/
我是用的是jvm
上传刚刚下载的json文件
启动springboot
grafana切换一下时间范围,太长显示有问题(没有历史数据)
效果预览
https://github.com/googalAmbition/hello-spring-boot/tree/main/44-actuator-prometheus