SpringBoot+Prometheus&Granfana实现微服务自动化监控

1、简介

springboot相比大家都很熟悉了,接下来简单介绍下Prometheus&Granfana。

1.1 Prometheus

Prometheus 是由 SoundCloud 开源监控告警解决方案,使用Go语言开发。

官网:https://prometheus.io/

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

架构:

Prometheus architecture

基本原理:

Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控。输出被监控组件信息的HTTP接口被叫做exporter 。

1.2 Granfana

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

官网:https://grafana.com/

下载地址:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1

2、安装使用

本次统一使用windows版

2.1 Prometheus安装

下载windows版: https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.windows-amd64.zip

解压后,运行prometheus.exe,浏览器访问127.0.0.1:9090

prometheus本地环境安装成功~

2.2 Granfana安装

下载windows版本:https://dl.grafana.com/oss/release/grafana-7.5.7.windows-amd64.zip

下载后解压,在bin目录下,打开grafana-server.exe,浏览器访问http://127.0.0.1:3000/

我的测试版本已内置Prometheus数据源

如果没有,可以手动添加

3、与SpringBoot集成

3.1 引入依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

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

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

3.2  新增配置文件application.yml

server:
  port: 8888

spring:
  application:
    name: springboot-prometheus

management:
  metrics:
    export:
      prometheus:
        enabled: true
        step: 1m
        descriptions: true
    tags:
      application: ${spring.application.name}
  web:
    server:
      auto-time-requests: true
  endpoints:
    prometheus:
      id: springmetrics
    web:
      exposure:
        include: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics

3.3 启动类

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 PrometheusApplication {

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

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

启动PrometheusApplication,访问 http://127.0.0.1:8888/actuator/prometheus,可以获取到采集信息。

4、Prometheus&Granfana配置

4.1 Prometheus配置

修改prometheus.yml,添加如下内容:

- job_name: 'springboot_prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['127.0.0.1:8888']

重启prometheus, 查看status->Targets:

prometheus Graph中已可以看到监控数据。

4.2 Granfana配置

导入JVM (Micrometer) dashboard: https://grafana.com/grafana/dashboards/4701

点击导入,监控大屏就出来了

是不是很酷炫!!!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值