微服务监控之三:Prometheus + Grafana Spring Boot 应用可视化监控
一、Springboot增加Prometheus
1、Spring Boot 应用暴露监控指标,添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.0.26</version>
</dependency>
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'io.prometheus:simpleclient_spring_boot:0.0.26'
2、然后,在启动类 Application.java 添加如下注解:
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
3、配置文件设置
在application.xml里设置属性:spring.metrics.servo.enabled=false,
去掉重复的metrics,不然在prometheus的控制台的targets页签里,会一直显示此endpoint为down状态。
#应用可视化监控
management.security.enabled=false
spring.metrics.servo.enabled=false
4、访问:http://192.168.10.213:6010/prometheus,可以看到 Prometheus 格式的指标数据
二、自定义prometheus注解
2.1、自定义prometheus注解
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 "";
}
2.2、自定义prometheus切面
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
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....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();
}
}
}
2.3、被监控的方法上添加--自定义prometheus注解
@PrometheusMetrics
@PostMapping(value = "isBacklist")
@ApiOperation(value = "黑名单判断", notes = "是否在黑名单中,如果存在并且记录状态为2,则为黑名单,返回true,否则返回:false")
@Log
public RespResult<Boolean> isBacklist(@RequestBody BacklistReqDTO reqDTO) {
三、Prometheus 采集 Spring Boot 指标数据
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every seconds. Default is every minute.
evaluation_interval: 15s # Evaluate rules every seconds. The default is every minute.
# scrape_timeout is set to the global default (10s). # Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager: # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml" # 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' # metrics_path defaults to '/metrics'
# scheme defaults to 'http'. static_configs:
- targets: ['10.200.110.100:8080'] #此处填写 Spring Boot 应用的 IP + 端口号
docker run -d -p : \
-u root \
-v /opt/prometheus/tsdb:/etc/prometheus/tsdb \
-v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
--privileged=true prom/prometheus \
--storage.tsdb.path=/etc/prometheus/tsdb \
--storage.tsdb.retention=7d \
--config.file=/etc/prometheus/prometheus.yml
./prometheus --config.file=prometheus2.yml
结果:
duanxz@ubuntu:~/Downloads/prometheus-2.0.0.linux-amd64$ ./prometheus --config.file=prometheus2.yml
level=info ts=2018-06-19T08:27:47.222527495Z caller=main.go:215 msg="Starting Prometheus" version="(version=2.0.0, branch=HEAD, revision=0a74f98628a0463dddc90528220c94de5032d1a0)"
level=info ts=2018-06-19T08:27:47.222895906Z caller=main.go:216 build_context="(go=go1.9.2, user=root@615b82cb36b6, date=20171108-07:11:59)"
level=info ts=2018-06-19T08:27:47.223110655Z caller=main.go:217 host_details="(Linux 4.4.0-128-generic #154~14.04.1-Ubuntu SMP Fri May 25 14:58:51 UTC 2018 x86_64 ubuntu (none))"
level=info ts=2018-06-19T08:27:47.227443134Z caller=web.go:380 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2018-06-19T08:27:47.234616341Z caller=main.go:314 msg="Starting TSDB"
level=info ts=2018-06-19T08:27:47.244932582Z caller=targetmanager.go:71 component="target manager" msg="Starting target manager..."
level=info ts=2018-06-19T08:27:47.24608357Z caller=main.go:326 msg="TSDB started"
level=info ts=2018-06-19T08:27:47.246514727Z caller=main.go:394 msg="Loading configuration file" filename=prometheus2.yml
level=info ts=2018-06-19T08:27:47.247799187Z caller=main.go:371 msg="Server is ready to receive requests."
四、Grafana 可视化监控数据
五、常用Prometheus 表达式
参考:http://www.spring4all.com/article/265
微服务监控之三:Prometheus + Grafana Spring Boot 应用可视化监控的更多相关文章
- Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警
Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...
- 拒绝黑盒应用-Spring Boot 应用可视化监控
图文简介 逻辑关系 效果演示 快速开始 1.Spring Boot 应用暴露监控指标[版本 1.5.7.RELEASE] 首先,添加依赖如下依赖: <dependency> <gro ...
- 【微框架】之一:从零开始,轻松搞定SpringCloud微服务系列--开山篇(spring boot 小demo)
Spring顶级框架有众多,那么接下的篇幅,我将重点讲解SpringCloud微框架的实现 Spring 顶级项目,包含众多,我们重点学习一下,SpringCloud项目以及SpringBoot项目 ...
- 谁才是微服务赢家:Quarkus 与 Spring Boot
在容器时代("Docker 时代")Java 仍然处于领先地位,但哪个更好?Spring Boot 还是 Quarkus? 谁会最先进的?Spring Boot 或 Quarkus ...
- Spring Boot 2.x监控数据可视化(Actuator + Prometheus + Grafana手把手)
TIPS 本文基于Spring Boot 2.1.4,理论支持Spring Boot 2.x所有版本 众所周知,Spring Boot有个子项目Spring Boot Actuator,它为应用提供了 ...
- 从实践出发:微服务布道师告诉你Spring Cloud与Spring Boot他如何选择
背景 随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. 系统架 ...
- Springboot监控之二:Spring Boot Admin对Springboot服务进行监控
概述 Spring Boot 监控核心是 spring-boot-starter-actuator 依赖,增加依赖后, Spring Boot 会默认配置一些通用的监控,比如 jvm 监控.类加载.健 ...
- 从实践出发:微服务布道师告诉你Spring Cloud与Boot他如何选择
背景 随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. 系统架 ...
- Spring Boot Admin2.X监控的服务context-path问题
在使用Spring Boot Admin进行监控时,如果被监控的服务没有加context-path的话是不会有任何问题的,一旦服务加了context-path的配置,监控就会失败. 下图是正常情况的显 ...
随机推荐
- System.Zip
自XE2增加的System.Zip单元很好.注意事项: 1.文件压缩到文档后所使用的文件名会成为解压后的文件名,如果该文件名为指定文件名且无后缀名,那么解压出来的文件名也没有后缀名:
- DRF中两大视图基类APIView/GenericAPIView的介绍
(1)APIView rest_framework.views.APIView APIView是REST framework提供的所有视图的基类,继承自Django的View父类. APIView与V ...
- FusionCharts制作实时刷新图
转自:http://yklovejava-163-com.iteye.com/blog/1889949 下面介绍的是用FusionCharts制作实时刷新图的过程(FusionCharts确实太好用了 ...
- webpack 事件触发 按需加载
比较易懂, 方法简单 var util_sync = require('./util-sync.js') alert(util_sync.data) document.getElementById(& ...
- 浅析Postgres中的并发控制(Concurrency Control)与事务特性(上)(转)
这篇博客将MVCC讲的很透彻,以前自己懂了,很难给别人讲出来,但是这篇文章给的例子就让人很容易的复述出来,因此想记录一下,转载给更多的人 转自:https://www.cnblogs.com/flyi ...
- 开启 Android WebView 的安全浏览模式
Hybrid App(混合式开发)已经是每一个商业应用都会使用的开发手段.其最大的优势就是将一些可动态更新的内容页面使用 H5 开发,然后借用移动端原生系统提供的 WebView 控件加载进来.这种方 ...
- 01-名字管理系统.py
#-*- coding:utf-8 -*- #1.打印功能提示 print("-"*50) print("名字管理系统 V8.6") print("1 ...
- node.js 之爬虫
1. cheerio 与 request request:模拟客户端行为,对页面进行请求 cheerio:对服务器端返回的页面进行解析: var cheerio = require('cheerio' ...
- 利用你的Mission Control--设置快速回到桌面等操作
第一步:打开系统偏好设置 第二步:进入设置界面点击 Mission Control 第三:Mission Control界面 进入Hot corners(触发角) 第四:设置mac屏幕四个角的快捷键( ...
- 实战:向GitHub提交代码时触发Jenkins自动构建
当我们提交代码到GitHub后,可以在Jenkins上执行构建,但是每次都要动手去执行略显麻烦,今天我们就来实战Jenkins的自动构建功能,每次提交代码到GitHub后,Jenkins会进行自动构建 ...