prometheus监控springboot应用简单使用介绍详解
作者:braveheart_07d6 时间:2023-02-24 03:49:00
对于springboot应用,需要以下几个步骤
springboot应用开启endpoint,添加actuator的以来和promethus的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
在yml文件或者properties文件中,加入以下配置:
management:
endpoints:
jmx:
exposure:
include: "*"
web:
exposure:
include: "*"
metrics:
export:
datadog:
application-key: ${spring.application.name}
这里需要注意是,*号是需要加双引号的。
以上两个步骤完毕后,剩下的就是要加registry了:
@Bean MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.
config().
commonTags("application", applicationName);
}
对于springboot的应用,到此基本完成了。接下来是启动promethus。
配置prometheus
首先要下在prometheus":Prometheus - Monitoring system & time series database"
在下载页面,选择何时的版本下载,推荐下载tar.gz包。下载好后,进行解压。在合适的路径下即可。
这里介绍下prometheus的目录和文件:
1、prometheus采用的都是yml文件的配置方式。
2、在根目录下,有个prometheus.yml配置文件,文件初始化的内容如下:
global:
scrape_interval: 15s # 这个是每次数据手机的频率
evaluation_interval: 15s # 评估告警规则的频率。
rule_files:
# - "first.rules"
# - "second.rules"
scrape_configs: # 通过这里的配置控制prometheus监控的资源
- job_name: prometheus # prometheus自身默认的
static_configs:
- targets: ['localhost:9090'] # 默认暴露的是9090端口服务
global是全局配置。具体见上面的注释说明。
3、添加我们的应用,对springboot进行监控
- job_name: 'spring-sample'
metrics_path: 'actuator/prometheus' # 这里我们springboot暴露出来的endpoint
scrape_interval: 5s # 信息收集时间是间隔5秒
static_configs:
- targets: ['localhost:8778'] # 这里是springboot暴露出来的地址和端口
4、这些配置完成后,可以启动prometheus,./prometheus --config.file=prometheus.yml,服务即可启动。具体访问产检官网。
配置grafana
下载grafana,直接启动即可。
1、启动命令参见官网:./grafana-server web
2、配置datasource,选择prometheus。这个里面有个很重要的注意点,我看网上很多人在转如何用prometheus监控springboot应用,估计自己没去实际搭建,在interval这个时间上,默认是数字,比如15,代表是15秒。在添加dashboard的时候,会发现监控图标左上角是个红点,报错:Invalid interval string, expecting a number followed by one of "Mwdhmsy" ,这个错的解决方案就是在这些时间间隔后面加个"s"。问题解决。
3、选择dashboard,import的里输入一个模板,可以去dashboards去找你对应的模板,我们这里选用jvm的4701模板,然后就能看到你的springboot的监控信息了。到此,整个搭建完成。
来源:https://www.jianshu.com/p/ed12dda024cc
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java操作IO对象流进行数据的读写
C++实现softmax函数的面试经验
![](https://img.aspxhome.com/file/2023/1/66321_0s.png)
Java实现马踏棋盘算法
![](https://img.aspxhome.com/file/2023/4/76254_0s.jpg)
Android 文件数据存储实例详解
Java实现宠物商店管理
Java基本语法之内部类示例详解
Zookeeper连接超时问题与拒绝连接的解决方案
![](https://img.aspxhome.com/file/2023/3/78863_0s.png)
利用java反射机制实现自动调用类的简单方法
Spring的@Validation和javax包下的@Valid区别以及自定义校验注解
Android Wear计时器开发
![](https://img.aspxhome.com/file/2023/4/93324_0s.png)
Trie树(字典树)的介绍及Java实现
![](https://img.aspxhome.com/file/2023/5/79325_0s.png)
C# 反射与 Quartz 实现流程处理详情
Spring-Data-JPA整合MySQL和配置的方法
![](https://img.aspxhome.com/file/2023/4/58734_0s.png)
解决idea每次新建项目都需要重新指定maven目录
![](https://img.aspxhome.com/file/2023/3/61273_0s.png)
C# 设计模式系列教程-简单工厂模式
![](https://img.aspxhome.com/file/2023/3/75623_0s.png)
C#窗体实现酒店管理系统
![](https://img.aspxhome.com/file/2023/5/79125_0s.jpg)
Java Swing null绝对布局的实现示例
![](https://img.aspxhome.com/file/2023/6/80896_0s.png)
C# 获取文件夹里所有文件名的详细代码
Spring Boot配置线程池拒绝策略的场景分析(妥善处理好溢出的任务)
Java Kryo,Protostuff,Hessian序列化方式对比
![](https://img.aspxhome.com/file/2023/0/68500_0s.png)