1、学习课程

带你十天轻松搞定 Go 微服务系列(八)- 服务监控

明天是学习 go 微服务的第八天,咱们持续接着昨天的步骤,搭建服务监控

2、增加服务

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

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

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

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

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

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

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

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

提醒:配置批改后,须要重启服务才会失效。

3、批改 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'

4、重启容器

配置文件批改好后,须要重启服务容器能力失效。

systemctl restart docker

5、启动所有服务

通过 exec 命令对指定的容器执行 bash,我是

docker exec -it 13258ded6d62  /bin/bash

而后在容器内开启所有服务

cd mall/service/user/rpcgo run user.go -f etc/user.yaml
cd mall/service/user/apigo run user.go -f etc/user.yaml
cd mall/service/product/rpcgo run product.go -f etc/product.yaml
cd mall/service/product/apigo run product.go -f etc/product.yaml
cd mall/service/order/rpcgo run order.go -f etc/order.yaml
cd mall/service/order/apigo run order.go -f etc/order.yaml
cd mall/service/pay/rpcgo run pay.go -f etc/pay.yaml
cd mall/service/pay/apigo run pay.go -f etc/pay.yaml

6、拜访 Prometheus 可视化界面

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

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

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

7、应用 Grafana 可视化 Prometheus 指标数据

7.1 增加 Prometheus 数据源

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

而后抉择 Prometheus 数据源

填写 HTTP 配置中 URL 地址(我这里的 IP地址 是 Prometheus 所在容器的 IP地址)

而后点击 Save & test 按钮,上方会提醒 Data source is working,阐明咱们数据源增加胜利且失常工作。

7.2 增加 Variables 用于服务筛选

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

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

7.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

7.4 增加 rpc 接口 Qps 仪表盘

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

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

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

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

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

7.7 保留仪表盘

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

8、测试

在 postman 测试用户注册接口,每次进行操作仪表盘就会反馈