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

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
简介: Spring Boot2.x-14 使用Prometheus + Grafana 实现可视化的监控

环境信息


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的版本


20190310225521380.png


# 下载 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会监控自己本身。

# 后台运行 & 
[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


20190310230934584.png


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

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


20190310231153698.png

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

查看prometheus规则


20190310231528335.png

查看监控对象


20190310231709915.png

SpringBoot集成Prometheus


20190310232509543.png


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.artisan</groupId>
  <artifactId>springbootPrometheusGrafana</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springbootPrometheusGrafana</name>
  <description>Artisan </description>
  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <optional>true</optional>
      <scope>true</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>


配置文件

spring: 
  application:
    name: springbootPrometheusGrafana
management:
  endpoints:
    web:
      exposure:
        include:  '*'
  metrics:
    tags:
       application: ${spring.application.name}


如果使用properties

spring.application.name=springbootPrometheusGrafana
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}


实例化MeterRegistryCustomizer

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

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



20190310233029949.png


服务器上启动该jar即可

java -jar springbootPrometheusGrafana-0.0.1-SNAPSHOT.jar &


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

增加如下配置:

#SpringBoot应用配置
  - job_name: 'springbootPrometheusGrafana'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['192.168.31.34:8080']


重启Prometheus

[root@artisan prometheus]# ./prometheus --config.file=prometheus.yml  &



访问 http://192.168.31.34:9090/targets


20190310233512234.png

查看配置文件



20190310233548394.png

服务自动发现

20190310233634591.png


Grafana 组件


官网:https://grafana.com/

下载地址: https://grafana.com/grafana/download

入门:http://docs.grafana.org/guides/getting_started/


20190310234526343.png


刚才也说了,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的版本


# 下载
[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


20190310235452223.png

Grafana 接入Prometheus 的数据

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

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

Step1 添加Prometheus 数据源


20190311000111504.png

选择 Prometheus


20190311000154147.png


Step2 设置相关信息


20190311003059651.png


Step3 导入想要的dashboards

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

可筛选

20190311002646456.png


20190311002315989.png

https://grafana.com/dashboards/4701


20190311003728172.png

导入

20190311003829142.png

20190311003945622.gif

20190311003211916.png

相关文章
|
2月前
|
Prometheus 监控 Cloud Native
Spring Boot 应用可视化监控
Spring Boot 应用可视化监控
24 0
|
2月前
|
监控 druid Java
Spring Boot3整合Druid(监控功能)
Spring Boot3整合Druid(监控功能)
78 1
|
3月前
|
监控 Java
HeartBeat监控springboot服务状态
HeartBeat监控springboot服务状态
|
4月前
|
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...
|
Prometheus 监控 Cloud Native
Springboot使用 prometheus监控
Springboot使用 prometheus监控
342 0
Springboot使用 prometheus监控
|
Prometheus 监控 Cloud Native
Springboot使用 prometheus监控
Springboot使用 prometheus监控
1543 0
Springboot使用 prometheus监控
|
Prometheus 监控 Cloud Native
SpringBoot使用prometheus监控
本文介绍SpringBoot如何使用Prometheus配合Grafana监控。 1.关于Prometheus Prometheus是一个根据应用的metrics来进行监控的开源工具。相信很多工程都在使用它来进行监控,有关详细介绍可以查看官网:https://prometheus.io/docs/introduction/overview/。
5308 0
|
1月前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
1月前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
100 2
|
1月前
|
前端开发 JavaScript Java
6个SpringBoot 项目拿来就可以学习项目经验接私活
6个SpringBoot 项目拿来就可以学习项目经验接私活
41 0