SpringBoot应用接入Prometheus+Grafana

一.Prometheus简介

Prometheus受启发于Google的Brogmon监控系统(相似的Kubernetes是从Google的Brog系统演变而来),从2012年开始由前Google工程师在Soundcloud以开源软件的形式进行研发,并且于2015年早期对外发布早期版本。2016年5月继Kubernetes之后成为第二个正式加入CNCF基金会的项目,同年6月正式发布1.0版本。2017年底发布了基于全新存储层的2.0版本,能更好地与容器平台、云平台配合。

二.Prometheus接入

1.下载安装包

官网指定下载包: https://prometheus.io/download/

2. SpringBoot应用接入

2.1 引入依赖

SpringBoot版本为 2.2.0.RELEASE

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><scope>runtime</scope>
</dependency>

2.2 yaml配置

需要指定Prometheus相关的参数

server:port: 8080spring:application:name: actuator-prometheus
management:endpoints:
# 这里指定所有的web接口都会上报web:
#      base-path: /actuatorexposure:include: "*"server:port: 9091metrics:export:prometheus:enabled: true
# 这个应用所有上报的metrics 都会带上 application 这个标签tags:application: ${spring.application.name}

上面配置完毕之后,会提供一个 /actuator/prometheus 的端点,供prometheus来拉取Metrics信息。

3. 访问Metrics信息

http://localhost:9091/actuator/prometheus 次数的端口9091为yaml中配置的端口号

4. prometheus 配置

进入到步骤1的包中的Prometheus.yml中

修改配置

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'metrics_path: '/actuator/prometheus'# scheme defaults to 'http'.static_configs:- targets: ['localhost:9091']

5. 启动prometheus服务

以管理员身份运行 prometheus


6. 访问prometheus监控

浏览器输入http://localhost:9090/回车

接下来访问Graph,选择metric: process_start_time_seconds 可以看到一条抓起metric的记录

当服务启动之后,我们也可以在控制台上我们的应用信息

7. prometheus测试

在启动类中加上注册核心代码块

public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}// 注意,这个是注册的核心代码块@BeanMeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {return (registry) -> registry.config().commonTags("application", applicationName);}}

编写测试controller

@Slf4j
@RestController
public class PrometheusController {private Random random = new Random();@Value("${spring.application.name}")private String name;// 一个用于演示的http接口@GetMapping(path = "hello")public String hello() {log.info("name=[{}]",name);int sleep = random.nextInt(200);try {Thread.sleep(sleep);} catch (InterruptedException e) {e.printStackTrace();}return "hello sleep: " + sleep + " for " + name;}
}

浏览器访问接口:http://localhost:8080/hello

进入实例中抓取请求记录


接下来访问Graph,选择metric: http_server_requests_seconds_count
可以看到一条抓起metric的记录


这些数据由框架层直接集成,实现REST接口的相关信息上报,借助这个metric,我们可以实现qps的统计.

二.Grafana接入

1.下载安装包

下载地址:https://grafana.com/grafana/download?platform=windows

解压后运行bin目录下的grafana-server.exe启动,游览器访问http://localhost:3000即可看到登录页面,默认账号密码是admin/admin。

浏览器访问http://localhost:3000

2. 设置数据源

在设置中找到 Data source

添加data source ,然后选择普罗米修斯

设置name和url,这里的url就是我们的prometheus地址

点击下面的save and test链接成功以后我们开始创建监控。

3. 创建监控面板

点击 new dashboard,然后点击 and a new penal

在这个面板我们就可以去编辑我们要监控的数据信息

4. 测试监控

4.1 创建测试接口

这里我们简单写一个接口来测试一下我们的请求次数信息;

public class PrometheusController {private Random random = new Random();@Value("${spring.application.name}")private String name;// 一个用于演示的http接口@GetMapping(path = "hello")public String hello() {log.info("name=[{}]",name);int sleep = random.nextInt(200);try {Thread.sleep(sleep);} catch (InterruptedException e) {e.printStackTrace();}return "hello sleep: " + sleep + " for " + name;}
}

4.2 启动服务

这里要同时启动springboot工程,prometueus和grafana

启动springboot工程

启动prometueus

启动grafana

4.3 创建监控面板

4.4 监控面板Demo

当我们统计的数据多的时候,我们就来可以规划我们的监控面板,下面是我简单统计的几个数据组成的页面,我们可以根据设计然后自定义对我们的监控面板进行排版。

右侧可以设置面板的刷新时间,这样就可以做到实时监控我们的应用数据信息了。

