当前位置: 首页 > wzjs >正文

宁波搭建网站公司盘州电子商务网站建设

宁波搭建网站公司,盘州电子商务网站建设,网站被黑,助企建站使用AOP Prometheus node-exporter grafana 实现Java系统的接口监控 本笔记适用于想要搭建业务系统监控能力的工程狮。此笔记适用的架构为单机部署方式,更倾向于一个小demo,本笔记需要用到cocker。请提前准备!! 1.监控的目的以…

使用AOP + Prometheus + node-exporter + grafana 实现Java系统的接口监控

本笔记适用于想要搭建业务系统监控能力的工程狮。此笔记适用的架构为单机部署方式,更倾向于一个小demo,本笔记需要用到cocker。请提前准备!!

1.监控的目的以及监控哪些内容?

在平常的业务需求中,我们通常需要一下两个方面的监控来全方位了解我们的系统:

1.1 机器健康度监控

  • CPU使用率内存使用率磁盘IO网络流量
  • 系统负载(Load Average)
  • 磁盘使用情况
  • 进程存活状态
  • Docker容器状态(如果使用容器化)
  • 系统日志异常

1.2 业务监控

  • 请求QPS、TPS(吞吐量)
  • 请求响应时间(P99, P95, P50)
  • HTTP状态码统计
  • 数据库查询耗时
  • 缓存命中率
  • 异常日志(如超时、错误)

2. 我们要用到哪些软件来实现呢?

2.1 Prometheus 主要用来存储和查询监控数据。
2.2 node Exporter 这个主要是用来采集服务器的CPU、内存、磁盘、网络指标等。(值得注意的是他基本不占什么内存)
2.3 grafana 通过可视化的图标来展示服务的运行情况
2.4 Java spring boot + Micrometer 实现业务监控

3. 开始搭建Prometheus + Grafana

3.1 创建docker-compose.yml

在本地创建一个文件夹

mkdir monitoring-demo && cd monitoring-demo

3.2 创建docker-compose.yml

version: '3'
services:prometheus:image: prom/prometheuscontainer_name: prometheusvolumes:- ./prometheus.yml:/etc/prometheus/prometheus.ymlports:- "9090:9090"grafana:image: grafana/grafanacontainer_name: grafanaports:- "3000:3000"node-exporter:image: prom/node-exportercontainer_name: node-exporterports:- "9100:9100"

在上面的文件中共创建了三个容器,并为他们开放了不同的端口分别是 9090、3000以及9100 其中node-exporter用来采集docker容器的资源。

volumes:- ./prometheus.yml:/etc/prometheus/prometheus.yml

这个主要是用来配置普罗米修斯的采集规则,在docker-compose.yml的平级目录中添加一个新的yml用来进行数据采集。

global:scrape_interval: 15s  # 全局抓取间隔scrape_configs:- job_name: 'prometheus' # 监控 Prometheus 自己static_configs:- targets: ['localhost:9090']- job_name: 'node-exporter' # 监控 Node Exporterstatic_configs:- targets: ['node-exporter:9100']- job_name: 'java-demo-app' # Java 业务系统metrics_path: '/actuator/prometheus'static_configs:- targets: ['host.docker.internal:8080']

3.3 启动docker
完成上面的操作后,就可以通过下面的命令来启动容器了

docker-compose up -d # -d 不展示启动log,在后台启动

启动后:

Prometheus 访问地址:http://localhost:9090

Grafana 访问地址:http://localhost:3000(默认用户 admin/admin) Ps🗡 第一次访问会让你改默认密码

一些知识点🛩 :

  1. 在普罗米修斯中可以通过简单的sql来查询已经上报的数据。可以通过 http://localhost:9090/query 这个路径来查询。如果你想要看到一些数据可以尝试查一下 node_cpu_seconds_total 这个信息,你大概可以看到一堆数据
  2. 如果你没看到任何信息或者你想要看普罗米修斯正在从收集哪些应用的信息,可以通过 http://localhost:9090/targets 来进行查看,这里可以看到哪些应用在线或者离线

4. 在Grafana中配置Prometheus数据源

访问Grafana http://localshot:3000

Grafana 左侧菜单 → 齿轮图标(Configuration) → Data Sources

点击 Add data source

选择 Prometheus

填写 URL:http://prometheus:9090 这里为什么写prometheus:9090呢,是以为Grafana 和 Prometheus 在同一个docker网络下,所以可以直呼其名

