有域名了如何自己做网站,代理公司注册公司坑人,施工企业会计论文,wordpress全屏幻灯项目中看到了有埋点监控、报表、日志分析#xff0c;有点兴趣想慢慢捣鼓一下1. 数据埋点监控机器环境的性能和业务流程或逻辑等各项数据#xff0c;并根据这些数据生成对应的指标#xff0c;那么我们就称为数据埋点。比如我们想知道某个接口调用的 TPS、机器 CPU 的使用率有点兴趣想慢慢捣鼓一下1. 数据埋点监控机器环境的性能和业务流程或逻辑等各项数据并根据这些数据生成对应的指标那么我们就称为数据埋点。比如我们想知道某个接口调用的 TPS、机器 CPU 的使用率这些都可以用到数据埋点2. MicrometerMicrometer 为流行的各种监控系统提供了一个简单的门面类似于日志门面 —— 提供了与供应商无关的接口counterstimersgauges等这些接口称为 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 ActuatorSpringBoot Actuator 其底层使用了 Mircometer 可度量 SpringBoot 应用和获取它的各项指标可通过 HTTP 或 JMX 来调用 Actuator 暴露的各种端点然后就可以获取一个正在运行中的应用的内部状态当然内部指标并不是所有都可以向外暴露的所以我们得有选择的开放或者加入权限校验之后才能获取如下内容有那些可配置的属性各依赖包的日志级别占用了多少内存HTTP 埋点被请求了多少次应用本身以及协作的外部服务的健康状态......3.1 添加依赖dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-actuator/artifactId version2.4.3/version /dependency3.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: supporthowl.com phone: 123456789 description: actuator test application # 消费结果 { contact: { email: supporthowl.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.requests4. 实例统计 /user/login 接口被调用的次数meterRegistry 注册中心在自动配置中加入容器可直接使用4.1 测试接口启动应用然后多次访问这个接口RestController RequestMapping(/user) public class UserController { Autowired SimpleMeterRegistry meterRegistry; GetMapping(/login) public String userLogin() { Counter counter meterRegistry.counter(http.server.requests, uri, /user/login); counter.increment(); return 登录成功; } }4.2 消费指标端点访问如下地址即可消费端点http://localhost:9090/actuator/metrics/http.server.requests?taguri:/user/login # 消费结果 # 可以看到这个接口度量COUNT了 18次 访问数据做了部分清除 { name: http.server.requests, baseUnit: seconds, measurements: [ { statistic: COUNT, value: 18.0 } ], availableTags: [ { tag: method, values: [ GET ] } ] }5. SpringBoot Admin使用上面的地址访问指标很不友好不可能看一堆这样的数据得使用一些美化的 UI 界面SpringBoot Admin 就提供了这样的框架SpringBoot Admin 作为简单的监控分为 服务器端 和 客户端5.1 Admin 服务器端作为监控的服务端一般是在另外一台服务器上部署的然后这台服务器会定时去配置好的地址里面拉取监控的指标数据5.1.1 启用功能、添加依赖在初始化应用的时候在 Spring Initializr 也可以选择EnableAdminServer SpringBootApplication public class AdminApplication { public static void main(String[] args) { SpringApplication.run(AdminApplication.class, args); } }dependency groupIdde.codecentric/groupId artifactIdspring-boot-admin-starter-server/artifactId version2.4.3/version /dependency5.1.2 选择一个端口server: port: 100005.1.3 访问直接访问 10000 端口即可当然现在没有东西可以监控主页面是空白没应用的5.2 Client 客户端5.2.1 添加依赖!-- actuator -- dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-actuator/artifactId version2.4.3/version /dependency !-- admin-client -- dependency groupIdde.codecentric/groupId artifactIdspring-boot-admin-starter-client/artifactId version2.4.3/version /dependency5.2.2添加配置# 服务端用客户端的主机名来通信但在虚拟机上客户端主机名会解析不正确导致实例一直下线状态 # 此时需要如下配置 spring: application: name: actuatorApplicaton boot: admin: client: url: http://admin-serve-ip:10000 # 向服务端定时发送请求 instance: service-url: http://admin-client-ip:8080 # 主页 management-base-url: http://admin-client-ip:9090 # 各类指标的基础地址5.2.3 访问 admin-serve访问即可发现有个应用的指标被获取了然后里面可以看各种已经暴露的端点指标5.3 Eureka 服务器发现上面每启动一个客户端都要手动进行配置监控的 IP 地址很是麻烦既然微服务架构有服务发现机制的那么我们可以在监控的服务端上配置 Eureka 的地址那么 Admin-Server 就会去注册中心获取地址再去拉取指标5.3.1 加依赖dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-eureka-client/artifactId version2.4.3/version /dependency5.3.2 添加配置eureka: client: service-url: defaultZone: http://xxx.xxx.xxx.xxx:xxx/eureka/6 缺点笔者个人觉得个人小型项目用这个组合来监控埋点已经足够了加上警告处理都是很不错的选择但是 SpringBoot Admin 只能监控短时间内的应用信息如果需要各时间段的监控那么就需要有时序数据库的支持比如查看这个月内的统计信息这些就显得无能为力了。当然还是有代替方案的Actuator埋点操作Promethus定期去 actuator 拉取数据并以时序的形式存储内部有时序数据库Granfan用户友好的 UI 数据展示展示 Promethus 的数据后面笔者还会写一篇 Promethus 监控的笔记