Actuator + Prometheus + Grafana搭建微服务监控平台

[TOC]


前言

关于Actuator

对Spring Boot监控能力有过了解的小伙伴都应该知道Spring Boot Actuator这个子项目,它为应用提供了强大的监控能力。从Spring Boot 2.x开始,Actuator将底层改为Micrometer,提供了更强、更灵活的监控能力。Micrometer是一个监控门面,可以类比成监控界的 Slf4j 。借助Micrometer,应用能够对接各种监控系统,例如本文所要介绍的:Prometheus

关于Prometheus

Prometheus是一个由SoundCloud开发的开源系统监控+告警+时序列数据库(TSDB),Prometheus大部分组件使用Go语言编写,是Google BorgMon监控系统的开源版本。目前在CNCF基金会托管,并已成功孵化。在开源社区Prometheus目前也是相当活跃,在性能上Prometheus也足够支撑上万台规模的集群。

Prometheus的功能:

  • 用度量名和键值对识别时间序列数据的多维数据模型
  • 拥有灵活的查询语言:PromQL
  • 不依赖分布式存储,单个服务器节点是自治的
  • 通过基于HTTP的pull方式采集时序数据
  • 可以通过中间网关进行时序列数据的推送
  • 支持通过服务发现或者静态配置来发现目标服务对象
  • 支持多种多样的图表和界面展示,比如Grafana等

更多内容参考:官方文档GitHub仓库

关于Grafana

Grafana 是一款采用 GO 语言编写的开源应用,支持跨平台度量分析和可视化 + 告警。可以通过将采集的数据查询然后可视化地展示,并及时通知。Grafana 支持多种数据源和展示方式,总而言之是一款强大酷炫的监控指标可视化工具。

更多内容参考:官方文档GitHub仓库


创建项目

本文的主要目的是实现微服务的监控,简单了解了上述工具的概念后,我们就来动手实践一下。首先创建一个简单的Spring Boot项目,其主要依赖如下:

<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>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
  • Tips:这里如果想要对接其他的监控系统,只需要更改依赖的包名。例如想对接 Influx ,则将依赖改为 micrometer-registry-influx 即可。

编辑项目配置:

server:
  port: 9562
spring:
  application:
    # 指定应用名
    name: prometheus-demo
management:
  endpoints:
    web:
      exposure:
        # 将 Actuator 的 /actuator/prometheus 端点暴露出来
        include: ‘prometheus‘
  metrics:
    tags:
      # 为指标设置一个Tag,这里设置为应用名,Tag是Prometheus提供的一种能力,从而实现更加灵活的筛选
      application: ${spring.application.name}

完成以上步骤后,进行一个简单的测试,看看端点是否能正常返回监控数据。启动项目,访问/actuator/prometheus端点。正常情况下会返回如下内容:

