前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控

Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控

作者头像
小小工匠
发布2021-08-17 16:01:02
3.4K0
发布2021-08-17 16:01:02
举报
文章被收录于专栏:小工匠聊架构

文章目录

环境信息

OS: Centos7 (防火墙已经关闭) IP: 192.168.31.34 APP: Spring Boot 2.1.3搭建的演示环境


Prometheus 组件

官网: https://prometheus.io/

下载地址: https://prometheus.io/download/

官方文档: https://prometheus.io/docs/prometheus/latest/getting_started/

Prometheus是一套开源的监控&报警&时间序列数据库的组合,基于应用的metrics来进行监控的开源工具 。

更多信息请参考官网介绍: https://prometheus.io/docs/introduction/overview/


下载 & 安装

官网下载速度太慢,我这里就没有使用最新版本,而是从 http://cactifans.hi-www.com 下载了 prometheus-2.1.0的版本

代码语言:javascript
复制
# 下载 prometheus-2.1.0.linux-amd64.tar.gz
[root@artisan ~]# wget http://cactifans.hi-www.com/prometheus/prometheus-2.1.0.linux-amd64.tar.gz
--2019-03-10 21:57:56--  http://cactifans.hi-www.com/prometheus/prometheus-2.1.0.linux-amd64.tar.gz
Resolving cactifans.hi-www.com (cactifans.hi-www.com)... 222.186.135.67
Connecting to cactifans.hi-www.com (cactifans.hi-www.com)|222.186.135.67|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25271154 (24M) [application/octet-stream]
Saving to: ‘prometheus-2.1.0.linux-amd64.tar.gz’

100%[===========================================================================================================================================>] 25,271,154  6.34MB/s   in 3.7s   

2019-03-10 21:58:00 (6.55 MB/s) - ‘prometheus-2.1.0.linux-amd64.tar.gz’ saved [25271154/25271154]
# 解压prometheus-2.1.0.linux-amd64.tar.gz
[root@artisan ~]# tar -xvzf  prometheus-2.1.0.linux-amd64.tar.gz 
prometheus-2.1.0.linux-amd64/
prometheus-2.1.0.linux-amd64/consoles/
prometheus-2.1.0.linux-amd64/consoles/index.html.example
prometheus-2.1.0.linux-amd64/consoles/node-cpu.html
prometheus-2.1.0.linux-amd64/consoles/node-disk.html
prometheus-2.1.0.linux-amd64/consoles/node-overview.html
prometheus-2.1.0.linux-amd64/consoles/node.html
prometheus-2.1.0.linux-amd64/consoles/prometheus-overview.html
prometheus-2.1.0.linux-amd64/consoles/prometheus.html
prometheus-2.1.0.linux-amd64/console_libraries/
prometheus-2.1.0.linux-amd64/console_libraries/menu.lib
prometheus-2.1.0.linux-amd64/console_libraries/prom.lib
prometheus-2.1.0.linux-amd64/prometheus.yml
prometheus-2.1.0.linux-amd64/LICENSE
prometheus-2.1.0.linux-amd64/NOTICE
prometheus-2.1.0.linux-amd64/prometheus
prometheus-2.1.0.linux-amd64/promtool
# 为了方便使用,给解压后的目录 建个软连接
[root@artisan ~]# ln -s  prometheus-2.1.0.linux-amd64  prometheus

通过指定配置文件prometheus.yml启动Prometheus

配置文件官方说明: https://prometheus.io/docs/prometheus/latest/configuration/configuration/

默认情况下,Prometheus会监控自己本身。

代码语言:javascript
复制
# 后台运行 & 
[root@artisan prometheus]# ./prometheus --config.file=prometheus.yml  & 
[1] 9015
[root@artisan prometheus]# level=info ts=2019-03-10T15:06:43.257570596Z caller=main.go:225 msg="Starting Prometheus" version="(version=2.1.0, branch=HEAD, revision=85f23d82a045d103ea7f3c89a91fba4a93e6367a)"
level=info ts=2019-03-10T15:06:43.257669987Z caller=main.go:226 build_context="(go=go1.9.2, user=root@6e784304d3ff, date=20180119-12:01:23)"
level=info ts=2019-03-10T15:06:43.257704431Z caller=main.go:227 host_details="(Linux 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 artisan (none))"
level=info ts=2019-03-10T15:06:43.257735315Z caller=main.go:228 fd_limits="(soft=1024, hard=4096)"
level=info ts=2019-03-10T15:06:43.263798112Z caller=main.go:499 msg="Starting TSDB ..."
level=info ts=2019-03-10T15:06:43.291174989Z caller=web.go:383 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2019-03-10T15:06:43.402481602Z caller=main.go:509 msg="TSDB started"
level=info ts=2019-03-10T15:06:43.402579292Z caller=main.go:585 msg="Loading configuration file" filename=prometheus.yml
level=info ts=2019-03-10T15:06:43.404207598Z caller=main.go:486 msg="Server is ready to receive web requests."
level=info ts=2019-03-10T15:06:43.404378748Z caller=manager.go:59 component="scrape manager" msg="Starting scrape manager..."

