Prometheus是一个开源的系统监控和报警系统,现在已经加入到CNCF基金会,成为继k8s之后第二个在CNCF托管的项目,在kubernetes容器管理系统中,通常会搭配prometheus进行监控,同时也支持多种exporter采集数据,还支持pushgateway进行数据上报,Prometheus性能足够支撑上万台规模的集群。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.github.mweirauch</groupId>
<artifactId>micrometer-jvm-extras</artifactId>
<version>0.2.0</version>
</dependency># actuator+prometheus+grafana+springboot2监控集成配置
management:
endpoints:
web:
exposure:
include: '*' # 暴露/actuator/prometheus
metrics:
tags:
application: ${spring.application.name} # 暴露的数据中添加application label@SpringBootApplication
@MapperScan("com.qingfeng.*.mapper")
@EnableMyRedis
@EnableMyProtect
@ServletComponentScan("com.qingfeng.framework.servlet")
public class QingfengApplication {
@Value("${spring.application.name}")
private String application;
public static void main(String[] args) {
SpringApplication.run(QingfengApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer() {
return (registry) -> registry.config().commonTags("application", application);
}
}启动后,在浏览器访问:http://localhost:8090/actuator/prometheus
出现如下信息,说明 集成成功。

下载地址:https://prometheus.io/download/
Prometheus会将所有采集到的样本数据以时间序列(time-series)的方式保存在内存数据库中,并且定时保存到硬盘上。
下载后,将项目解压,解压目录如下:

在最后加上SpringBootPrometheus
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"]
- job_name: "SpringBootPrometheus"
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ["127.0.0.1:8090"]启动Prometheus
windows下直接双击:prometheus.exe 进行启动。

Linux启动方式:
nohup ./prometheus &浏览器访问:http://localhost:9090

查看Prometheus监控的应用

查看具体的监控指标

安装配置Grafana下载安装启动 下载地址:https://grafana.com/grafana/download

下载后,解压

双击启动grafana-server.exe 启动

Linux启动方式:nohup ./grafana&浏览器访问:http://127.0.0.1:3000/login账号和密码都是:admin

添加数据源 在Data Sources选项中添加数据源

设置数据源的名称(唯一的,可添加多个数据源)和Prometheus的访问地址,如果Prometheus有设置账号密码才可以访问,则需要在Auth模块勾选Basuc Auth设置账号密码

导入仪表盘模板 ●模板地址:https://grafana.com/dashboards ●在搜索框中搜索Spring Boot会检索出相关的模板,我们使用:SpringBoot APM Dashboard

拷贝模板id,此处的模板id为:12900

模板导入 输入模板ID 点击Load


配置

