使用 docker 搭建 skywalking,并在 asp.net core 项目中加入监控

1、使用 docker-compose 快速搭建,脚本下载:https://github.com/apache/skywalking-docker/blob/master/8/8.1.0/compose-es7/docker-compose.yml

2、可自行修改脚本,如下,使用桥接网络以不占用公开的 elasticsearch 端口、去掉了固定的容器命名可以指定命名、增加了 elasticsearch 的数据外部映射便于数据持久化:

# 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
    network_mode: bridge
    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
    volumes:
      - ./elasticsearch/data:/usr/share/elasticsearch/data
  oap:
    image: apache/skywalking-oap-server:8.1.0-es7
#    container_name: oap
    depends_on:
      - elasticsearch
    links:
      - elasticsearch
    network_mode: bridge
    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
    network_mode: bridge
    restart: always
    ports:
      - 8080:8080
    environment:
      SW_OAP_ADDRESS: oap:12800

3、新建一个文件夹,将 docker-compose.yml 放入,在文件夹中,新建 elasticsearch/data 文件夹,并赋予写入权限,以便持久化数据(不需要可以在上面脚本中去除)

4、使用命令:docker-compose -p 'skywalking' up -d,将启动三个容器

5、使用 8080 端口访问 web 界面,应用通过 11800 端口调用 gRPC 接口

6、在 asp.net core 项目中加入名称为 SkyAPM.Agent.AspNetCore 的 nuget 包,版本为 1.0.0,以对应上面 skywalking 8.1.0 版本

7、在 asp.net core 项目中加入 skyapm.json 配置文件,内容如下:

{
  "SkyWalking": {
    "ServiceInstanceName": "name.001_othername",
    "ServiceName": "name",
    "Namespace": "",
    "HeaderVersions": [
      "sw8"
    ],
    "Sampling": {
      "SamplePer3Secs": -1,
      "Percentage": -1.0
    },
    "Logging": {
      "Level": "Information",
      "FilePath": "Logs\skyapm-{Date}.log"
    },
    "Transport": {
      "Interval": 3000,
      "ProtocolVersion": "v8",
      "QueueSize": 30000,
      "BatchSize": 3000,
      "gRPC": {
        "Servers": "skywalkingserviceaddress:11800",
        "Timeout": 10000,
        "ConnectTimeout": 10000,
        "ReportTimeout": 600000,
        "Authentication": ""
      }
    }
  }
}

其中:ServiceInstanceName 项格式必须为:字符.数字_字符,否则在 skywalking 服务端会报错,听说会在 8.2.0 中解决

8、在 Transport 的 gRPC 的 Servers 中,配置 skywalking 服务的地址(配置均针对 skywalking 8.1.0 的版本)

9、编辑 asp.net core 项目的 launchSettings.json 文件,在 environmentVariables 中添加环境变量(或在运行位置的环境变量中添加,或在代码中添加)

"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SkyAPM.Agent.AspNetCore"

10、启动项目,就应该是正常运行了。如果有问题,可以查看相应的项目日志文件,再进行解决。

参考文章:https://www.cnblogs.com/fallTakeMan/p/13437215.html

https://www.cnblogs.com/liuhaoyang/p/skywalking-dotnet-v02-release.html

https://www.cnblogs.com/SteveLee/p/SkyWalking_Exceptionless_Actual_Combat.html

https://www.cnblogs.com/fuxuyang/p/11819328.html

原文地址:https://www.cnblogs.com/xwgli/p/13827042.html