pom.xml引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
<version>3.6.7</version>
<scope>runtime</scope>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
编辑application.properties
# Actuator Web 访问端口 默认8081
management.server.port=8080
# 暴露所有端点
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
启动springboot应用
浏览器正常展示metrics指标
springboot应用完成
接下来处理prometheus
拷贝一份prometheus.yml
在prometheus.yml添加以下监控springboot应用内容
# 监控springboot应用配置
- job_name: "boot2prometheus-service"
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["192.168.1.106:8080"]
修改后的完整的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"]
# 监控springboot应用配置
- job_name: "boot2prometheus-service"
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["192.168.1.106:8080"]
在etc路径下创建prometheus目录
mkidr /etc/prometheus
在执行以下命令创建prometheus.yml文件
cat > /etc/prometheus/prometheus.yml <<-'EOF'
# 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"]
# 监控springboot应用配置
- job_name: "boot2prometheus-service"
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["192.168.1.106:8080"]
EOF
确保宿主机安装了docker
采用docker命令安装prometheus
docker run \
-d \
-p 9090:9090 \
-v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
bitnami/prometheus
prometheus中status选择targets
这里也采用docker命令安装grafana
docker run -d -p 3000:3000 --name=grafana grafana/grafana-enterprise
grafana默认账户密码为admin
点击Configuration(小齿轮图标)
选择Data sources 点击Add data source按钮
填写prometheus地址并保存
点击+号图标并选择import
填4701编号点击load按钮
选择prometheus点击Import按钮
导入后会展示各种图标,里面有java常用的指标监控,记得选择右上角的时间范围,默认是24小时,对于刚启动的程序是没有数据的,我们选择过去5分钟的选项即可观察到数据