# Micrometer 初体验

高性能计算
• 阅读 1291

为基于JVM的应用程序提供一个数据采集门面。监控系统有很多,Prometheus、Datadog等,每个系统都有自己的数据采集方式,实现方式各有不同,如果需要更换监控系统,代码改动很大。Micrometer 就可以在中间充当门面,代码层面之和 Micrometer 交互,再由 Micrometer 和监控系统对接。
io.micrometer 的三个模块

  • 核心模块 micrometer-core

    • 数据收集SPI 核心模块
  • 不同监控系统的实现模块 micrometer-registry-*

    • 如针对 Prometheus 的 micrometer-registry-prometheus,针对datadog的micrometer-registry-dog
    • 这些模块都依赖于core
  • 测试模块 micrometer-test

Springboot + micrometer +Prometheus+Grafana 采集数据

Springboot

配置pom

        <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>
        // 配置micrometer prometheus插件
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>

配置application.yml

management:
  endpoints:
    web:
      exposure:
        // 配置需要加载的断点
        include: health, metrics, trace, prometheus

编写测试接口

@RestController
public class TestController {
    private Counter counter;
    // 注入meterRegistry
    public TestController(MeterRegistry meterRegistry ) {
        this.counter = meterRegistry.counter("demo.http.requests.total", Tags.of("uri","/hello"));
    }

    @RequestMapping("/hello")
    public String hello() {
        counter.increment();
        return "Hello World";
    }
}

测试接口

curl http://localhost:8088/hello
Hello World%

// 查看health
curl http://localhost:8088/actuator/health

// 查看 metrics 指标,可以看到所有监控的指标。
// "demo.http.requests.total"是自定义的指标
curl http://localhost:8088/actuator/metrics
{
"names": [
"demo.http.requests.total",
"http.server.requests",
......
"tomcat.sessions.expired",
"tomcat.sessions.rejected"
]
}

// 此接口可以查询 metrics 指标的具体值
curl http://localhost:8088/actuator/metrics/demo.http.requests.total

// 此接口开放给 prometheus 采集metrics数据
curl http://localhost:8088/actuator/prometheus

Prometheus

  1. 配置prometheus.yml

在scrape_configs下增加配置

  • job_name:job名称
  • mtrics_paty:默认路径 /metrics ,prometheus监控springboot的路径是 /actuator/prometheus
  • static_configs:

    • targets 可以配置多个,[ip : port],应用的ip端口
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: "micrometer-test"
    # metrics_path defaults to '/metrics'
    metrics_path: "/actuator/prometheus"
    # scheme defaults to 'http'.
    static_configs:
      - targets: ["192.168.31.152:8088"]
  1. 重启Prometheus
在Prometheus根目录下直接执行
./prometheus
会自动加载同在根目录下的 prometheus.yml

或者 --conifg 指定配置文件
prometheus --conifg=/xx/xx/prometheus.yml
  1. 访问prometheus : localhost:9090

Grafana

Grafana 安装方法很多,任选一个即可,关键是启动 Grafana 后配置数据源和dashboard

  1. 配置数据源

搜索 prometheus 数据源,只需配置 url 为 prometheus 的 ip 端口即可

  1. 配置dashboard

直接引入官方的 JVM 仪表盘,id为4701,再选择刚才配置的数据源就可以导入了。
更多完整dashboard可以在Grafana Dashboard 找到

  1. 添加自定义 panel

springboot 中定义的指标demo.http.requests.total,可以添加panel。

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
4年前
springboot如何集成Prometheus如何暴露Histogram来获取P99等监控指标
背景springboot如何集成Prometheus我这里不做详细描述,要想了解集成过程,可以参考一下博客:SpringBoot使用Micrometer集成Prometheus监控Java应用性能(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fblog.cs
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
4年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
4年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa
Easter79 Easter79
4年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Easter79 Easter79
4年前
SpringBoot整合Redis乱码原因及解决方案
问题描述:springboot使用springdataredis存储数据时乱码rediskey/value出现\\xAC\\xED\\x00\\x05t\\x00\\x05问题分析:查看RedisTemplate类!(https://oscimg.oschina.net/oscnet/0a85565fa