springboot 集成prometheus监控平台详解

今天我们开始分享springboot 集成prometheus监控平台,从这个纬度纵深分析监控平台的使用及原理,持续有十多篇文章依次分析。先使用再原理、先客户端再服务端、先web接口监控再软硬件服务监控。下面我们开始springboot集成prometheus.

1、springboot 引入 prometheus相关jar包

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

        <!-- prometheus 监控相关jar包 -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <version>1.1.4</version>
        </dependency>

特别注意,boot 版本 2.1.6.RELEASE,对应的prometheus  版本!否则会有问题。

2、.yml 配置文件配置:

server:
  port: 8081
  servlet:
    context-path: /nandao-pc-api


#prometheus监控平台配置
management:
  endpoints:
    web:
      exposure:
        include: "*"
        exclude: configprops
  endpoint:
    health:
      show-details: ALWAYS
  metrics:
    tags:
      application: ${server.servlet.context-path}

spring:
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 300MB

3、业务代码里监控某个接口:

/**
 * @author wanghuainan
 * @date 2020/9/8
 */
@RestController
@RequestMapping("/promethues/")
@Slf4j
public class PrometheusController {

    @Autowired
    MeterRegistry registry;

    /**
     * 指标类型设置
     */
    private Counter counter_core;
    private Counter counter_index;
    private AtomicInteger app_online_count;

    /**
     * 服务启动时创建自定义指标
     */
    @PostConstruct
    private void init(){
        counter_core = registry.counter("app_requests_method_count", "method", "PrometheusController.core");
        counter_index = registry.counter("app_requests_method_count", "method", "PrometheusController.index");
        app_online_count = registry.gauge("app_online_count", new AtomicInteger(0));
    }

   
    /**
     * 监控平台是否可用,没调用一次就记录一次,每次调用就加一
     * @return
     */
    @GetMapping("testIsUsable")
    public R testIsUsable() {
        counter_index.increment();
        return R.ok(counter_index.count() + " index of springboot-prometheus.");
    }
    /**
     * 监控平台核心接口请求次数
     * @return
     */
    @GetMapping("testIsCore")
    public R testIsCore() {
        counter_core.increment();
        return R.ok(counter_core.count() + " index of springboot-prometheus.");
    }

    /**
     * 测试实时在线人数,动态数据,每次请求数据可能都不一样
     * @return
     */
    @RequestMapping(value = "/online")
    public Object online(){
        int people = 0;
        try {
            people = new Random().nextInt(2000);
            app_online_count.set(people);
        } catch (Exception e){
            return e;
        }
        //return R.error(HuobiResultCode.PARAM_ERROR);
        return "current online people: " + people;
    }

}

4、启动boot服务后,访问监控的数据,接口是:

http://127.0.0.1:8081/nandao-pc-api/actuator/prometheus

或者浏览器访问:

 这些访问访问到的数据都是监控平台默认的指标数据,后期我们会详细分析。

5、访问一个监控的业务接口后,再次访问监控的数据:

 再次访问:http://127.0.0.1:8081/nandao-pc-api/actuator/prometheus

以上可知监控到了自定义指标的数据,下篇我们分享,通过切面监控某个服务所有接口的请求次数、时间、异常次数等,敬请期待! 

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寅灯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值