1. 程式人生 > >在 .NetCore 專案中使用 SkyWalkingAPM 踩坑排坑日記

在 .NetCore 專案中使用 SkyWalkingAPM 踩坑排坑日記

 SkyWalking 概述

  SkyWalking 是觀察性分析平臺和應用效能管理系統。提供分散式追蹤、服務網格遙測分析、度量聚合和視覺化一體化解決方案。支援Java, .Net Core, PHP, NodeJS, Golang, LUA語言探針,支援Envoy + Istio構建的Service Mesh。

  這裡丟擲兩個概念,SkyWalking 服務和語言探針。SkyWalking 本身是用 Java 寫的,作為應用效能管理系統,探針是收集應用效能指標資料的,比如在 .net core 專案中引入 SkyAPM.Agent.AspNetCore,做一些配置,就可以將該專案的資料報告給 SkyWalking 服務,在 SkyWalking UI 介面看到視覺化的資料。

  SkyWalking 展示的資料是需要儲存的,預設是 H2,同時也支援使用ElasticSearch、MySQL、TiDB、InfluxDB,這裡使用 elasticsearch。

環境說明

  本機開發環境:Win10 + VS2019,伺服器是 CentOS,ip:172.17.81.23

Docker 方式(踩坑)

  安裝 Elasticsearch

docker run --name elasticsearch --restart always -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.8.1

  安裝 Skywalking

docker run --name skywalking --restart always -d -p 11800:11800 -p 12800:12800 --link elasticsearch:elasticsearch -e SW_STORAGE=elasticsearch -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server:8.1.0-es7

  安裝 SkyWalking-UI

docker run --name skywalking-ui --restart always -d -p 8080:8080 --link skywalking:skywalking -e SW_OAP_ADDRESS=skywalking:12800 apache/skywalking-ui:8.1.0

 

  本機訪問 skywalking 服務,然而頁面一片空白,審查元素髮現這麼一句話:We're sorry but SkyWalking doesn't work properly without JavaScript enabled.Please enable it to continue.

 

  檢視 Docker 服務,發現 SkyWalking 一直在 restart 狀態。檢視 dockerr 日誌,skywalking 連線到 elasticsearch 超時。

 

Docker Compose 方式(踩坑)

  說明:用到的 compose 檔案版本是 3.8,需要 Docker 版本在 19.03.0 或更新的版本。

