OS: Centos7 (防火墙已经关闭) IP: 192.168.31.34 APP: Spring Boot 2.1.3搭建的演示环境
官网: 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的版本
# 下载 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
配置文件官方说明: 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
太多了,真是没法看,还好有个弱弱的图形页面 (待会整合到Grafana 中就方便看了)
访问: http://192.168.31.34:9090/graph
选中某个指标,点击Execute . 超多指标可以查看 …
<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>
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}
方便起见,我们就在启动类中实例化吧
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即可
java -jar springbootPrometheusGrafana-0.0.1-SNAPSHOT.jar &
增加如下配置:
#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
查看配置文件
服务自动发现
官网:https://grafana.com/ 下载地址: https://grafana.com/grafana/download 入门:http://docs.grafana.org/guides/getting_started/
刚才也说了,Prometheus 的可视化功能比较弱,这里我们来接入Grafana 。
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
如何将 Prometheus 数据 添加到 Grafana 中,如下
Prometheus 官方指导 : https://prometheus.io/docs/visualization/grafana/
选择 Prometheus
看板地址: https://grafana.com/dashboards
可筛选
https://grafana.com/dashboards/4701
导入