# 查看进程, 9015端口,刚才启动的时候也输出了这个端口
[root@artisan prometheus]# ps -ef|grep prometheus |grep -v grep
root       9015   7675  0 23:06 pts/0    00:00:00 ./prometheus --config.file=prometheus.yml

查看采集到的性能指标

访问 http://192.168.31.34:9090/metrics

太多了,真是没法看,还好有个弱弱的图形页面 (待会整合到Grafana 中就方便看了)

访问: http://192.168.31.34:9090/graph

选中某个指标,点击Execute . 超多指标可以查看 …

查看prometheus规则


查看监控对象


SpringBoot集成Prometheus

pom.xml

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.1.3.RELEASEversion>
		<relativePath /> 
	parent>
	<groupId>com.artisangroupId>
	<artifactId>springbootPrometheusGrafanaartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>springbootPrometheusGrafananame>
	<description>Artisan description>

	<properties>
		<java.version>1.8java.version>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-actuatorartifactId>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>
		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>io.micrometergroupId>
			<artifactId>micrometer-registry-prometheusartifactId>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-devtoolsartifactId>
			<optional>trueoptional>
			<scope>truescope>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>

project>

配置文件

代码语言:javascript
复制
spring: 
  application:
    name: springbootPrometheusGrafana
    
management:
  endpoints:
    web:
      exposure:
        include:  '*'
  metrics:
    tags:
       application: ${spring.application.name}

如果使用properties

代码语言:javascript
复制
spring.application.name=springbootPrometheusGrafana
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}

实例化MeterRegistryCustomizer

方便起见,我们就在启动类中实例化吧

代码语言:javascript
复制
package com.artisan;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import io.micrometer.core.instrument.MeterRegistry;

@SpringBootApplication
public class SpringbootPrometheusGrafanaApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootPrometheusGrafanaApplication.class, args);
	}

	@Bean
	MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
		return (registry) -> registry.config().commonTags("application", applicationName);
	}

}

打包,上传到服务器上

右键–Run As – Maven build ,输入 clean package

服务器上启动该jar即可

代码语言:javascript
复制
java -jar springbootPrometheusGrafana-0.0.1-SNAPSHOT.jar &

Prometheus 修改配置文件prometheus.yml 接入该工程

增加如下配置:

代码语言:javascript
复制
#SpringBoot应用配置
  - job_name: 'springbootPrometheusGrafana'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['192.168.31.34:8080']

重启Prometheus

代码语言:javascript
复制
[root@artisan prometheus]# ./prometheus --config.file=prometheus.yml  & 

访问 http://192.168.31.34:9090/targets

查看配置文件

服务自动发现


Grafana 组件

官网:https://grafana.com/ 下载地址: https://grafana.com/grafana/download 入门:http://docs.grafana.org/guides/getting_started/

刚才也说了,Prometheus 的可视化功能比较弱,这里我们来接入Grafana 。

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

  • 展示方式:快速灵活的客户端图表,面板插件有许多不同方式的可视化指标和日志,官方库中具有丰富的仪表盘插件,比如热图、折线图、图表等多种展示方式;
  • 数据源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;
  • 通知提醒:以可视方式定义最重要指标的警报规则,Grafana将不断计算并发送通知,在数据达到阈值时通过Slack、PagerDuty等获得通知;
  • 混合展示:在同一图表中混合使用不同的数据源,可以基于每个查询指定数据源,甚至自定义数据源;
  • 注释:使用来自不同数据源的丰富事件注释图表,将鼠标悬停在事件上会显示完整的事件元数据和标记;
  • 过滤器:Ad-hoc过滤器允许动态创建新的键/值过滤器,这些过滤器会自动应用于使用该数据源的所有查询。

下载 & 安装 & 启动Grafana

官网下载速度太慢,我这里就没有使用最新版本,而是从 http://cactifans.hi-www.com 下载了 grafana-5.4.2-1.x86_64.rpm的版本

代码语言:javascript
复制
# 下载
[root@artisan ~]# wget http://cactifans.hi-www.com/grafana/grafana-5.4.2-1.x86_64.rpm
--2019-03-10 23:44:28--  http://cactifans.hi-www.com/grafana/grafana-5.4.2-1.x86_64.rpm
Resolving cactifans.hi-www.com (cactifans.hi-www.com)... 222.186.135.67
Connecting to cactifans.hi-www.com (cactifans.hi-www.com)|222.186.135.67|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 54991977 (52M) [application/x-redhat-package-manager]
Saving to: ‘grafana-5.4.2-1.x86_64.rpm’