点击 Save & Test → 显示 Data source is working,表示连接成功!

5. 导入监控面板(Dashboard)

导入现成的服务器监控 Dashboard(Node Exporter)
  1. 左侧 → Dashboard(四个方块图标) → Import
  2. 输入 Dashboard ID1860
  3. 点击 Load
  4. 选择 Prometheus 数据源
  5. 点击 Import

效果:马上看到服务器 CPU、内存、磁盘、网络图表!

✅ 到目前为止你所完成事情 每个组件的职责和意义

组件作用你做了什么
Docker快速构建环境装了 Docker,部署了容器
Prometheus抓取指标,保存数据配了 prometheus.yml,启动服务
Node Exporter提供主机指标启动了 node-exporter,暴露 9100
Grafana数据展示和分析配了数据源、导入 Dashboard

6. 启动Java DEMO

  1. 创建一个maven项目,pom文件如下
<dependencies><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.pivotal.spring.cloud</groupId><artifactId>spring-cloud-services-starter-service-registry</artifactId></dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
  1. 配置application.yml
  spring:application:name: demoserver:port: 8080# 以下内容主要是上报jvm的健康度management:endpoints:web:exposure:include: health,info,prometheusmetrics:tags:application: demo-app
  1. 启动Javademo项目,这个Java项目的jvm信息将会被上报给普罗米修斯。

  2. 想要看看 jvm GC的情况?可以在仪表盘中导入这个json模板

     {"annotations": {"list": []},"panels": [{"type": "stat","title": "GC 次数 (1m)","datasource": "prometheus-1","targets": [{"expr": "increase(jvm_gc_pause_seconds_count{application=\"demo-app\"}[1m])","interval": "","legendFormat": "GC 次数","refId": "A"}],"fieldConfig": {"defaults": {"unit": "none","thresholds": {"mode": "absolute","steps": [{"color": "green","value": null},{"color": "yellow","value": 5},{"color": "red","value": 10}]}},"overrides": []},"options": {"reduceOptions": {"calcs": ["lastNotNull"],"fields": "","values": false},"orientation": "horizontal","colorMode": "value","graphMode": "none","justifyMode": "auto"},"gridPos": {"h": 6,"w": 6,"x": 0,"y": 0}},{"type": "stat","title": "GC 耗时 (1m)","datasource": "prometheus-1","targets": [{"expr": "increase(jvm_gc_pause_seconds_sum{application=\"demo-app\"}[1m])","interval": "","legendFormat": "GC 耗时 (秒)","refId": "B"}],"fieldConfig": {"defaults": {"unit": "s","thresholds": {"mode": "absolute","steps": [{"color": "green","value": null},{"color": "yellow","value": 0.5},{"color": "red","value": 1}]}},"overrides": []},"options": {"reduceOptions": {"calcs": ["lastNotNull"],"fields": "","values": false},"orientation": "horizontal","colorMode": "value","graphMode": "none","justifyMode": "auto"},"gridPos": {"h": 6,"w": 6,"x": 6,"y": 0}},{"type": "timeseries","title": "GC 次数趋势","datasource": "prometheus-1","targets": [{"expr": "rate(jvm_gc_pause_seconds_count{application=\"demo-app\"}[1m])","interval": "","legendFormat": "{{gc}}","refId": "C"}],"fieldConfig": {"defaults": {"unit": "none"},"overrides": []},"options": {"legend": {"displayMode": "table","placement": "bottom"},"tooltip": {"mode": "single"}},"gridPos": {"h": 8,"w": 12,"x": 0,"y": 6}},{"type": "timeseries","title": "GC 耗时趋势","datasource": "prometheus-1","targets": [{"expr": "rate(jvm_gc_pause_seconds_sum{application=\"demo-app\"}[1m])","interval": "","legendFormat": "{{gc}}","refId": "D"}],"fieldConfig": {"defaults": {"unit": "s"},"overrides": []},"options": {"legend": {"displayMode": "table","placement": "bottom"},"tooltip": {"mode": "single"}},"gridPos": {"h": 8,"w": 12,"x": 12,"y": 6}}],"schemaVersion": 34,"title": "JVM GC 仪表盘","timezone": "browser","version": 1}
  1. 想要实现controller和service接口的执行情况? 通过aop来实现 这个aop可以上报所有接口tp 999 tp 99 tp 50 的情况
     import io.micrometer.core.instrument.MeterRegistry;import io.micrometer.core.instrument.Timer;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;@Aspect@Componentpublic class MethodTimerAspect {private final MeterRegistry meterRegistry;public MethodTimerAspect(MeterRegistry meterRegistry) {this.meterRegistry = meterRegistry;}@Around("execution(* com.example..controller..*(..)) || execution(* com.example..service..*(..))")public Object timeMethods(ProceedingJoinPoint pjp) throws Throwable {String className = pjp.getTarget().getClass().getSimpleName();String methodName = pjp.getSignature().getName();String timerName = "method_execution_timer";Timer.Sample sample = Timer.start(meterRegistry);try {return pjp.proceed();} finally {sample.stop(Timer.builder(timerName).description("方法执行耗时统计").tags("class", className, "method", methodName).publishPercentiles(0.5, 0.9, 0.99, 0.999).publishPercentileHistogram().register(meterRegistry));}}}```6. 如果你想要看看各个接口的执行情况,导入下面这个json模板可以看到```json{"annotations": {"list": []},"templating": {"list": [{"name": "datasource","type": "datasource","query": "prometheus","label": "Prometheus 数据源","hide": 0}]},"panels": [{"type": "stat","title": "接口 TP999 延迟 (ms)","datasource": "prometheus-1","targets": [{"expr": "histogram_quantile(0.999, sum(rate(method_execution_time_bucket[1m])) by (le, method))","legendFormat": "{{method}}","refId": "A"}],"fieldConfig": {"defaults": {"unit": "ms","thresholds": {"mode": "absolute","steps": [{ "color": "green", "value": null },{ "color": "yellow", "value": 1000 },{ "color": "red", "value": 2000 }]}},"overrides": []},"options": {"reduceOptions": {"calcs": ["max"],"fields": "","values": false},"orientation": "horizontal","colorMode": "value","graphMode": "none","justifyMode": "auto"},"gridPos": { "h": 6, "w": 12, "x": 0, "y": 0 }},{"type": "timeseries","title": "接口 TP999 延迟趋势","datasource": "prometheus-1","targets": [{"expr": "histogram_quantile(0.999, sum(rate(method_execution_time_bucket[1m])) by (le, method))","legendFormat": "{{method}}","refId": "B"}],"fieldConfig": {"defaults": {"unit": "ms"},"overrides": []},"options": {"legend": {"displayMode": "table","placement": "bottom"},"tooltip": {"mode": "single"}},"gridPos": { "h": 8, "w": 24, "x": 0, "y": 6 }},{"type": "stat","title": "接口 QPS","datasource": "${datasource}","targets": [{"expr": "sum(rate(http_server_requests_seconds_count[1m])) by (uri, method)","legendFormat": "{{uri}} {{method}}","refId": "C"}],"fieldConfig": {"defaults": {"unit": "ops","thresholds": {"mode": "absolute","steps": [{ "color": "red", "value": null },{ "color": "yellow", "value": 5 },{ "color": "green", "value": 20 }]}},"overrides": []},"options": {"reduceOptions": {"calcs": ["max"],"fields": "","values": false},"orientation": "horizontal","colorMode": "value","graphMode": "none","justifyMode": "auto"},"gridPos": { "h": 6, "w": 12, "x": 0, "y": 14 }},{"type": "timeseries","title": "接口 QPS 趋势","datasource": "prometheus-1","targets": [{"expr": "sum(rate(http_server_requests_seconds_count[1m])) by (uri, method)","legendFormat": "{{uri}} {{method}}","refId": "D"}],"fieldConfig": {"defaults": {"unit": "ops"},"overrides": []},"options": {"legend": {"displayMode": "table","placement": "bottom"},"tooltip": {"mode": "single"}},"gridPos": { "h": 8, "w": 24, "x": 0, "y": 20 }},{"type": "stat","title": "接口 5xx 错误数","datasource": "prometheus-1","targets": [{"expr": "sum(rate(http_server_requests_seconds_count{status=~\"5..\"}[1m])) by (uri, method)","legendFormat": "{{uri}} {{method}}","refId": "E"}],"fieldConfig": {"defaults": {"unit": "short","thresholds": {"mode": "absolute","steps": [{ "color": "green", "value": null },{ "color": "yellow", "value": 1 },{ "color": "red", "value": 5 }]}},"overrides": []},"options": {"reduceOptions": {"calcs": ["max"],"fields": "","values": false},"orientation": "horizontal","colorMode": "value","graphMode": "none","justifyMode": "auto"},"gridPos": { "h": 6, "w": 12, "x": 0, "y": 28 }},{"type": "timeseries","title": "接口 5xx 错误数趋势","datasource": "prometheus-1","targets": [{"expr": "sum(rate(http_server_requests_seconds_count{status=~\"5..\"}[1m])) by (uri, method)","legendFormat": "{{uri}} {{method}}","refId": "F"}],"fieldConfig": {"defaults": {"unit": "short"},"overrides": []},"options": {"legend": {"displayMode": "table","placement": "bottom"},"tooltip": {"mode": "single"}},"gridPos": { "h": 8, "w": 24, "x": 0, "y": 34 }}],"title": "接口 TP999 + QPS + 错误监控","timezone": "browser","schemaVersion": 34,"version": 1}

