本次主要记录如何使用prometheus监控Java微服务和nacos,以及自己发布指标到promethus

1. 监控nacos

由于nacos注册中心,本身也是基于Java开发的所以这里就放在一起对比着进行配置,对应nacos的监控非常简单,Prometheus官网上并没有提供exporter。

nacos 官方文档

Nacos 监控手册 | Nacos

1.1 首先打开nacos的metrics接口

打开nacos 配置文件 vim application.properties

增加 nacos.prometheus.metrics.enabled=true

prometheus的histogram指标可以画柱状图嘛 prometheus自定义指标_grafana


验证结果 访问192.168.3.111:8848/nacos/actuator/prometheus

prometheus的histogram指标可以画柱状图嘛 prometheus自定义指标_nacos_02


 1.2 添加Prometheus监控


- job_name: 'nacos-exporter'
    scrape_interval: 15s
    metrics_path: '/nacos/actuator/prometheus' ## 默认不写为metrics 对特殊的路径需要自己填写
    static_configs:
    - targets: ['192.168.3.111:8848']
      labels:
        instance: 111-nacos服务器


 重载配置 curl -X POST http://localhost:9090/-/reload

查看效果

prometheus的histogram指标可以画柱状图嘛 prometheus自定义指标_spring-boot_03


1.3 配置grafana监控


找到喜欢的模板id,13221

Dashboards | Grafana Labs

查看效果访问 http://192.168.3.112:3000/ 

prometheus的histogram指标可以画柱状图嘛 prometheus自定义指标_nacos_04


2.监控spring-boot

对spring-boot的监控主要依赖于actuator组件

2.1 spring-boot引入依赖

1.引入actuator依赖,版本跟随spring-boot-starter-parent版本


<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>


 2.2 暴漏prometheu端点

修改application.yml文件,暴漏actuator 端点,

简单配置

management:
  endpoints:
    web:
      exposure:
        include: '*' ##actuator暴漏的端口 * 为所有端口,也可以按需开放


进阶配置
management:
  endpoints:
    web:
      exposure:
        include: '*' ##actuator暴漏的端口
      base-path: /actuator ## 默认 为 /
  endpoint:
    shutdown:
      enabled: true
    health:
      show-details: always
  metrics:
    tags:
      application: ${spring.application.name}
    export:
      prometheus:
        enabled: true # 是否启用prometheus
  server:
    port: 7005 #自定义actuator端口 默认为服务端口
2.3.启动服务测试


http://localhost:7005/actuator/prometheus


prometheus的histogram指标可以画柱状图嘛 prometheus自定义指标_spring_05

2.4 配置 Prometheus监控

追加配置 vim ../prometheus/prometheus.yml

- job_name: 'springboot-exporter'
    scrape_interval: 15s
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['192.168.3.112:7005']
      labels:
        instance: 112-spring-boot微服务

重载配置 curl -X POST http://localhost:9090/-/reload

查看结果 访问 192.168.3.112:9090

prometheus的histogram指标可以画柱状图嘛 prometheus自定义指标_prometheus_06

2.5 配置grafana监控

找到喜欢的模板id,11378

Dashboards | Grafana Labs

查看效果访问 http://192.168.3.112:3000/ 

 

prometheus的histogram指标可以画柱状图嘛 prometheus自定义指标_spring-boot_07

3. spring-boot 注册自定义指标

3.1 引入依赖

 引入依赖

<!--         将指标转换成prometheus可使用的格式-->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

prometheus总共有四种数据类型,用于不同类型的数据

这里实现 Counter 类型,注册一个counter 当index方法发生调用是,count会记录调用次数

package com.legendyun.prometheus.controller;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;

/**
 * @title: IndexController
 * @description: TODO
 * @auther: zhangjianyun
 * @date: 2024/3/11 17:13
 */
@RestController
@RequestMapping("/v1")
public class IndexController {

    @Autowired
    MeterRegistry registry;

    private Counter counter_core;
    private Counter counter_index;

    @PostConstruct
    private void init(){
        counter_core = registry.counter("app_requests_method_core", "method", "IndexController.core","legend-label","legend12333333");
        counter_index = registry.counter("app_requests_method_index", "method", "IndexController.index","legend-label","legend4566666");
    }

    @RequestMapping(value = "/index")
    public Object index(){
        try{
//            counter_index.increment();
            registry.counter("app_requests_method_index", "method", "IndexController.index","legend-label","legend4566666").increment();
            // 同名的数据类型再次注册只会有一个
        } catch (Exception e) {
            return e;
        }
        return counter_index.count() + " index of springboot2-prometheus.";
    }

    @RequestMapping(value = "/core")
    public Object coreUrl(){
        try{
//            counter_core.increment();
            // 可以根据标签进行过滤
            registry.get("app_requests_method_core").counter().increment();
        } catch (Exception e) {
            return e;
        }
        return counter_core.count() + " coreUrl Monitor by Prometheus.";
    }

}

完整代码和其他类型数据可以查看我的GitHub

GitHub - ObstinateCloud/prometheus-test: 读取Prometheus数据