Prometheus

Prometheus 架构

Prometheus 架构.png
Prometheus Server
  • Prometheus 的核心,做 Metrics 数据监控的,用数字表示的信息都可以用 Prometheus Server 采集,比如系统的流量,服务的响应时间;
  • Prometheus Server 根据配置完成数据的采集、服务的发现、数据的存储;
  • HTTP Servver:把收集到 Prometheus Server 的数据暴露出去,供前端的应用使(Prometheus web UI 和)
Service Discovery
  • 服务的发现,通过 Service Discovery,Prometheus Server 就知道从哪里采集数据;
  • Service Discovery 有两种方式:1)静态的,通过文件告知从哪里拿 Metrics 的数据;2)动态的,通过 zookeeper 等配置中心,配置中心里的数据变化的时候,Prometheus Server 也跟着变,从不同的地方抓取数据;
Pushgateway
  • 支持 push 模式的,有些数据并不是一直存在的,比如定时任务的数据,Prometheus Server 来抓的时候,正好定时任务没起,就抓不到数据,这中情况,定时事务就需要把它产生的数据,push 到 Pushgateway,Prometheus Server 从 Pushgateway 中 pull 数据;
  • 除了Pushgateway 中的数据需要应用系统推过来之外,其他的数据都是 Prometheus Server 根据配置到各个应用系统 pull 的;
Prometheus web UI
  • Prometheus 自带的图形化界面,可以把 Prometheus Server 中收集的格式化的数据展示出来,这个页面很简陋,为了解决这个问题,有一个项目叫 Grafna;
Grafna
  • 更高级的图形界面,很炫酷的图表 ;
API Client
  • 可以自己写服务,调 Prometheus 中的服务,自己实现定制化的业务逻辑;
PromQL
  • Prometheus web UI 、Grafna、API Client 和 Prometheus Server 通信时使用的语言,和 SQL 类似,和 SQL 不同的是,这个语言只能查询,Prometheus Server 所有的数据都是 Prometheus Server 自己 pull 来的;
Alertmanager
  • 报警用的;
  • 报警的方式有很多种:Email、微信、钉钉、还可以调指定的接口;
  • Prometheus Server 会根据指定的规则推送给 Alertmanager;
  • 不是有数被 push 到 Alertmanager 就会有报警,它会再评估几个周期,比如 5 个周期,如果 5 个周期都是要报警,才会真正的去报警,防止网络抖动造成的误报;

Prometheus 安装

  • 一旦装了 Prometheus Server,Pushgateway、Prometheus web UI 和 Alertmanager 就都装上了,Grafna 需要单独装一下;
  • Prometheus 的安装,是通过 docker-compose 的编排,同时安装了 Prometheus 和 Grafana,通过命令 sudo docker-compose -f docker-compose.yml up
docker-compose.yml
version: "3"
services:
  prometheus:
    image: prom/prometheus:v2.4.3
    container_name: 'prometheus'
    volumes:
    - ./prometheus/:/etc/prometheus/
    ports:
    - '9092:9090'
  grafana:
    image: grafana/grafana:5.2.4
    container_name: 'grafana'
    ports:
    - '3000:3000'
    volumes:
    - ./grafana/config/grafana.ini:/etc/grafana/grafana.ini
    - ./grafana/provisioning/:/etc/grafana/provisioning/
    env_file:
    - ./grafana/config.monitoring
    depends_on:
    - prometheus
Prometheus 的配置文件 | monitoring/prometheus/prometheus.yml
  • 172.18.0.1 是宿主机的 IP,宿主机有好几个 IP,这个 IP 和 Docker 容器中的 IP 一个样子;
global:
  scrape_interval:   15s
  
scrape_configs:
- job_name:       'springboot-app'

  scrape_interval: 10s
  metrics_path: '/actuator/prometheus'

  static_configs:
  - targets: ['172.18.0.1:9082']
    labels:
      application: 'springboot-app'

- job_name: 'prometheus'

  scrape_interval: 5s

  static_configs:
  - targets: ['localhost:9090']

改造被监控的微服务 | orderApi

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

<!-- 在 actuator 的基础上,暴露一个 prometheus 的端口 -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.yml
server:
  port: 9082

management:
  endpoints:
    # 这是 promethus 暴露的端口
    promethus:
      enable: true
    # 这是 actuator 暴露的端口
    web:
      exposure:
        include:
          - prometheus
          - info
          - health
让 promethus 的端点不受安全机制的约束直接访问到
package com.lixinlei.security.order.config;

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

@Configuration
public class ActuatorSecurityConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                // 所有的监控端点都不受 Spring Security 的约束
                .requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
                .anyRequest().authenticated();
    }

}

微服务给 prometheus 提供的数据

  • http://localhost:9082/actuator/prometheus
从 prometheus 中看这些数据
  • http://localhost:9092/graph 中输入从 http://localhost:9082/actuator/prometheus 看到的属性,比如 jvm_threads_states_threads,然后点 Execute;

prometheus web UI 提供的能力优先,一般会搭配 Grafana 和 Prometheus Server 使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值