学习笔记:带你十天轻松完成 Go 微服务系列(八)- 服务监控

学习笔记:带你十天轻松完成 Go 微服务系列(八)- 服务监控,第1张

学习笔记:带你十天轻松搞定 Go 微服务系列(八)- 服务监控 1、学习课程2、添加服务2.1 添加 user api 服务 Prometheus 配置2.2 添加 user rpc 服务 Prometheus 配置2.3 添加 product api 服务 Prometheus 配置2.4 添加 product rpc 服务 Prometheus 配置2.5 添加 order api 服务 Prometheus 配置2.6 添加 order rpc 服务 Prometheus 配置2.7 添加 pay api 服务 Prometheus 配置2.8 添加 pay rpc 服务 Prometheus 配置 3、修改 Prometheus 配置4、重启容器5、启动所有服务6、访问 Prometheus 可视化界面7、使用 Grafana 可视化 Prometheus 指标数据7.1 添加 Prometheus 数据源7.2 添加 Variables 用于服务筛选7.3 添加 api 接口 qps 仪表盘7.4 添加 rpc 接口 Qps 仪表盘7.5 添加 api 接口状态码仪表盘7.6 添加 rpc 接口状态码仪表盘7.7 保存仪表盘 8、测试9、系列

1、学习课程

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

今天是学习 go 微服务的第八天,我们继续接着昨天的步骤,搭建服务监控

2、添加服务 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
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
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
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
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
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
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
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

提示:配置修改后,需要重启服务才会生效。

3、修改 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=` 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/rpc
go run user.go -f etc/user.yaml
cd mall/service/user/api
go run user.go -f etc/user.yaml
cd mall/service/product/rpc
go run product.go -f etc/product.yaml
cd mall/service/product/api
go run product.go -f etc/product.yaml
cd mall/service/order/rpc
go run order.go -f etc/order.yaml
cd mall/service/order/api
go run order.go -f etc/order.yaml
cd mall/service/pay/rpc
go run pay.go -f etc/pay.yaml
cd mall/service/pay/api
go 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 测试用户注册接口,每次进行 *** 作仪表盘就会反应

9、系列

学习笔记:带你十天轻松搞定 Go 微服务系列(一)- 环境搭建
学习笔记:带你十天轻松搞定 Go 微服务系列(二)- 服务拆分
学习笔记:带你十天轻松搞定 Go 微服务系列(三)- 用户服务
学习笔记:带你十天轻松搞定 Go 微服务系列(四)- 产品服务
学习笔记:带你十天轻松搞定 Go 微服务系列(五)- 订单服务
学习笔记:带你十天轻松搞定 Go 微服务系列(六)- 支付服务
学习笔记:带你十天轻松搞定 Go 微服务系列(七)- RPC 服务 Auth 验证
学习笔记:带你十天轻松搞定 Go 微服务系列(八)- 服务监控
学习笔记:带你十天轻松搞定 Go 微服务系列(九)- 链路追踪
学习笔记:带你十天轻松搞定 Go 微服务系列大结局(十)- 分布式事务

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/994301.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存