Spring Boot2 集成 Prometheus 和 Grafana 实现微服务监控入门

前言
随着微服务越来越多,系统的稳定运行和监控越来越重要,我们需要随时掌控系统的运行情况。因此,对系统的实时监控以及可视化展示甚至及时告警通知,就成了架构设计的必须能力。以前使用Zabbix,但是随着技术不断的更新,新技术更能适用现在技术需求。本文基于Spring Boot2使用Prometheus和Grafana在不入侵系统代码的情况下实现系统监控。

一 Prometheus

1.1 简介

Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB)。Prometheus使用Go语言开发,是Google BorgMon监控系统的开源版本。
2016年由Google发起Linux基金会旗下的原生云基金会(Cloud Native Computing Foundation), 将Prometheus纳入其下第二大开源项目。
Prometheus目前在开源社区相当活跃。
Prometheus和Heapster(Heapster是K8S的一个子项目,用于获取集群的性能数据。)相比功能更完善、更全面。Prometheus性能也足够支撑上万台规模的集群。

1.2 主要特点

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

1.3 架构图

二 Grafana 

2.1 简介

Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。

2.2 主要特点

  • 可视化:快速和灵活的客户端图形具有多种选项。面板插件为许多不同的方式可视化指标和日志。
  • 报警:可视化地为最重要的指标定义警报规则。Grafana将持续评估它们,并发送通知。
  • 通知:警报更改状态时,它会发出通知。接收电子邮件通知。
  • 动态仪表盘:使用模板变量创建动态和可重用的仪表板,这些模板变量作为下拉菜单出现在仪表板顶部。
  • 混合数据源:在同一个图中混合不同的数据源!可以根据每个查询指定数据源。这甚至适用于自定义数据源。
  • 注释:注释来自不同数据源图表。将鼠标悬停在事件上可以显示完整的事件元数据和标记。
  • 过滤器:过滤器允许您动态创建新的键/值过滤器,这些过滤器将自动应用于使用该数据源的所有查询。

三 实现微服务监控

3.1 Spring Boot2集成配置

注意本文章使用SpringBoot2.3.0版本。

3.1.1 添加依赖

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

		<!-- prometheus监控 -->
		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-registry-prometheus</artifactId>
		</dependency>

3.1.2 配置 application.properties

server.port=8087
server.servlet.context-path=/frms

# prometheus 监控配置
spring.application.name=frms
management.endpoints.web.exposure.include=prometheus
management.endpoint.health.show-details=always
management.metrics.tags.application=${spring.application.name}

3.1.3 启动服务

访问地址:http://localhost:8087/frms/actuator/prometheus,如图:

3.2 安装Prometheus

3.2.1 下载安装包

下载地址点击这里:https://prometheus.io/download/,本文下载的是Windows版本:prometheus-2.25.0.windows-amd64.zip

3.2.2 修改prometheus.yml

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# 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: 'frms'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    metrics_path: frms/actuator/prometheus
    static_configs:
    - targets: ['127.0.0.1:8087']

3.2.3 启动Prometheus

点击prometheus.exe启动,如图:

输入地址,访问可视化界面:http://localhost:9090/targets,如图:

3.3 安装Grafana

3.3.1 下载安装包

下载地址点击这里:https://grafana.com/grafana/download,本文用到的是 Windows 版本:grafana-7.4.3.windows-amd64.zip。如图:

3.3.2 启动Grafana

进入/bin目录下,点击grafana-server.exe启动,如图:

输入地址,访问可视化界面:http://localhost:3000,默认账号密码是admin/admin,如图:

3.3.3 设置数据源

填写name,在填写url,点击保存并测试

3.3.4 监控JVM

至此Spring Boot2集成Prometheus和Grafana实现微服务监控入门暂时完结,后续还会添加其他功能。

猜你喜欢

转载自blog.csdn.net/lovelichao12/article/details/114337647