Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化

Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化

    • Prometheus
      • 下载并运行Prometheus
      • 配置Prometheus
      • 启动Prometheus
    • SpringBoot2整合Prometeus(演示springboot版本为2.1.9.RELEASE)
      • 增加pom依赖
      • 配置application.properties
      • 增加配置文件
    • Grafana
      • 下载安装
      • Grafana 导入Prometheus 的数据
        • 增加数据源
        • 导入的dashboards

看了网上存在的普遍的版本,却在整合过程中出现了prometheus监控不到项目数据的情况。然后决定写此版本。

Prometheus

官方文档:https://prometheus.io/docs/introduction/overview/

下载并运行Prometheus

官方下载最新的Prometheus,下载地址:https://prometheus.io/download/
这里以linux 2.13.1版本为例,下载解压:

tar xvfz prometheus-2.13.1.linux-amd64.tar.gz
cd prometheus-2.13.1.linux-amd64

配置Prometheus

Prometheus配置保存为名为的文件prometheus.yml:

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'
# 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=` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']

  # 需要监控的项目
  #项目名
  - job_name: 'xxx'
    metrics_path: '/prometheus'
	#需监控项目地址
    static_configs:
      - targets: ['localhost:8080']

启动Prometheus

./prometheus --config.file=prometheus.yml

访问安装路径:http://localhost:9090/graph,得到如下页面:
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第1张图片
①图中Configuration,可查看配置状态;
②图中Targets,可查看监控目标状态;
点击图中Targets,如下图:
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第2张图片
上图:①对应下图配置中配置的①,自身监控默认/metrics
②对应下图配置的②,配置路劲为’/prometheus’
③State:up成功,down失败
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第3张图片

SpringBoot2整合Prometeus(演示springboot版本为2.1.9.RELEASE)

增加pom依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-registry-prometheus</artifactId>
		</dependency>

配置application.properties

#prometheus配置
spring.application.name = prometheus-demo
management.security.enabled = false
management.metrics.export.prometheus.enabled = true
management.metrics.export.prometheus.step = 1ms
management.metrics.export.prometheus.descriptions = true
management.web.server.auto-time-requests = true

management.endpoint.prometheus.enabled = true
management.endpoints.web.exposure.include = *
#刚开始没配置web-path,prometheus一直监控不到
management.endpoints.web.base-path = /

增加配置文件

增加配置文件MyPrometheusConfig.class

/**
 * @author luhong
 * @version 1.0
 * @Description
 * @date 2019/10/26
 * @updateby
 * @updatedate
 * @since 1.0
 */
@Configuration
public class MyPrometheusConfig {
	/**
     * @author: luhong
     * @Description 取配置文件application.properties中应用名称注册
     * @Date 15:58 2019/10/26
     * @param applicationName
     * @return org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer
     */
    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
        return (registry) -> registry.config().commonTags("application", applicationName);
    }
}

启动或部署项目,查看http://localhost:9090/graph下的Targets,是否成功

Grafana

官方文档:https://grafana.com/docs/

下载安装

wget https://dl.grafana.com/oss/release/grafana-6.4.3.linux-amd64.tar.gz 
tar -zxvf grafana-6.4.3.linux-amd64.tar.gz  

该软件包中没有初始化脚本或安装脚本。
要配置Grafana custom.ini,请在conf文件夹中添加一个名为的配置文件,并覆盖中定义的任何设置conf/defaults.ini。

通过执行启动Grafana ./bin/grafana-server web。该grafana-server二进制需要工作目录是安装根目录(二进制文件以及public文件夹的位置)。

其它安装参考官方文档:https://grafana.com/docs/installation/rpm/

要运行Grafana,请打开浏览器并转到http:// localhost:3000 /。如果您未配置其他端口,则3000是Grafana侦听的默认HTTP 端口。

Grafana 导入Prometheus 的数据

增加数据源

Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第4张图片
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第5张图片
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第6张图片
按照上面配置好数据源

导入的dashboards

dashboards地址:https://grafana.com/grafana/dashboards
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第7张图片
登陆导入得下图:
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第8张图片
填写10280,得到如下:
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第9张图片
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第10张图片
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第11张图片
Spring Boot 2.1X+Prometheus +Grafana实现监控及可视化_第12张图片
各监控指标的含义参考:https://blog.csdn.net/fu_zhongyuan/article/details/87973174

你可能感兴趣的:(java,监控)