🍁 作者:知识浅谈,CSDN 签约讲师,CSDN 原力作者,后端领域优质创作者,热爱分享创作
📌 擅长领域:全栈工程师、爬虫、ACM 算法
💒 公众号:知识浅谈
🔥 联系方式 vx:zsqtcc
🤞这次都给他拿下🤞
十分钟快速上手 Prometheus 与 Grafana 监控 SpringBoot 项目先来捋一下数据流的传输
正菜来了⛳⛳⛳
环境:springboot 项目:127.0.0.1:8081prometheus:127.0.0.1:9090grafana:127.0.0.1:3000
🎈项目的创建
📐第 1 步:==pom 文件依赖==
<!--这个依赖用于健康检查,审计,指标收集-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<!--这个依赖用于把数据转换为prometheus格式使用-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.9.2</version>
</dependency>
复制代码
📐第 2 步:==application 配置文件==
spring:
application:
name: springboot-prometheus
#对外暴露哪些指标
management:
endpoints:
web:
exposure:
include: "*"
#激活promethues并转换为对应的promethues数据
endpoint:
prometheus:
enabled: true
health:
show-details: always
#允许对应的数据指标被导出
metrics:
export:
prometheus:
enabled: true
server:
port: 8081
复制代码
📐第 3 步:==为启动类设置自定义标签==
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
// 为实例设置自定义标签
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String name){
return registry -> registry.config().commonTags("application",name);
}
}
复制代码
📐第 4 步:==启动之后结果==
🎈Prometheus 搭建
📐第 1 步: ==创建 prometheus 配置文件==
#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: 'springboot_promethues'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8081']
复制代码
📐第 2 步 :==prometheus docker 容器的创建==
docker run -d --name=prometheus -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -p 9090:9090 bitnami/prometheus:latest
复制代码
📐第 3 步:==查看结果==
🎈Grafana 搭建
📐第 1 步: ==创建 grafana 容器==docker run -d --name=grafana -p 3000:3000 grafana/grafana
📐第 3 步:==登陆账号密码都是 admin==
📐第 3 步 :==指定数据源==
==4701 是针对 springboot 项目的。==
📐第 3 步:==查看结果==
🍚总结
通过 Prometheus 与 Grafana,成功监控 springboot 项目的运行状态,像是 jvm 等一些指标都能够可视化出来。
评论