帶你十天輕鬆搞定 Go 微服務系列(八、服務監控)

序言

我們通過一個系列文章跟大家詳細展示一個 go-zero 微服務示例,整個系列分十篇文章,目錄結構如下:

  1. 環境搭建:帶你十天輕鬆搞定 Go 微服務系列(一)

  2. 服務拆分:帶你十天輕鬆搞定 Go 微服務系列(二)

  3. 用戶服務:帶你十天輕鬆搞定 Go 微服務系列(三)

  4. 產品服務:帶你十天輕鬆搞定 Go 微服務系列(四)

  5. 訂單服務:帶你十天輕鬆搞定 Go 微服務系列(五)

  6. 支付服務:帶你十天輕鬆搞定 Go 微服務系列(六)

  7. RPC 服務 Auth 驗證:帶你十天輕鬆搞定 Go 微服務系列(七)

  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 有以下特點:

8.2 go-zero 使用 Prometheus 監控服務

go-zero 框架中集成了基於 Prometheus 的服務指標監控,go-zero 目前在 http 的中間件和 rpc 的攔截器中添加了對請求指標的監控。

主要從 請求耗時請求錯誤 兩個維度,請求耗時採用了 Histogram 指標類型定義了多個 Buckets 方便進行分位統計,請求錯誤採用了 Counter 類型,並在 http metric 中添加了 path 標籤,rpc metric 中添加了 method 標籤以便進行細分監控。

接下來我們分別爲前面幾章實現的服務添加 Prometheus 監控,首先我們先回顧下 第二章 服務拆分,爲了模擬服務的分佈式部署,我們是在一個容器裏啓動了所有的服務,併爲其分配了不同的端口號。下面我們再爲這些服務分配一個 Prometheus 採集指標數據的端口號。

YmCIuw

8.2.1 添加 user api 服務 Prometheus 配置

$ vim mall/service/user/api/etc/user.yaml
Name: User
Host: 0.0.0.0
Port: 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.rpc
ListenOn: 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: Product
Host: 0.0.0.0
Port: 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.rpc
ListenOn: 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: Order
Host: 0.0.0.0
Port: 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.rpc
ListenOn: 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: Pay
Host: 0.0.0.0
Port: 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.rpc
ListenOn: 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 config
global:
  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 configuration
alerting:
  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 可視化界面

8.3 使用 Grafana 可視化 Prometheus 指標數據

8.3.1 添加 Prometheus 數據源

8.3.2 添加 Variables 用於服務篩選

8.3.3 添加 api 接口 qps 儀表盤

8.3.4 添加 rpc 接口 qps 儀表盤

8.3.5 添加 api 接口狀態碼儀表盤

8.3.6 添加 rpc 接口狀態碼儀表盤

8.3.7 保存儀表盤

項目地址

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

歡迎使用 go-zerostar 支持我們!

微信交流羣

關注『微服務實踐』公衆號並點擊 交流羣 獲取社區羣二維碼。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/cp--0RS3NybMufBUsht8bw