100%[===========================================================================================================================================>] 54,991,977  5.20MB/s   in 9.5s   

2019-03-10 23:44:37 (5.51 MB/s) - ‘grafana-5.4.2-1.x86_64.rpm’ saved [54991977/54991977]

# yum本地安装
[root@artisan ~]# yum localinstall grafana-5.4.2-1.x86_64.rpm
Loaded plugins: fastestmirror, langpacks
Examining grafana-5.4.2-1.x86_64.rpm: grafana-5.4.2-1.x86_64
Marking grafana-5.4.2-1.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package grafana.x86_64 0:5.4.2-1 will be installed
--> Finished Dependency Resolution
base/7/x86_64                                                                                                                                                 | 3.6 kB  00:00:00     
extras/7/x86_64                                                                                                                                               | 3.4 kB  00:00:00     
extras/7/x86_64/primary_db                                                                                                                                    | 180 kB  00:00:00     
updates/7/x86_64                                                                                                                                              | 3.4 kB  00:00:00     
updates/7/x86_64/primary_db                                                                                                                                   | 2.5 MB  00:00:00     

Dependencies Resolved

=====================================================================================================================================================================================
 Package                                Arch                                  Version                                   Repository                                              Size
=====================================================================================================================================================================================
Installing:
 grafana                                x86_64                                5.4.2-1                                   /grafana-5.4.2-1.x86_64                                151 M

Transaction Summary
=====================================================================================================================================================================================
Install  1 Package

Total size: 151 M
Installed size: 151 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : grafana-5.4.2-1.x86_64                                                                                                                                            1/1 
### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
 sudo /bin/systemctl daemon-reload
 sudo /bin/systemctl enable grafana-server.service
### You can start grafana-server by executing
 sudo /bin/systemctl start grafana-server.service
POSTTRANS: Running script
  Verifying  : grafana-5.4.2-1.x86_64                                                                                                                                            1/1 

Installed:
  grafana.x86_64 0:5.4.2-1                                                                                                                                                           

Complete!

# 启动 
[root@artisan ~]# systemctl start grafana-server
# 设置为开机启动
[root@artisan ~]# systemctl enable grafana-server
Created symlink from /etc/systemd/system/multi-user.target.wants/grafana-server.service to /usr/lib/systemd/system/grafana-server.service.
# 查看进程
[root@artisan ~]# ps -ef|grep grafana-server |grep -v grep
grafana    9542      1  2 23:49 ?        00:00:00 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid --packaging=rpm cfg:default.paths.logs=/var/log/grafana cfg:default.paths.data=/var/lib/grafana cfg:default.paths.plugins=/var/lib/grafana/plugins cfg:default.paths.provisioning=/etc/grafana/provisioning
[root@artisan ~]# 

配置文件 /etc/grafana/grafana.ini , 默认3000端口,按需修改

访问 http://192.168.31.34:3000/login

默认的用户名和密码为 admin/admin


Grafana 接入Prometheus 的数据

如何将 Prometheus 数据 添加到 Grafana 中,如下

Prometheus 官方指导 : https://prometheus.io/docs/visualization/grafana/

Step1 添加Prometheus 数据源

选择 Prometheus


Step2 设置相关信息


Step3 导入想要的dashboards

看板地址: https://grafana.com/dashboards

可筛选

https://grafana.com/dashboards/4701

导入

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/03/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 环境信息
  • Prometheus 组件
    • 下载 & 安装
      • 通过指定配置文件prometheus.yml启动Prometheus
        • 查看采集到的性能指标
          • 查看prometheus规则
            • 查看监控对象
            • SpringBoot集成Prometheus
              • pom.xml
                • 配置文件
                  • 实例化MeterRegistryCustomizer
                    • 打包,上传到服务器上
                      • Prometheus 修改配置文件prometheus.yml 接入该工程
                      • Grafana 组件
                        • 下载 & 安装 & 启动Grafana
                          • Grafana 接入Prometheus 的数据
                            • Step1 添加Prometheus 数据源
                            • Step2 设置相关信息
                            • Step3 导入想要的dashboards
                        相关产品与服务
                        Grafana 服务
                        Grafana 服务(TencentCloud Managed Service for Grafana,TCMG)是腾讯云基于社区广受欢迎的开源可视化项目 Grafana ,并与 Grafana Lab 合作开发的托管服务。TCMG 为您提供安全、免运维 Grafana 的能力,内建腾讯云多种数据源插件,如 Prometheus 监控服务、容器服务、日志服务 、Graphite 和 InfluxDB 等,最终实现数据的统一可视化。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档