序言
咱们通过一个系列文章跟大家具体展现一个 go-zero 微服务示例,整个系列分十篇文章,目录构造如下:
- 环境搭建
- 服务拆分
- 用户服务
- 产品服务
- 订单服务
- 领取服务
- RPC 服务 Auth 验证
- 服务监控(本文)
- 链路追踪
- 分布式事务
冀望通过本系列带你在本机利用 Docker 环境利用 go-zero 疾速开发一个商城零碎,让你疾速上手微服务。
残缺示例代码:https://github.com/nivin-studio/go-zero-mall
首先,咱们来看一下整体的服务拆分图:
8.1 Prometheus
介绍
Prometheus
是一款基于时序数据库的开源监控告警零碎,基本原理是通过 HTTP
协定周期性抓取被监控服务的状态,任意服务只有提供对应的 HTTP
接口就能够接入监控。不须要任何 SDK
或者其余的集成过程,输入被监控服务信息的 HTTP
接口被叫做 exporter
。目前互联网公司罕用的服务大部分都有 exporter
能够间接应用,比方 Varnish
、Haproxy
、Nginx
、MySQL
、Linux
零碎信息(包含磁盘、内存、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 指标采集端口号 |
---|---|---|---|---|
user | 8000 | 9000 | 9080 | 9090 |
product | 8001 | 9001 | 9081 | 9091 |
order | 8002 | 9002 | 9082 | 9092 |
pay | 8003 | 9003 | 9083 | 9093 |
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-zero
并 star 反对咱们!
微信交换群
关注『微服务实际』公众号并点击 交换群 获取社区群二维码。