✅通过上面的步骤你应该就能实现业务接口的监控,后续微服务式监控可能还会更新 敬请期待…


文章转载自:

http://4FLoYem2.cyjjp.cn
http://FfcXDGt7.cyjjp.cn
http://JwjYvL6O.cyjjp.cn
http://smBGwJ1G.cyjjp.cn
http://B09E82S1.cyjjp.cn
http://2fo2ZxGA.cyjjp.cn
http://iRjzQlM0.cyjjp.cn
http://LziwRUoN.cyjjp.cn
http://HEgjKMdQ.cyjjp.cn
http://MEXx7JKO.cyjjp.cn
http://6zlhPrG8.cyjjp.cn
http://lCSbXb47.cyjjp.cn
http://f9SQwz2f.cyjjp.cn
http://PtqZX1Me.cyjjp.cn
http://cz7vG1jv.cyjjp.cn
http://yrjYfaPn.cyjjp.cn
http://hPO0trHX.cyjjp.cn
http://VbIDtdqB.cyjjp.cn
http://Dzbv9bcH.cyjjp.cn
http://Vowuwq5E.cyjjp.cn
http://UAhivrr4.cyjjp.cn
http://EY71ozcW.cyjjp.cn
http://TgQKIryn.cyjjp.cn
http://VUVU2ADe.cyjjp.cn
http://nsOrzqwW.cyjjp.cn
http://htb1sD2S.cyjjp.cn
http://KzHfJIoo.cyjjp.cn
http://CJ0vc1Nk.cyjjp.cn
http://K0lPjnWZ.cyjjp.cn
http://Ww52jXlT.cyjjp.cn
http://www.dtcms.com/wzjs/624378.html

