1 Maven pom.xml引入依赖

<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId>
</dependency>

2 启动类引入注解

import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}

3 Controller类写需要监控的指标,比如Counter

import io.prometheus.client.Counter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Random;@RestController
public class SampleController {private static Random random = new Random();private static final Counter requestTotal = Counter.build().name("my_sample_counter").labelNames("status").help("A simple Counter to illustrate custom Counters in Spring Boot and Prometheus").register();@RequestMapping("/endpoint")public void endpoint() {if (random.nextInt(2) > 0) {requestTotal.labels("success").inc();} else {requestTotal.labels("error").inc();}}
}

4 设置springboot应用的服务名和端口,在application.properties

spring.application.name=mydemo
server.port=8888

5 配置prometheus.yml

global:scrape_interval:     15s # By default, scrape targets every 15 seconds.evaluation_interval: 15s # By default, scrape targets every 15 seconds.# scrape_timeout is set to the global default (10s).# Attach these labels to any time series or alerts when communicating with# external systems (federation, remote storage, Alertmanager).external_labels:monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:# - "first.rules"# - "second.rules"# 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'# Override the global default and scrape targets from this job every 5 seconds.scrape_interval: 5s# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ['localhost:9090']- job_name: 'mydemo'# Override the global default and scrape targets from this job every 5 seconds.scrape_interval: 5smetrics_path: '/prometheus'# scheme defaults to 'http'.static_configs:- targets: ['10.94.20.52:8888']

最关键的配置就是targets: [‘10.94.20.52:8888’],就是springboot应用的ip和端口

注:在application.xml里设置属性:spring.metrics.servo.enabled=false,去掉重复的metrics,不然在prometheus的控制台的targets页签里,会一直显示此endpoint为down状态。

6 多次访问 http://localhost:8888/mydemo/endpoint,然后在prometheus控制台查看相关metrics信息,my_sample_counter,2个页签:Graph,Console

springboot集成prometheus相关推荐

  1. 完整版SpringBoot集成Prometheus配置Grafana监控指标包括响应时间分位数TP90,TP80(图+文)

    1 缘起 监控作为线上服务管理的最重要一环, 每当新服务上线后,都需要监控上线服务运行情况,包括QPS.时延.成功响应率.内存和CPU使用情况等, 通过监控信息,清晰且及时地掌握当前服务的健康程度,以 ...

  2. springboot 接口404_资深架构带你学习Springboot集成普罗米修斯

    这篇文章主要介绍了springboot集成普罗米修斯(Prometheus)的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 ...

  3. springboot集成普罗米修斯

    Prometheus 是一套开源的系统监控报警框架.它由工作在 SoundCloud 的 员工创建,并在 2015 年正式发布的开源项目.2016 年,Prometheus 正式加入 Cloud Na ...

  4. SpringBoot集成Actuator端点配置

    1.说明 Actuator端点可以监控应用程序并与之交互. Spring Boot包括许多内置的端点, 比如health端点提供基本的应用程序运行状况信息, 并允许添加自定义端点. 可以控制每个单独的 ...

  5. Windows下安装Mongodb SpringBoot集成MongoDB和Redis多数据源

    全文内容: Mongodb安装 说明:Mongodb和redis是开发中常用的中间件,Redis的安装使用比较简单就不写了,只说本地也就是Windows安装Mongodb. SpringBoot集成M ...

  6. SpringBoot使用prometheus监控

    本文介绍SpringBoot如何使用Prometheus配合Grafana监控. 1.关于Prometheus Prometheus是一个根据应用的metrics来进行监控的开源工具.相信很多工程都在 ...

  7. SpringBoot集成FreeMarker

    给大家简单介绍一下springboot 集成FreeMarker 过程很简单,5分钟即可. 首先在项目中增添依赖spring-boot-starter-freemarker pom文件代码如下: &l ...

  8. springboot集成swagger2测试接口

    springboot集成swagger2测试接口 1.需要的依赖 2.开始编写一个swagger2 3.演示效果图片 1.需要的依赖 <dependency><groupId> ...

  9. springboot 集成logback

    springboot 集成logback 1.application.properties配置文件指定logback.xml logging.config=classpath:logback.xml ...

最新文章

  1. 扩展typeof来判断js变量的类型
  2. 死磕java_死磕JavaScript-垃圾收集机制
  3. 【数据结构】——归并排序
  4. 每次新增页面复制粘贴?100多行源码的 element-ui 的新增组件功能教你解愁
  5. apache jmeter_Apache Server和JMeter调试
  6. 【经典重温】所有数据无需共享同一个卷积核!谷歌提出条件参数化卷积CondConv(附Pytorch复现代码)...
  7. 嵌套组合图echarts
  8. webRTC之智能指针std::unique_ptr::reset()使用(十四)
  9. vue跨域使用websocket_在vue中使用SockJS实现webSocket通信的过程
  10. MyBatis概述与架构
  11. 微信小程序学习笔记-(11)-仿糗事百科
  12. BestCoder Round #67 (div.2) N*M bulbs
  13. MAPDF.NET 电子书合集
  14. namecheap域名如何绑定IP
  15. SRTF最短剩余时间优先调度C语言实现
  16. symfony配置文件之三:routing和路由配置
  17. 高翔视觉SLAM十四讲(第二版)各种软件、库安装的以及报错解决方法
  18. Meld——目录文件对比工具
  19. mysql explain type_MySQL explain type详解
  20. h5 微信公众号获取当前位置

热门文章

  1. 疯狂kotlin讲义连载之Kotlin的基础类型--null安全
  2. 腾讯,百度,网易游戏,华为笔面经验
  3. 互联网协议入门(一)
  4. 网站维护:Linux服务器查看外网访问IP指令汇总
  5. 语音信号的分帧加窗的matlab实现
  6. c++内存管理优化之ptmalloc,tcmalloc,jemalloc使用实例
  7. C++类型转换: static_cast const_cast reinterpret_cast dynamic_cast
  8. ubuntu12.04LTS安装已经共享的打印机
  9. python实现e2lsh高维数据集k近邻搜索——实现流程
  10. Kotlin 普及度增加,代码质量比 Java 更高?