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
啟動之后可以看到 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.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
docker-compose 啟動之后大概1分鍾,啟動 api 項目,查看日志,沒有異常。訪問 api 項目,查看 SkyWalking UI,激動人心的時刻終於到來了。

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