mkdir skywalking
cd skywalking
vi docker-compose.yml
# 內容見 https://github.com/apache/skywalking-docker/blob/master/8/8.1.0/compose-es7/docker-compose.yml docker-compose up -d
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: '3.8'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
    container_name: elasticsearch
    restart: always
    ports:
      - 9200:9200
    healthcheck:
      test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    environment:
      - discovery.type=single-node
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
  oap:
    image: apache/skywalking-oap-server:8.1.0-es7
    container_name: oap
    depends_on:
      - elasticsearch
    links:
      - elasticsearch
    restart: always
    ports:
      - 11800:11800
      - 12800:12800
    healthcheck:
      test: ["CMD-SHELL", "/skywalking/bin/swctl"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    environment:
      SW_STORAGE: elasticsearch7
      SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200
  ui:
    image: apache/skywalking-ui:8.1.0
    container_name: ui
    depends_on:
      - oap
    links:
      - oap
    restart: always
    ports:
      - 8080:8080
    environment:
      SW_OAP_ADDRESS: oap:12800
sw8.1-es7.5/docker-compose.yml

  啟動之後可以看到 docker 服務狀態正常。狀態也是 healthy,總算放心了。

  本機訪問 SkyWalking 服務地址,UI 暴露埠是 8080. 審查網路,沒有錯誤。

Asp.Net Core 專案應用

  建立一個 WebAPI 專案,在專案中引用 NuGet 包:SkyAPM.Agent.AspNetCore,當前版本是 0.9.0

  在專案根目錄下新增 skyapm.json 配置檔案。gRPC 下 的 Servers 為 SkyWalking 服務地址,注意替換成自己的服務地址。

{
  "SkyWalking": {
    "ServiceName": "skyapm",
    "Namespace": "",
    "HeaderVersions": [
      "sw6"
    ],
    "Sampling": {
      "SamplePer3Secs": -1,
      "Percentage": -1.0
    },
    "Logging": {
      "Level": "Information",
      "FilePath": "logs\\skyapm-{Date}.log"
    },
    "Transport": {
      "Interval": 3000,
      "ProtocolVersion": "v6",
      "QueueSize": 30000,
      "BatchSize": 3000,
      "gRPC": {
        "Servers": "172.17.81.23:11800",
        "Timeout": 10000,
        "ConnectTimeout": 10000,
        "ReportTimeout": 600000
      }
    }
  }
}
skyapm.json

  也可以通過命令列工具來生成配置檔案。安裝 SkyAPM.DotNet.CLI,在專案根目錄下使用 CLI 命令生成 SkyWalking 配置檔案。命令規則是:dotnet skyapm config [your_service_name] [your_servers]

dotnet tool install -g SkyAPM.DotNet.CLI

dotnet skyapm config skyapm 172.17.81.23:11800

 

 

  啟動專案,沒有異常,但是也沒有展示出資料。檢視日誌,發現服務註冊失敗。

[skyapm] [Error] SkyApm.Transport.Grpc.V6.ServiceRegister : Register service fail.
Grpc.Core.RpcException: Status(StatusCode=Unimplemented, Detail="Method not found: Register/doServiceRegister")
   at SkyApm.Transport.Grpc.V6.ServiceRegister.

  翻看原始碼,查詢GitHub issue,原來這個註冊方法是 SkyWalking v7.0 中的方法,新的版本中是沒有這個方法的。

 

 

  破案了,SkyAPM.Agent.AspNetCore 這個 NuGet 包有一年未更新了,不支援 SkyWalking 8,在 .Net Core 專案中需要使用 SkyWalking v7 或更低的版本。

 

 Docker 方式(√)

  SW7.0 之後的版本是用不了了,所以目前只能用 7.0 版本。

docker run --name elasticsearch --restart always -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.8.1

docker run --name skywalking --restart always -d -p 11800:11800 -p 12800:12800 --link elasticsearch:elasticsearch -e SW_STORAGE=elasticsearch7 -e SW_STORAGE_ES_CLUSTER_NODES=elasticsearch:9200 apache/skywalking-oap-server:7.0.0-es7

docker run --name skywalking-ui --restart always -d -p 8080:8080 --link skywalking:skywalking -e SW_OAP_ADDRESS=skywalking:12800 apache/skywalking-ui:7.0.0

 

Docker Compose 方式(√)

  將 docker-compose.yml 的內容替換成這個版本:https://github.com/apache/skywalking-docker/blob/master/7/7.0/compose-es7/docker-compose.yml。6.6版本親測可行。

version: '3.3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
    container_name: elasticsearch
    restart: always
    ports:
      - 9200:9200
    environment:
      - discovery.type=single-node
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
  oap:
    image: apache/skywalking-oap-server:7.0.0-es7
    container_name: oap
    depends_on:
      - elasticsearch
    links:
      - elasticsearch
    restart: always
    ports:
      - 11800:11800
      - 12800:12800
    environment:
      SW_STORAGE: elasticsearch7
      SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200
  ui:
    image: apache/skywalking-ui:7.0.0
    container_name: ui
    depends_on:
      - oap
    links:
      - oap
    restart: always
    ports:
      - 8080:8080
    environment:
      SW_OAP_ADDRESS: oap:12800
sw7.0-es7.5/docker-compose.yml

  docker-compose 啟動之後大概1分鐘,啟動 api 專案,檢視日誌,沒有異常。訪問 api 專案,檢視 SkyWalking UI,激動人心的時刻終於到來了。

 

 總結

  SkyWalking 不同語言探針不是同一個團隊開發的,SkyAPM-dotnet 也沒怎麼更新了,在 .NetCore 平臺使用 SkyWalking 作為 APM,一定要選對版本,不要盲目求新啊

&n