# HELP process_start_time_seconds Start time of the process since unix epoch.
# TYPE process_start_time_seconds gauge
process_start_time_seconds{application="prometheus-demo",} 1.577697308142E9
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_memory_used_bytes{application="prometheus-demo",id="direct",} 16384.0
# HELP tomcat_sessions_expired_sessions_total
# TYPE tomcat_sessions_expired_sessions_total counter
tomcat_sessions_expired_sessions_total{application="prometheus-demo",} 0.0
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.006
jvm_gc_pause_seconds_count{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.032
jvm_gc_pause_seconds_count{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 0.008
# HELP jvm_gc_pause_seconds_max Time spent in GC pause
# TYPE jvm_gc_pause_seconds_max gauge
jvm_gc_pause_seconds_max{action="end of minor GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.006
jvm_gc_pause_seconds_max{action="end of major GC",application="prometheus-demo",cause="Metadata GC Threshold",} 0.032
jvm_gc_pause_seconds_max{action="end of minor GC",application="prometheus-demo",cause="Allocation Failure",} 0.008
# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 0.0
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 1.3801776E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} 3.522832E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 6860800.0
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.9782928E7
jvm_memory_used_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 4825568.0
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{application="prometheus-demo",level="info",} 7.0
logback_events_total{application="prometheus-demo",level="trace",} 0.0
logback_events_total{application="prometheus-demo",level="warn",} 0.0
logback_events_total{application="prometheus-demo",level="debug",} 0.0
logback_events_total{application="prometheus-demo",level="error",} 0.0
# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds{application="prometheus-demo",} 30.499
# HELP jvm_buffer_count_buffers An estimate of the number of buffers in the pool
# TYPE jvm_buffer_count_buffers gauge
jvm_buffer_count_buffers{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_count_buffers{application="prometheus-demo",id="direct",} 2.0
# HELP system_cpu_count The number of processors available to the Java virtual machine
# TYPE system_cpu_count gauge
system_cpu_count{application="prometheus-demo",} 6.0
# HELP jvm_threads_peak_threads The peak live thread count since the Java virtual machine started or peak was reset
# TYPE jvm_threads_peak_threads gauge
jvm_threads_peak_threads{application="prometheus-demo",} 22.0
# HELP tomcat_sessions_alive_max_seconds
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds{application="prometheus-demo",} 0.0
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 1.5204352E7
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 1.31596288E8
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} 3.7879808E7
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 6881280.0
jvm_memory_committed_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.76685056E8
jvm_memory_committed_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 5373952.0
# HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool
# TYPE jvm_buffer_total_capacity_bytes gauge
jvm_buffer_total_capacity_bytes{application="prometheus-demo",id="mapped",} 0.0
jvm_buffer_total_capacity_bytes{application="prometheus-demo",id="direct",} 16384.0
# HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC
# TYPE jvm_gc_live_data_size_bytes gauge
jvm_gc_live_data_size_bytes{application="prometheus-demo",} 1.3801776E7
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
# TYPE jvm_memory_max_bytes gauge
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 1.5204352E7
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Old Gen",} 2.841116672E9
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Metaspace",} -1.0
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Code Cache",} 2.5165824E8
jvm_memory_max_bytes{application="prometheus-demo",area="heap",id="PS Eden Space",} 1.390411776E9
jvm_memory_max_bytes{application="prometheus-demo",area="nonheap",id="Compressed Class Space",} 1.073741824E9
# HELP jvm_threads_daemon_threads The current number of live daemon threads
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="prometheus-demo",} 18.0
# HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{application="prometheus-demo",state="runnable",} 8.0
jvm_threads_states_threads{application="prometheus-demo",state="new",} 0.0
jvm_threads_states_threads{application="prometheus-demo",state="timed-waiting",} 2.0
jvm_threads_states_threads{application="prometheus-demo",state="blocked",} 0.0
jvm_threads_states_threads{application="prometheus-demo",state="waiting",} 12.0
jvm_threads_states_threads{application="prometheus-demo",state="terminated",} 0.0
# HELP jvm_gc_memory_promoted_bytes_total Count of positive increases in the size of the old generation memory pool before GC to after GC
# TYPE jvm_gc_memory_promoted_bytes_total counter
jvm_gc_memory_promoted_bytes_total{application="prometheus-demo",} 8296848.0
# HELP tomcat_sessions_active_max_sessions
# TYPE tomcat_sessions_active_max_sessions gauge
tomcat_sessions_active_max_sessions{application="prometheus-demo",} 0.0
# HELP tomcat_sessions_created_sessions_total
# TYPE tomcat_sessions_created_sessions_total counter
tomcat_sessions_created_sessions_total{application="prometheus-demo",} 0.0
# HELP jvm_gc_memory_allocated_bytes_total Incremented for an increase in the size of the young generation memory pool after one GC to before the next
# TYPE jvm_gc_memory_allocated_bytes_total counter
jvm_gc_memory_allocated_bytes_total{application="prometheus-demo",} 1.36924824E8
# HELP process_cpu_usage The "recent cpu usage" for the Java Virtual Machine process
# TYPE process_cpu_usage gauge
process_cpu_usage{application="prometheus-demo",} 0.10024585094452443
# HELP system_cpu_usage The "recent cpu usage" for the whole system
# TYPE system_cpu_usage gauge
system_cpu_usage{application="prometheus-demo",} 0.38661791030714154
# HELP tomcat_sessions_active_current_sessions
# TYPE tomcat_sessions_active_current_sessions gauge
tomcat_sessions_active_current_sessions{application="prometheus-demo",} 0.0
# HELP jvm_classes_loaded_classes The number of classes that are currently loaded in the Java virtual machine
# TYPE jvm_classes_loaded_classes gauge
jvm_classes_loaded_classes{application="prometheus-demo",} 7195.0
# HELP http_server_requests_seconds
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0
http_server_requests_seconds_sum{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.012429856
# HELP http_server_requests_seconds_max
# TYPE http_server_requests_seconds_max gauge
http_server_requests_seconds_max{application="prometheus-demo",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.012429856
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="prometheus-demo",} 2.841116672E9
# HELP jvm_threads_live_threads The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live_threads gauge
jvm_threads_live_threads{application="prometheus-demo",} 22.0
# HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution
# TYPE jvm_classes_unloaded_classes_total counter
jvm_classes_unloaded_classes_total{application="prometheus-demo",} 1.0
# HELP tomcat_sessions_rejected_sessions_total
# TYPE tomcat_sessions_rejected_sessions_total counter
tomcat_sessions_rejected_sessions_total{application="prometheus-demo",} 0.0

该端点返回的数据是Prometheus需要使用的。每一项都有相应的注释解释其含义,相信不难看懂。例如:

# HELP jvm_memory_used_bytes The amount of used memory
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{application="prometheus-demo",area="heap",id="PS Survivor Space",} 0.0

表示:prometheus-demo 应用堆内存中的 PS Survivor Space 区域占用的空间是 0.0 个字节。


安装Prometheus服务

接下来就是需要在服务器上安装Prometheus服务,用于从微服务暴露的监控端点中采集监控数据。为了简单起见,我这里采用docker的安装方式,其他安装方式可以参考 官方安装文档

首先为Prometheus准备一个配置文件:

[[email protected] ~]# mkdir /etc/prometheus
[[email protected] ~]# vim /etc/prometheus/prometheus.yml
scrape_configs:
# 任意写,建议英文,不要包含特殊字符
- job_name: ‘spring‘
  # 多久采集一次数据
  scrape_interval: 15s
  # 采集时的超时时间
  scrape_timeout: 10s
  # 采集的端点
  metrics_path: ‘/actuator/prometheus‘
  # 被采集的服务地址,即微服务的ip及端口
  static_configs:
  - targets: [‘192.168.1.252:9562‘]

该配置文件的目的是让Prometheus服务自动每隔15秒请求 http://192.168.1.252:9562/actuator/prometheus 。更多配置项参考:Prometheus Configuration官方文档

最后通过docker启动Prometheus服务,命令如下:

[[email protected] ~]# docker run -d -p 9090:9090 -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

启动成功后,正常情况下访问http://{ip}:9090,就可以看到Prometheus的首页:

点击 Insert metric at cursor ,即可选择监控指标;点击 Graph ,即可让指标以图表方式展示;点击Execute 按钮,即可看到类似下图的结果:

功能说明:

  • Insert metric at cursor:选择展示的指标
  • Graph:让指标以图形展示
  • Execute:绘制指标图表信息
  • Add Graph:绘制更多指标图表

Grafana可视化

上一小节我们已经成功搭建了Prometheus服务,并简单介绍了Prometheus自带的监控数据可视化界面,然而使用体验并不好,功能也比较少。下面我们来集成Grafana实现更友好、更贴近生产的监控数据可视化平台。

同样需要在服务器上安装Grafana服务,为了简单起见,我这里依旧采用docker的安装方式。其他安装方式可以参考 官方安装文档

使用docker只需要一行命令就可以启动Grafana,如下:

[[email protected] ~]# docker run -d -p 3000:3000 grafana/grafana

配置监控数据源

Grafana启动成功后,访问http://{ip}:3000/login进行登录,默认账户密码均为admin

登录成功后,首页如下:

首先需要添加监控数据的来源,点击首页中的Add data source ,即可看到类似如下的界面:

这里点击Prometheus,即可看到类似如下界面,在这里配置Prometheus服务相关的信息:

保存成功后会有如下提示:


创建监控Dashboard

点击导航栏上的 + 按钮,并点击Dashboard,将会看到类似如下的界面:

点击 Add Query ,即可看到类似如下的界面:

在红框标记的位置添加指标查询,指标的取值详见Spring Boot应用的 /actuator/prometheus 端点,例如jvm_memory_used_bytesjvm_threads_states_threadsjvm_threads_live_threads 等。

Grafana会给你较好的提示,并且支持较为复杂的计算,例如聚合、求和、平均等。如果想要绘制多个线条,可点击Add Query 按钮。如上图所示,笔者为图表绘制了两条线,分别代表daemon以及peak线程。

点击下图的按钮,并填入Title,即可设置图表标题:

若需要为Dashboard添加新的图表则点击上图中的左上角按钮:

并按下图步骤操作即可:

如果需要保存该Dashboard,则点击右上角的保存按钮即可:


Dashboard市场

至此,我们已经成功将Grafana与Prometheus集成,实现了较为丰富的图表展示——将关心的监控指标放置到Dashboard上,并且非常灵活!然而,这个配置的操作虽然不难,但还是挺费时间的。

那么是否有配置好的又强大、又通用、拿来即用的Dashboard呢?答案是肯定的!前往 Grafana Lab - Dashboards ,输入关键词即可搜索指定Dashboard:

如上图所示,可以找到若干款以 Prometheus 作为数据源,支持Micrometer的Dashboard。下面,简单演示一下如何使用 JVM(Micrometer) 这个Dashboard。点击 JVM(Micrometer) 进入Dashboard详情介绍页,如下图所示:

如图已详细描述了该Dashboard的特性、配置。其中的management.metrics.tags.application ,前面安装Prometheus服务时已经配置过了。该页的右上角用红框标注的 4701 是一个非常重要的数字,因为这是该Dashboard的id。

回到Grafana的首页,我们来导入这个Dashboard,按下图步骤操作:

输入后即可看到类似如下的界面,选择数据源,并点击Import:

此时,即可看到类似如下的界面,我们常关心的指标该Dashboard均已支持:

在上方的选项栏中可以选择查看不同的服务/应用:

此外,还有一些比较好用的Dashboard,可以自行了解一下这里就不赘述了:

原文地址:https://blog.51cto.com/zero01/2463452

时间: 2024-07-30 09:43:44

Actuator + Prometheus + Grafana搭建微服务监控平台的相关文章

微服务监控平台-缓存设计

一,架构图及其说明 1,UI数据:页面 2,一级缓存:内存 3,二级缓存:硬盘文件系统 4,定时任务:同步数据库与缓存中的数据 5,数据源:数据库 二,场景分析 加缓存之前的数据信息流: 浏览器发出数据请求后,服务器后端接收到数据请求,开始通过数据源连接读取各种数据后,通过业务逻辑层处理成需要的逻辑数据后返回给浏览器.浏览器更新数据模型展现数据. 三,上述场景存在的问题 1.  每次获取数据都要走相同的数据流程来获取数据,如果遇到源数据宕机.或者逻辑性能降低情况时页面效果会非常的差. 2.  每

【译文】用Spring Cloud和Docker搭建微服务平台

by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-microservices.html This blog series will introduce you to some of the foundational concepts of building a microservice-based platform using Spring Cloud

微服务管理平台nacos虚拟ip负载均衡集群模式搭建

一.Nacos简介 Nacos是用于微服务管理的平台,其核心功能是服务注册与发现.服务配置管理. Nacos作为服务注册发现组件,可以替换Spring Cloud应用中传统的服务注册于发现组件,如:Eureka.consul等,支持服务的健康检查. Nacos作为服务配置中心,可以替换Spring Cloud Config. 当然Nacos作为一个微服务管理平台,除了面向spring Cloud,还支持很多其他的微服务基础设施,如:docker.dubbo.kubernetes等.除了核心的服务

SpringCloud从入门到进阶(四)——使用SpringBoot搭建微服务

内容 SpringBoot整合SpringCloud的Eureka.Zuul等组件,快速实现简单易懂且具有服务熔断.负载均衡的分布式架构1.0,体验微服务的魅力. 版本 IDE:IDEA 2017.2.2 x64 JDK:1.8.0_171 manve:3.3.3 SpringBoot:1.5.9.RELEASE SpringCloud:Dalston.SR1 适合人群 ?Java开发人员 说明 转载请说明出处:SpringCloud从入门到进阶(四)--使用SpringBoot搭建微服务 参考

【3分钟就会系列】使用Ocelot+Consul搭建微服务吧!

原文:[3分钟就会系列]使用Ocelot+Consul搭建微服务吧! 一.什么Ocelot? API网关是一个服务器,是系统的唯一入口.API 网关一般放到微服务的最前端,并且要让API 网关变成由应用所发起的每个请求的入口.这样就可以明显的简化客户端实现和微服务应用程序之间的沟通方式.从面向对象设计的角度看,它与外观模式类似.API网关封装了系统内部架构,为每个客户端提供一个定制的API.它可能还具有其它职责,如身份验证.监控.负载均衡.缓存.请求分片与管理.静态响应处理. Ocelot 是一

mPass 微服务开发平台

mPass (Microservice Pass) 基于SpringBoot2.x.SpringCloud并采用前后端分离的企业级微服务,多租户系统架构微服务开发平台 mPaaS(Microservice PaaS)为租户业务开发.测试.运营及运维开源框架,能有效降低技术门槛.减少研发成本.提升开发效率,协助企业快速搭建稳定高质量的微服务应用;同时还集合各种微服务治理功能和监控功能.模块包括:企业级的认证系统.开发平台.应用监控.慢sql监控.统一日志.单点登录.Redis分布式高速缓存.配置中

多租户系统微服务开发平台

目录 项目总体架构图 基础业务模块 项目开发更新进度 运维架构图 服务简述 基于SpringBoot2.x.SpringCloud并采用前后端分离的企业级微服务,多租户系统架构微服务开发平台 mPaaS(Microservice PaaS)为租户业务开发.测试.运营及运维开源框架,能有效降低技术门槛.减少研发成本.提升开发效率,协助企业快速搭建稳定高质量的微服务应用;同时还集合各种微服务治理功能和监控功能.模块包括:企业级的认证系统.开发平台.应用监控.慢sql监控.统一日志.单点登录.Redi

Jenkins-k8s-helm-harbor-githab-mysql-nfs微服务发布平台实战

基于 K8S 构建 Jenkins 微服务发布平台 实现汇总: 发布流程设计讲解 准备基础环境 K8s环境(部署Ingress Controller,CoreDNS,Calico/Flannel) 部署代码版本仓库Gitlab 配置本地Git上传测试代码,创建项目到Gitlab 部署pinpoint 全链路监控系统(提前修改Dockerfile,打包镜像上传) 部署镜像仓库Harbor(开启helm仓库) master节点部署helm应用包管理器(配置本地helm仓库,上传helm包) 部署K8

微服务监控案例之一

     首先,您需要了解什么是微服务架构设计,同时了解相关微服务与Docker介绍, 微服务架构的本质,是把整体的业务拆分成很多有特定明确功能的服务,通过很多分散的小服务之间的配合,去解决更大,更复杂的问题.对被拆分后的服务进行分类和管理,彼此之间使用统一的接口来进行交互.      微服务的特点决定了功能模块的部署是分布式的,以往在单应用环境下,所有的业务都在同一个服务器上,如果服务器出现错误和异常,我们只要盯住一个点,就可以快速定位和处理问题,但是在微服务的架构下,大部分功能模块都是单独部