栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mac基于SpringBoot2+prometheus+ Grafana搭建监控系统的完整过程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mac基于SpringBoot2+prometheus+ Grafana搭建监控系统的完整过程

SpringBoot2+prometheus+ Grafana搭建监控系统是目前Java后台服务的一个大方向,微博、美团都大厂也都在用,相比其他监控体系,该方案更加完善和强大,同时还非常灵活。本章我们就来介绍一下如何在mac搭建一套系统来玩玩。因为要用三个组件,所以主要工作也分为三个部分:

1.快速建立一个新的SpringBoot2工程,并添加prometheus坐标。

2.下载安装、配置并启动prometheus。

3.下载安装并启动Grafana,同时在Grafana做一些设置,让我们可以看到监控数据。

接下来我们就从0开始完整实现该过程。

1.新建的SpringBoot2工程

使用idea新建一个pom工程,这个应该不用多说了,完成之后配置一下pom.xml文件;



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
         
    
    com.lqc
    monitor_test
    1.0-SNAPSHOT
    springboot2_prometheus
    springboot2_prometheus
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.micrometer
            micrometer-registry-prometheus
            1.1.3
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


这几个配置文件就够了。

然后在resource下新建一个配置文件:application.properties,内容为;

spring.application.name=springboot_prometheus
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
server.port=2022

这里的name非常重要,我们后面还会使用该名字,因此不用乱写。

然后建立一个Java类,Springboot2PrometheusApplication.java,并创建Main方法,完整代码如下:

package com.lqc;

import io.micrometer.core.instrument.MeterRegistry;
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;

@SpringBootApplication
public class Springboot2PrometheusApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot2PrometheusApplication.class, args);
	}
	@Bean
	MeterRegistryCustomizer configurer(
			@Value("${spring.application.name}") String applicationName) {
		return (registry) -> registry.config().commonTags("application", applicationName);
	}
}

然后启动一下,启动成功后,输入地址:http://localhost:2022/actuator/prometheus

此时会展示出很多信息,这就是SpringBoot2的actuator将很多基础的数据推送出来了。

到此,我们将服务放着就行了。

2.下载安装、配置并启动prometheus

首先下载并安装prometheus,下载地址是Download | Prometheus

 这里只有最新版的,我们直接下载下来就行,我下载的是prometheus-2.36.0.darwin-amd64.tar.gz。然后将其解压,接下来要修改配置文件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:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['127.0.0.1:9090']
###以下内容为SpringBoot应用配置
  - job_name: 'springboot_prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['127.0.0.1:2022']

注意这里的最后一个job_name要和我们的java服务的对应起来。之后就可以执行了,命令是;

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

在mac下初次执行可能会被“安全与隐私”功能拦截,我们将其设置为允许就行了。

之后输入http://localhost:9090/

就可以查看是否启动成功。之后选择Status下的Targets如下位置:

 之后如果正常看到的是如下内容:

这里的的两个就是我们配置的两个job,一个是prometheus自己,一个是我们的SpringBoot服务。

至此,第二步完成。

3.下载安装Grafana,并检查监控数据

我们继续下载Grafana,Download Grafana | Grafana Labs

完成之后,直接执行bin目录下的./grafana-server 即可,此时仍然可能被mac的安全系统拦截,选择允许即可。

启动成功之后,在浏览器输入:

http://localhost:3000/

即可,默认的用户名和密码是admin,此时可以看到如下的界面:

之后要为其配置数据源 ,设置这几个位置就行了:

 这里会自动执行一次测试,如果测试通过就ok了,

之后就是根据需要添加dashbaord了,操作如下:

此时会进入如下界面:

 

我们选择“Add a new panel”,然后可以看到下面图示:

 要生成指标图非常简单,我们只要在“metrics browser”输入我们需要的监控的指标名字就可以展示出来,然后选择“query”,这时候就给我们生成了一个实时监控图。

然后就可以一直添加需要的指标,就逐步将大盘给建立起来了。

 这些指标都是actutor推送出来的,prometheus是个实时数据库,将这些信息给缓存下来了,最后Grafana就执行各种展示了。

到这里我们就将整个流程走通了,如果要实际用还有很多不足,例如Grafana已关闭就将所有信息都丢了等等,。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938879.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号