SpringBoot应用接入Prometheus+Grafana相关推荐

  1. SpringBoot应用接入Prometheus的全过程解析

    普罗米修斯:Prometheus是一个开放性的监控解决方案,用户可以非常方便的安装和使用Prometheus并且能够非常方便的对其进行扩展 下面将实现一个SpringBoot应用接入Prometheu ...

  2. prometheus + grafana 对 springboot 项目进行监控

    1.prometheus接入springboot prometheus安装后,在安装目录有一个默认的配置文件prometheus.yml # my global config global:scrap ...

  3. SpringBoot+Prometheus+Grafana实现应用监控和报警

    作者:烟味i cnblogs.com/2YSP/p/12827487.html 一.背景 SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较 ...

  4. 运维(32) Prometheus+Grafana监控SpringBoot

    文章目录 一.前言 二.SpringBoot集成Micrometer 1.`pom.xml`中引入依赖 2.`application.yml`配置 3.Micrometer配置 三.部署Prometh ...

  5. SpringBoot应用监控SpringBoot+Prometheus+Grafana

    SpringBoot应用监控SpringBoot+Prometheus+Grafana 1. SpringBoot应用监控 1.1 SpringBoot应用监控 1.2 SpringBoot应用搭建 ...

  6. SpringBoot + Prometheus + Grafana 打造可视化监控一条龙!

    1.背景 SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较常用的方案之一.它们三者之间的关系大概如下图: 关系图 2.开发SpringBo ...

  7. Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控

    文章目录 环境信息 Prometheus 组件 下载 & 安装 通过指定配置文件prometheus.yml启动Prometheus 查看采集到的性能指标 查看prometheus规则 查看监 ...

  8. Prometheus+Grafana (史上最全)

    尼恩大架构 最强环境 系列文章 一键打造 本地elk 实操环境: ELK日志平台(elasticsearch +logstash+kibana)原理和实操(史上最全) 高级开发必备,架构师必备 一键打 ...

  9. Linux安装prometheus+grafana监控

    一.在业务中遇到服务器负载过高问题,由于没有监控,一直没发现,直到业务方反馈网站打开速度慢,才发现问题.这样显得开发很被动.所以是时候搭建一套监控系统了. 由于是业余时间自己捯饬,所以神马业务层面的监 ...

  10. MinIO集群怎么接入Prometheus监控?(上)

    前言 minio集群有暴露监控指标接口给Prometheus,可通过配置Prometheus访问MinIO集群的权限,将MinIO集群接入Prometheus监控,并通过MinIO官方发布的Grafa ...

最新文章

  1. Microbiome:南土所梁玉婷组-稻田土壤产甲烷菌的共存模式
  2. python 字符串形式的列表 转 列表
  3. girton college map
  4. 未能加载文件或程序集“Iesi.Collections”或它的某一个依赖项。参数错误。 (异常来自 HRESULT:0x80070057 (E_INVALIDARG))
  5. 18 | 案例篇:内存泄漏了,我该如何定位和处理?
  6. 我的世界java怎么玩起床战争_我的世界怎么玩起床战争_我的世界起床战争怎么玩_52pk单机游戏...
  7. vue-resource跨域问题
  8. LeetCode 1323. 6 和 9 组成的最大数字
  9. Java案例:Java版生命游戏
  10. Python 列表 min() 方法
  11. 使用Nginx、Keepalived构建负载均衡
  12. python+selenium+Firefox+pycharm版本匹配
  13. 竞赛成绩管理系统用c语言编写,学生成绩管理系统代码(c语言编写).doc
  14. paip.提升效率--数据绑定到table原理和流程Angular js jquery实现
  15. 根据缺口的模式选股买股票,python 学习代码
  16. java图书管理系统毕业设计_java图书管理系统毕业论文
  17. quartus仿真文件的编写
  18. 计算机中文无敌版,与电脑下象棋无敌版
  19. html+浏览器自动全屏,web 使网站在浏览器中全屏显示 fullscreen=yes
  20. HTTP长连接与短连接、长轮询与短轮询及长轮询的实现概述

热门文章

  1. unity控制物体移动和转向
  2. ARouter there is no route matched
  3. vivo打开开发者选项
  4. OpenStreetMap初探(一)——了解OpenStreetMap
  5. 【网络基础】DDNS - 动态DNS
  6. 微信游戏奇迹暖暖选取服务器失败,奇迹暖暖微信登录授权失败
  7. 中国十六烷基磷酸钾行业市场供需与战略研究报告
  8. php远程控制代码,内部网络机器的远程控制软件UltraVNC及其源代码
  9. 实际运用1:正弦和余弦的动画图像生成(基于matplotlib的python数据可视化)
  10. 经历“海潮效应”,云图如何成为智能家居界的苹果?