序言

咱们通过一个系列文章跟大家具体展现一个 go-zero 微服务示例,整个系列分十篇文章,目录构造如下:

  1. 环境搭建
  2. 服务拆分
  3. 用户服务
  4. 产品服务
  5. 订单服务
  6. 领取服务
  7. RPC 服务 Auth 验证
  8. 服务监控(本文)
  9. 链路追踪
  10. 分布式事务

冀望通过本系列带你在本机利用 Docker 环境利用 go-zero 疾速开发一个商城零碎,让你疾速上手微服务。

残缺示例代码:https://github.com/nivin-studio/go-zero-mall

首先,咱们来看一下整体的服务拆分图:

8.1 Prometheus 介绍

Prometheus 是一款基于时序数据库的开源监控告警零碎,基本原理是通过 HTTP 协定周期性抓取被监控服务的状态,任意服务只有提供对应的 HTTP 接口就能够接入监控。不须要任何 SDK 或者其余的集成过程,输入被监控服务信息的 HTTP 接口被叫做 exporter 。目前互联网公司罕用的服务大部分都有 exporter 能够间接应用,比方 VarnishHaproxyNginxMySQLLinux 零碎信息(包含磁盘、内存、CPU、网络等等)。Promethus 有以下特点:

  • 反对多维数据模型(由度量名和键值对组成的工夫序列数据)
  • 反对 PromQL 查询语言,能够实现非常复杂的查问和剖析,对图表展现和告警十分有意义
  • 不依赖分布式存储,单点服务器也能够应用
  • 反对 HTTP 协定被动拉取形式采集工夫序列数据
  • 反对 PushGateway 推送工夫序列数据
  • 反对服务发现和动态配置两种形式获取监控指标
  • 反对接入 Grafana

8.2 go-zero 应用 Prometheus 监控服务

go-zero 框架中集成了基于 Prometheus 的服务指标监控,go-zero 目前在 http 的中间件和 rpc 的拦截器中增加了对申请指标的监控。

次要从 申请耗时申请谬误 两个维度,申请耗时采纳了 Histogram 指标类型定义了多个 Buckets 不便进行分位统计,申请谬误采纳了 Counter 类型,并在 http metric 中增加了 path 标签,rpc metric 中增加了 method 标签以便进行细分监控。

接下来咱们别离为后面几章实现的服务增加 Prometheus 监控,首先咱们先回顾下 第二章 服务拆分,为了模仿服务的分布式部署,咱们是在一个容器里启动了所有的服务,并为其调配了不同的端口号。上面咱们再为这些服务调配一个 Prometheus 采集指标数据的端口号。

服务api 服务端口号rpc 服务端口号api 指标采集端口号rpc 指标采集端口号
user8000900090809090
product8001900190819091
order8002900290829092
pay8003900390839093

8.2.1 增加 user api 服务 Prometheus 配置

$ vim mall/service/user/api/etc/user.yaml
Name: UserHost: 0.0.0.0Port: 8000...Prometheus:  Host: 0.0.0.0  Port: 9080  Path: /metrics

8.2.2 增加 user rpc 服务 Prometheus 配置

$ vim mall/service/user/rpc/etc/user.yaml
Name: user.rpcListenOn: 0.0.0.0:9000...Prometheus:  Host: 0.0.0.0  Port: 9090  Path: /metrics

8.2.3 增加 product api 服务 Prometheus 配置

$ vim mall/service/product/api/etc/product.yaml
Name: ProductHost: 0.0.0.0Port: 8001...Prometheus:  Host: 0.0.0.0  Port: 9081  Path: /metrics

8.2.4 增加 product rpc 服务 Prometheus 配置

$ vim mall/service/product/rpc/etc/product.yaml
Name: product.rpcListenOn: 0.0.0.0:9001...Prometheus:  Host: 0.0.0.0  Port: 9091  Path: /metrics

8.2.5 增加 order api 服务 Prometheus 配置

$ vim mall/service/order/api/etc/order.yaml
Name: OrderHost: 0.0.0.0Port: 8002...Prometheus:  Host: 0.0.0.0  Port: 9082  Path: /metrics

8.2.6 增加 order rpc 服务 Prometheus 配置

$ vim mall/service/order/rpc/etc/order.yaml
Name: order.rpcListenOn: 0.0.0.0:9002...Prometheus:  Host: 0.0.0.0  Port: 9092  Path: /metrics

8.2.7 增加 pay api 服务 Prometheus 配置

$ vim mall/service/pay/api/etc/pay.yaml
Name: PayHost: 0.0.0.0Port: 8003...Prometheus:  Host: 0.0.0.0  Port: 9083  Path: /metrics

8.2.8 增加 pay rpc 服务 Prometheus 配置

$ vim mall/service/pay/rpc/etc/pay.yaml
Name: pay.rpcListenOn: 0.0.0.0:9003...Prometheus:  Host: 0.0.0.0  Port: 9093  Path: /metrics
提醒:配置批改后,须要重启服务才会失效。