相关文章:

  • 欧美网站建设风格特点上海市五金外贸公司
  • 江苏 江苏省住房和城乡建设厅网站免费开源电商系统
  • 企业备案网站名称怎么填网站免费认证联盟
  • 网站建设和淘宝店装修是不是一样wordpress无法显示登陆
  • 用什么开发和建设网站最好下载公众号
  • 国外翻墙设计网站高端品牌网页设计
  • 施工方案下载免费网站成都互联网网站建设
  • 青岛 网站备案设计师网单怎么做
  • 网店推广网站网站的排版设计
  • 网站托管套餐做直播网站需要手续
  • 足球网站怎么做的布吉网站建设方案
  • 台州网站建设方案咨询亚瑟中文 在线
  • 建一个网站需要多少钱天津的网站建设公司哪家好
  • 常见的三种网站类型外贸公司网站源码
  • 网站建立项目步骤百度小说风云榜排名完结
  • 手机网站制作套餐中国网站建设公司有哪些内容
  • 美食网站开发与设计任务书鸿鑫建设集团有限公司网站
  • 江苏科技大学新校区建设网站有哪些软件可以查企业信息
  • 建设网站的意义知乎关闭wordpress评论
  • 最全的网页模板网站crm管理系统登录
  • 网站月流量是什么意思北京装饰公司招聘信息
  • 有啦域名网站怎么做wordpress图片快速主题
  • 苏州做视频网站广告公司优秀的外贸网站案例
  • 信邦建设工程有限公司网站网站开发 flex
  • 外贸网站定做花店网页设计素材
  • vue配合什么做网站比较好沃尔玛网上商城和超市价格一样吗
  • 怎么仿网站链接网站源码路径
  • 网站备案的幕布尺寸南宁做网站公司
  • 产品网站别人是如何做优化的关键词排名点击器
  • 网站策划书的内涵网络营销的概念及特点