prometheus和granfana搭建监控系统

  1. docker 安装 
    1. 跳过
  2. docker镜像拉取加速,适用阿里云镜像
    1. 登陆阿里云镜像服务:https://cr.console.aliyun.com/
    2.  生成自己的加速地址
    3. 按照操作文档配置docker 
  3. docker方式启动prometheus
    1. # 定义自己的目录映射
      docker run -it -d -p 9090:9090 -v /**/**/prometheus/:/etc/prometheus/ prom/prometheus
    2. 启动后会默认使用 /etc/prometheus/prometheus.yaml 作为启动配置文件,这里我们已经在本地映射了该路径,修改本地对应文件即可
    3. 这里新增的表示 prometheus将要收集的指标地址为:http://192.168.1.108:8080/actuator/prometheusd
  4. docker方式启动granfana
    1. docker run -d --name=grafana -p 3000:3000 grafana/grafana
    2. 这里使用默认配置即可,打开本地地址:http://127.0.0.1:3000 即可看到granfana导航页
    3. 选择datasource为prometheus,并设置地址(prometheus所在机器地址):http://192.168.1.108:9090
    4. 如图所示,则granfana搭建成功
    5.  此时可以收集到 prometheus 默认指标
    6. 业务系统收集指标

      1. 搭建spring boot项目,跳过
      2. 引入pom
                <!-- 实现对 Actuator 的自动化配置 -->
        		<dependency>
        			<groupId>org.springframework.boot</groupId>
        			<artifactId>spring-boot-starter-actuator</artifactId>
        		</dependency>
        
        		<!-- Micrometer 对 Prometheus 的支持 -->
        		<dependency>
        			<groupId>io.micrometer</groupId>
        			<artifactId>micrometer-registry-prometheus</artifactId>
        		</dependency>
      3. 引入后可以实现actuator集成prometheus,收集各种指标(jvm相关、http、db等)
      4. application.properties配置
        spring.application.name=demo-application
        #表示开启所有指标收集
        management.endpoints.web.exposure.include=*
        management.metrics.tags.application=${spring.application.name}
      5. 启动服务,以上可以完成micrometer支持的指标收集,已经很全面了,看个http请求监控的例子
  5. 自定义指标收集
    1. 监控工具类demo
    2. package com.yihuu.camunda.demo.util;
      
      import io.prometheus.client.CollectorRegistry;
      import io.prometheus.client.Counter;
      import org.springframework.beans.BeansException;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.ApplicationContextAware;
      import org.springframework.stereotype.Component;
      
      import java.util.Map;
      import java.util.concurrent.ConcurrentHashMap;
      
      /**
       * @author yuhui
       */
      @Component
      public class YMonitor implements ApplicationContextAware {
      
          private static final byte[] LOCK = new byte[0];
      
          private static ApplicationContext context;
      
          private static Map<String, Counter> map = new ConcurrentHashMap<>();
      
          public static void inc(String key) {
              Counter counter = map.get(key);
              if (counter != null) {
                  counter.inc();
              } else {
                  Counter newCounter = Counter.build(key, key).create();
                  Counter old = map.putIfAbsent(key, newCounter);
                  if (old != null) {
                      old.inc();
                  } else {
                      synchronized (LOCK) {
                          CollectorRegistry collectorRegistry = context.getBean(CollectorRegistry.class);
                          newCounter.register(collectorRegistry);
                          newCounter.inc();
                      }
                  }
              }
          }
      
          @Override
          public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
              setContext(applicationContext);
          }
      
          private void setContext(ApplicationContext applicationContext) {
              context = applicationContext;
          }
      }
      
    3.   业务计数
    4.  @GetMapping("/start")
          public Object test1(Req req) {
      
              try {
                  Map<String, Object> params = new HashMap<>();
                  params.put("orderNo", req.getOrderNo());
                  params.put("orderStatus", req.getOrderStatus());
                  params.put("domain", req.getDomain());
                  params.put("request", req);
      
                  ProcessInstanceWithVariablesImpl processInstance = (ProcessInstanceWithVariablesImpl) runtimeService.startProcessInstanceByKey("no_ticket_refund", req.getOrderNo(), params);
                  String tips = processInstance.getVariables().getValue("tips", String.class);
                  return tips;
              } catch (Exception e) {
                  YMonitor.inc("TestController_test1_exception");
                  return null;
              } finally {
                  YMonitor.inc("TestController_test1");
              }
          }

    5. ab压测请求

      ab -n1000 -c 20 'http://127.0.0.1:8080/start?orderNo=123456&orderStatus=1&domain=xep'

    6. 查看监控


版权声明:本文为tianming_yueye原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。