8.2.9 批改 Prometheus 配置

第一章 环境搭建 中咱们集成了 Prometheus 服务,在prometheus 目录下有个 prometheus.yml 的配置文件,咱们当初须要批改这个配置文件。

# my global configglobal:  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.   evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.       # scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting:  alertmanagers:    - static_configs:        - targets:          # - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.  rule_files:  # - "first_rules.yml"  # - "second_rules.yml"# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs:  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.  - job_name: "prometheus"    # metrics_path defaults to '/metrics'    # scheme defaults to 'http'.    static_configs:      - targets: ["localhost:9090"]  # 咱们本人的商城我的项目配置  - job_name: 'mall'    static_configs:      # 指标的采集地址      - targets: ['golang:9080']        labels:          # 自定义标签          app: 'user-api'          env: 'test'      - targets: ['golang:9090']        labels:          app: 'user-rpc'          env: 'test'      - targets: ['golang:9081']        labels:          app: 'product-api'          env: 'test'      - targets: ['golang:9091']        labels:          app: 'product-rpc'          env: 'test'      - targets: ['golang:9082']        labels:          app: 'order-api'          env: 'test'      - targets: ['golang:9092']        labels:          app: 'order-rpc'          env: 'test'      - targets: ['golang:9083']        labels:          app: 'pay-api'          env: 'test'      - targets: ['golang:9093']        labels:          app: 'pay-rpc'          env: 'test'
提醒:配置文件批改好后,须要重启 Prometheus 服务容器能力失效。

8.2.10 拜访 Prometheus 可视化界面

  • 第一章 环境搭建 中咱们集成了 Prometheus 服务,并为其端口号9090 做了宿主机端口 3000 的映射关系,所以在浏览器中输出 http://127.0.0.1:3000/ 拜访 Prometheus 界面。

  • 抉择 Status -> Targets 菜单,即可看到咱们配置的采集指标的状态和自定义的标签。

  • 咱们屡次拜访 api 服务的接口后,抉择 Graph 菜单,在查问输入框中输出 {path="api接口地址"} 或者 {method="rpc接口办法"} 指令,即可查看监控指标。

8.3 应用 Grafana 可视化 Prometheus 指标数据

8.3.1 增加 Prometheus 数据源

  • 第一章 环境搭建 中咱们集成了 Grafana 服务,并为其端口号3000 做了宿主机端口 4000 的映射关系,所以在浏览器中输出 http://127.0.0.1:4000/ 拜访 Grafana 界面。点击左侧边栏 Configuration -> Data Source -> Add data source 进行数据源增加。

  • 而后抉择 Prometheus 数据源

  • 填写 HTTP 配置中 URL 地址(我这里的 IP地址Prometheus 所在容器的 IP地址),而后点击 Save & test 按,上方会提醒 Data source is working,阐明咱们数据源增加胜利且失常工作。

8.3.2 增加 Variables 用于服务筛选

  • 点击左侧边栏 Dashboard 抉择右上角 Dashboard settings 按钮,在 Settings 页面抉择 Variables -> Add variable 增加变量,不便针对不同的标签进行过滤筛选。

  • 别离增加 api_app API服务名称,rpc_app RPC服务名称变量,用于不同服务的筛选。变量数据源抉择 Prometheus 数据源,应用正则表达式提取出对应的 app 标签。

8.3.3 增加 api 接口 qps 仪表盘

  • 回到 Dashboard 页面抉择右上角 Add panel 按钮,而后再抉择 Add an empty panel 增加一个空的面板。

  • 面板编辑页,批改面板题目为 API接口QPS,在 Metrics 中输出 sum(rate(http_server_requests_duration_ms_count{app="$api_app"}[5m])) by (path)path 维度统计 api 接口的 qps

8.3.4 增加 rpc 接口 qps 仪表盘

  • 再新建一个面板,批改面板题目为 RPC接口QPS,在 Metrics 中输出 sum(rate(rpc_server_requests_duration_ms_count{app="$rpc_app"}[5m])) by (method)method 维度统计 rpc 接口的 qps

8.3.5 增加 api 接口状态码仪表盘

  • 再新建一个面板,批改面板题目为 API接口状态码,在 Metrics 中输出 sum(rate(http_server_requests_code_total{app="$api_app"}[5m])) by (code)code 维度统计 api 接口的状态码

8.3.6 增加 rpc 接口状态码仪表盘

  • 再新建一个面板,批改面板题目为 RPC接口状态码,在 Metrics 中输出 sum(rate(rpc_server_requests_code_total{app="$rpc_app"}[5m])) by (code)code 维度统计 rpc 接口的状态码

8.3.7 保留仪表盘

  • 调整下面板地位,抉择右上角 Save dashboard 按钮保留仪表盘。

我的项目地址

https://github.com/zeromicro/go-zero

欢送应用 go-zerostar 反对咱们!

微信交换群

关注『微服务实际』公众号并点击 交换群 获取社区群二维码。