加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

SpringBoot Actuator — 埋点和监控

发布时间:2022-08-03 10:11:07 所属栏目:PHP教程 来源:互联网
导读:1. 数据埋点 监控机器环境的性能和业务流程或逻辑等各项数据,并根据这些数据生成对应的指标,那么我们就称为数据埋点。比如我们想知道某个接口调用的 TPS、机器 CPU 的使用率,这些都可以用到数据埋点 2. Micrometer Micrometer 为流行的各种监控系统提供了
  1. 数据埋点
  监控机器环境的性能和业务流程或逻辑等各项数据,并根据这些数据生成对应的指标,那么我们就称为数据埋点。比如我们想知道某个接口调用的 TPS、机器 CPU 的使用率,这些都可以用到数据埋点
 
 
 
 
 
 
 
  2. Micrometer
  Micrometer 为流行的各种监控系统提供了一个简单的门面(类似于日志门面) —— 提供了与供应商无关的接口(counters,timers,gauges等),这些接口称为 meter 接口,其由 MeterRegistry 创建并保存,可理解为 MeterRegistry 是个集合里面存储了各种 meter 的度量数据,下面展示最简单的 counter 接口的使用
 
 
  2.1 简单使用
  其还有 timers、gauges 等接口,自行查阅
 
  // 创建一个 meter 注册中心
  MeterRegistry registry = new SimpleMeterRegistry();
 
  // 创建一个名为 test 度量
  Counter counter = meterRegistry.counter("test");
 
  // 让这个度量的计数加 1
  counter.increment();
  就是如此简单,比如在调用指定接口的时候,可以使用 counter 接口来度量调用的次数,频率等等
 
 
 
  2.2 命名规范
  Micrometer 命名用 . 分隔小写单词字符,在接入其他监控系统时会自动将命名转成其适应的格式(或者可重写一个 NamingConvention 转换器来覆盖默认命名转换)。而且还支持多标签来量化,即有了多维度的度量,使统计更加丰富。下面简单地举例命名规范:
 
  # 表示 http 请求
  Counter counter = meterRegistry.counter("http.server.requests");
 
  # 表示 http 请求中,请求 user 模块
  Counter counter = meterRegistry.counter("http.server.requests", "user");
 
  # 表示 http 请求中,请求 user 模块中,请求 login 登录方法
  Counter counter = meterRegistry.counter("http.server.requests", "user", "login");
 
 
 
 
 
 
  3. SpringBoot Actuator
  SpringBoot Actuator 其底层使用了 Mircometer ,可度量 SpringBoot 应用和获取它的各项指标,可通过 HTTP 或 JMX 来调用 Actuator 暴露的各种端点,然后就可以获取一个正在运行中的应用的内部状态
 
 
  当然内部指标并不是所有都可以向外暴露的,所以我们得有选择的开放,或者加入权限校验之后才能获取如下内容:
 
  有那些可配置的属性
  各依赖包的日志级别
  占用了多少内存
  HTTP 埋点被请求了多少次
  应用本身以及协作的外部服务的健康状态
  ......
 
  3.1 添加依赖
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>2.4.3</version>
  </dependency>
 
 
  3.2 基础配置
  management:
    server:
      port: 9090 # 一般启动独立端口(默认和应用端口一致),启用后源端口不可查
    endpoints:
      web:
        base-path: /actuator # 默认前缀路径,可修改
        exposure:
          include: health,info,metrics # 向外暴露的端点,可用通配符('*',需要单引号)
          exclude: env,heapdump # 排除暴露的端点
 
 
  3.3 查看可消费的端点
  可先用 HTTP 访问 localhost:9090/actuator 来获取 HATEOAS(可简单理解为暴露的端点文档),它是所有可暴露端点的地图,可通过属性对应的地址来获取的指标,内容如下:
 
  {
    "_links": {
      "self": {
        "href": "http://localhost:9090/actuator",
        "templated": false
      },
      "health-path": {
        "href": "http://localhost:9090/actuator/health/{*path}",
        "templated": true
      },
      "health": {
        "href": "http://localhost:9090/actuator/health",
        "templated": false
      },
      "info": {
        "href": "http://localhost:9090/actuator/info",
        "templated": false
      }
    }
  }
 
 
  3.4 获取应用的基本信息
  消费对应的指标,就在地址后面加上名字即可。应用的基本信息是需要自己配置的,没有默认值,所以首次访问 可访问 localhost:9090/actuator/info 是一个空 json。
 
  可配置 info 开头的属性,比如联系方式,应用的作用等等,其配置和消费结果如下:
 
  info:
    contact:
      email: support@howl.com
      phone: 123456789
    description: actuator test application
    
    
  # 消费结果
  {
    "contact": {
      "email": "support@howl.com",
      "phone": 123456789
    },
    "description": "actuator test application"
  }
 
 
  3.5 健康指标
  首先尝试访问 localhost:9090/actuator/health,就可以获取指标内容了
 
  {
    "status": "UP"
  }
  这里显示的是一个或多个健康指示器的聚合状态,即当前应用和与之交互的外部系统(数据库,消息队列,Eureka等等)的健康状态的聚合状态。我们可以添加如下配置来获取健康指示器的内聚状态
 
  management:
    endpoint:
      health:
        show-details: always
        
        
  # 消费结果
  {
    "status": "UP",
    "components": {
      "diskSpace": {
        "status": "UP",
        "details": {
          "total": 493516484608,
          "free": 436332154880,
          "threshold": 10485760,
          "exists": true
        }
      },
      "ping": {
        "status": "UP"
      }
    }
  }
  SpringBoot 的自动配置功能可以确保只有与应用交互的组件才会显示到 health 里面
 
 
 
  3.6 指标端点 metrics
  可访问如下地址来获取 Actuator 提供的开箱即用的指标分类,包括了内存、处理器、垃圾收集、HTTP请求等指标
 
  http://localhost:9090/actuator/metrics
 
 
  # 消费结果
  {
    "names": [
      "http.server.requests",
      "jvm.buffer.count",
      "jvm.buffer.memory.used",
      "jvm.buffer.total.capacity",
      "jvm.classes.loaded",
      "jvm.classes.unloaded",
      "jvm.gc.live.data.size",
      "jvm.gc.max.data.size",
      "jvm.gc.memory.allocated",
      "jvm.gc.memory.promoted",
      "jvm.gc.pause",
      "jvm.memory.committed",
      "jvm.memory.max",
      "jvm.memory.used",
      "jvm.threads.daemon",
      "jvm.threads.live",
      "jvm.threads.peak",
      "jvm.threads.states",
      "logback.events",
      "process.cpu.usage",
      "process.start.time",
      "process.uptime",
      "system.cpu.count",
      "system.cpu.usage",
      "tomcat.sessions.active.current",
      "tomcat.sessions.active.max",
      "tomcat.sessions.alive.max",
      "tomcat.sessions.created",
      "tomcat.sessions.expired",
      "tomcat.sessions.rejected"
    ]
  }
 
 
 
  # 可用标准地址 + 指标端点名字 来消费某个指标端点
  http://localhost:9090/actuator/metrics/http.server.requests
 
  
 
  4. 实例
  统计 /user/login 接口被调用的次数,meterRegistry 注册中心在自动配置中加入容器可直接使用  1. 数据埋点
  监控机器环境的性能和业务流程或逻辑等各项数据,并根据这些数据生成对应的指标,那么我们就称为数据埋点。比如我们想知道某个接口调用的 TPS、机器 CPU 的使用率,这些都可以用到数据埋点
 
 
 
  2. Micrometer
  Micrometer 为流行的各种监控系统提供了一个简单的门面(类似于日志门面) —— 提供了与供应商无关的接口(counters,timers,gauges等),这些接口称为 meter 接口,其由 MeterRegistry 创建并保存,可理解为 MeterRegistry 是个集合里面存储了各种 meter 的度量数据,下面展示最简单的 counter 接口的使用
 
 
  2.1 简单使用
  其还有 timers、gauges 等接口,自行查阅
 
  // 创建一个 meter 注册中心
  MeterRegistry registry = new SimpleMeterRegistry();
 
  // 创建一个名为 test 度量
  Counter counter = meterRegistry.counter("test");
 
  // 让这个度量的计数加 1
  counter.increment();
  就是如此简单,比如在调用指定接口的时候,可以使用 counter 接口来度量调用的次数,频率等等
 
 
 
  2.2 命名规范
  Micrometer 命名用 . 分隔小写单词字符,在接入其他监控系统时会自动将命名转成其适应的格式(或者可重写一个 NamingConvention 转换器来覆盖默认命名转换)。而且还支持多标签来量化,即有了多维度的度量,使统计更加丰富。下面简单地举例命名规范:
 
  # 表示 http 请求
  Counter counter = meterRegistry.counter("http.server.requests");
 
  # 表示 http 请求中,请求 user 模块
  Counter counter = meterRegistry.counter("http.server.requests", "user");
 
  # 表示 http 请求中,请求 user 模块中,请求 login 登录方法
  Counter counter = meterRegistry.counter("http.server.requests", "user", "login");
 
 
 
 
 
 
  3. SpringBoot Actuator
  SpringBoot Actuator 其底层使用了 Mircometer ,可度量 SpringBoot 应用和获取它的各项指标,可通过 HTTP 或 JMX 来调用 Actuator 暴露的各种端点,然后就可以获取一个正在运行中的应用的内部状态
 
 
  当然内部指标并不是所有都可以向外暴露的,所以我们得有选择的开放,或者加入权限校验之后才能获取如下内容:
 
  有那些可配置的属性
  各依赖包的日志级别
  占用了多少内存
  HTTP 埋点被请求了多少次
  应用本身以及协作的外部服务的健康状态
  ......
 
  3.1 添加依赖
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>2.4.3</version>
  </dependency>
 
 
  3.2 基础配置
  management:
    server:
      port: 9090 # 一般启动独立端口(默认和应用端口一致),启用后源端口不可查
    endpoints:
      web:
        base-path: /actuator # 默认前缀路径,可修改
        exposure:
          include: health,info,metrics # 向外暴露的端点,可用通配符('*',需要单引号)
          exclude: env,heapdump # 排除暴露的端点
 
 
  3.3 查看可消费的端点
  可先用 HTTP 访问 localhost:9090/actuator 来获取 HATEOAS(可简单理解为暴露的端点文档),它是所有可暴露端点的地图,可通过属性对应的地址来获取的指标,内容如下:
 
  {
    "_links": {
      "self": {
        "href": "http://localhost:9090/actuator",
        "templated": false
      },
      "health-path": {
        "href": "http://localhost:9090/actuator/health/{*path}",
        "templated": true
      },
      "health": {
        "href": "http://localhost:9090/actuator/health",
        "templated": false
      },
      "info": {
        "href": "http://localhost:9090/actuator/info",
        "templated": false
      }
    }
  }
 
 
  3.4 获取应用的基本信息
  消费对应的指标,就在地址后面加上名字即可。应用的基本信息是需要自己配置的,没有默认值,所以首次访问 可访问 localhost:9090/actuator/info 是一个空 json。
 
  可配置 info 开头的属性,比如联系方式,应用的作用等等,其配置和消费结果如下:
 
  info:
    contact:
      email: support@howl.com
      phone: 123456789
    description: actuator test application
    
    
  # 消费结果
  {
    "contact": {
      "email": "support@howl.com",
      "phone": 123456789
    },
    "description": "actuator test application"
  }
 
 
  3.5 健康指标
  首先尝试访问 localhost:9090/actuator/health,就可以获取指标内容了
 
  {
    "status": "UP"
  }
  这里显示的是一个或多个健康指示器的聚合状态,即当前应用和与之交互的外部系统(数据库,消息队列,Eureka等等)的健康状态的聚合状态。我们可以添加如下配置来获取健康指示器的内聚状态
 
  management:
    endpoint:
      health:
        show-details: always
        
        
  # 消费结果
  {
    "status": "UP",
    "components": {
      "diskSpace": {
        "status": "UP",
        "details": {
          "total": 493516484608,
          "free": 436332154880,
          "threshold": 10485760,
          "exists": true
        }
      },
      "ping": {
        "status": "UP"
      }
    }
  }
  SpringBoot 的自动配置功能可以确保只有与应用交互的组件才会显示到 health 里面
 
 
 
  3.6 指标端点 metrics
  可访问如下地址来获取 Actuator 提供的开箱即用的指标分类,包括了内存、处理器、垃圾收集、HTTP请求等指标
 
  http://localhost:9090/actuator/metrics
 
 
  # 消费结果
  {
    "names": [
      "http.server.requests",
      "jvm.buffer.count",
      "jvm.buffer.memory.used",
      "jvm.buffer.total.capacity",
      "jvm.classes.loaded",
      "jvm.classes.unloaded",
      "jvm.gc.live.data.size",
      "jvm.gc.max.data.size",
      "jvm.gc.memory.allocated",
      "jvm.gc.memory.promoted",
      "jvm.gc.pause",
      "jvm.memory.committed",
      "jvm.memory.max",
      "jvm.memory.used",
      "jvm.threads.daemon",
      "jvm.threads.live",
      "jvm.threads.peak",
      "jvm.threads.states",
      "logback.events",
      "process.cpu.usage",
      "process.start.time",
      "process.uptime",
      "system.cpu.count",
      "system.cpu.usage",
      "tomcat.sessions.active.current",
      "tomcat.sessions.active.max",
      "tomcat.sessions.alive.max",
      "tomcat.sessions.created",
      "tomcat.sessions.expired",
      "tomcat.sessions.rejected"
    ]
  }
 
 
 
  # 可用标准地址 + 指标端点名字 来消费某个指标端点
  http://localhost:9090/actuator/metrics/http.server.requests
 
  
 
  4. 实例
  统计 /user/login 接口被调用的次数,meterRegistry 注册中心在自动配置中加入容器可直接使用

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读