Springboot使用 prometheus监控

简介: Springboot使用 prometheus监控

Springboot使用 prometheus监控

添加prometheus的Maven坐标

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

image.gif

启动类增加prometheus注解

package com.newcapec;
import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * @version V1.0
 * @Title: 应用主类
 * @ClassName: com.newcapec.SpringbootdemoApplication.java
 * @Description:
 * @Copyright 2016-2017  - Powered By 研发中心
 * @author: fly
 * @date:2017-03-15 8:01
 */
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@MapperScan("com.newcapec.dao.mapper")
public class SpringbootdemoApplication {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    public static void main(String[] args) {
        SpringApplication.run(SpringbootdemoApplication.class, args);
       /* SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringbootdemoApplication.class);
        //修改Banner的模式为OFF
        builder.bannerMode(Banner.Mode.OFF).run(args);*/
    }
}

image.gif

配置文件设置

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

image.gif

#应用可视化监控
management.security.enabled=false
spring.metrics.servo.enabled=false

image.gif

配置prometheus.yml,指定SpringBoot应用的ip和端口

    static_configs:
          - targets: ['IP:端口号']
    • image.gif

    自定义prometheus注解

    package com.newcapec.config.annotation;
    import java.lang.annotation.*;
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface PrometheusMetrics {
        /**
         *  默认为空,程序使用method signature作为Metric name
         *  如果name有设置值,使用name作为Metric name
         * @return
         */
        String name() default "";
    }

    image.gif

    自定义prometheus切面

    package com.newcapec.config.aspect;
    import com.newcapec.config.annotation.PrometheusMetrics;
    import groovy.util.logging.Slf4j;
    import io.prometheus.client.Counter;
    import io.prometheus.client.Histogram;
    import org.apache.commons.lang3.StringUtils;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import javax.servlet.http.HttpServletRequest;
    @Aspect
    @Component
    @Slf4j
    public class PrometheusMetricsAspect {
        private static final Counter requestTotal = Counter.build().name("couter_all").labelNames("api").help
                ("total request couter of api").register();
        private static final Counter requestError = Counter.build().name("couter_error").labelNames("api").help
                ("response Error couter of api").register();
        private static final Histogram histogram = Histogram.build().name("histogram_consuming").labelNames("api").help
                ("response consuming of api").register();
        // 自定义Prometheus注解的全路径
        @Pointcut("@annotation(com.newcapec.config.annotation.PrometheusMetrics)")
        public void pcMethod() {
        }
        @Around(value="pcMethod() && @annotation(annotation)")
        public Object MetricsCollector(ProceedingJoinPoint joinPoint, PrometheusMetrics annotation) throws Throwable {
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            PrometheusMetrics prometheusMetrics = methodSignature.getMethod().getAnnotation(PrometheusMetrics.class);
            if (prometheusMetrics != null) {
                String name;
                if (StringUtils.isNotEmpty(prometheusMetrics.name()) ){
                    name = prometheusMetrics.name();
                }else{
                    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                            .getRequest();
                    name = request.getRequestURI();
                }
                requestTotal.labels(name).inc();
                Histogram.Timer requestTimer = histogram.labels(name).startTimer();
                Object object;
                try {
                    object = joinPoint.proceed();
                } catch (Exception e) {
                    requestError.labels(name).inc();
                    throw e;
                } finally {
                    requestTimer.observeDuration();
                }
                return object;
            } else {
                return joinPoint.proceed();
            }
        }
    }

    image.gif被监控的方法上添加--自定义prometheus注解

    @RequestMapping(value ="/hello", method = RequestMethod.GET)
    @ResponseBody
    @PrometheusMetrics
    public String index() {
         return "Hello World";
    }

    image.gif

    prometheus的监控页面

    http://localhost:9090/targets

    image.gif20171123205904470.png


    多次访问 http://localhost:8888/web项目名/hello,然后在prometheus控制台查看相关metrics信息,couter_all,2个页签:Graph,Console

    image.gif

    参考来源:http://blog.csdn.net/zl1zl2zl3/article/details/75045005?locationNum=9&fps=1

    参考来源:http://blog.csdn.net/ericprotectearth/article/details/78581312

    目录
    相关文章
    |
    Prometheus 监控 Cloud Native
    SpringBoot+Prometheus+Grafana 实现自定义监控
    SpringBoot+Prometheus+Grafana 实现自定义监控
    |
    2天前
    |
    Prometheus 监控 Cloud Native
    微服务框架(十九)Spring Boot 可视化监控 Prometheus + Grafana
      此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Spring Boot 通过 micrometer 的监控门面,实现Prometheus + G...
    |
    2天前
    |
    Prometheus 监控 Cloud Native
    SpringBoot3 整合Prometheus + Grafana
    SpringBoot3 整合Prometheus + Grafana
    SpringBoot3 整合Prometheus + Grafana
    |
    2天前
    |
    Prometheus 监控 Cloud Native
    Spring Boot应用如何快速接入Prometheus监控
    Spring Boot应用如何快速接入Prometheus监控
    64 0
    |
    6月前
    |
    Prometheus 监控 Cloud Native
    Prometheus监控Spring Boot应用,自定义应用监控指标
    Prometheus监控Spring Boot应用,自定义应用监控指标
    120 0
    Prometheus监控Spring Boot应用,自定义应用监控指标
    |
    11月前
    |
    Prometheus 监控 Cloud Native
    使用Prometheus监控SpringBoot应用
    使用Prometheus监控SpringBoot应用
    110 0
    |
    12月前
    |
    Prometheus 监控 Cloud Native
    Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控
    Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控
    245 0
    |
    存储 JSON Prometheus
    Prometheus+SpringBoot应用监控全过程详解
    1. Prometheus是什么 Prometheus是一个具有活跃生态系统的开源系统监控和告警工具包。一言以蔽之,它是一套开源监控解决方案
    |
    Prometheus 运维 监控
    Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然……(3)
    Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然……(3)
    182 0
    Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然……(3)
    |
    数据采集 Prometheus 监控
    Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然……(2)
    Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然……(2)
    126 0
    Spring Boot + Prometheus + Grafana 打造可视化监控,一目了然……(2)