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

 

 

  项目属性添加两个环境变量。

  SKYWALKING__SERVICENAME:skyapm

  ASPNETCORE_HOSTINGSTARTUPASSEMBLIES:SkyAPM.Agent.AspNetCore

 

  启动项目,没有异常,但是也没有展示出数据。查看日志,发现服务注册失败。

[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,一定要选对版本,不要盲目求新啊

 

posted @ 2020-08-07 15:22  原来是李  阅读(6052)  评论(43编辑  收藏  举报