使用 docker 搭建 skywalking,并在 asp.net core 项目中加入监控
1、使用 docker-compose 快速搭建,脚本下载:https://github.com/apache/skywalking/tree/master/docker
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: elasticsearch:7.17.19
# 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: 10s
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
- ./elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
oap:
image: apache/skywalking-oap-server:9.7.0
# container_name: oap
depends_on:
elasticsearch:
condition: service_healthy
links:
- elasticsearch
network_mode: bridge
restart: always
ports:
- "11800:11800"
- "12800:12800"
healthcheck:
test: [ "CMD-SHELL", "curl http://localhost:12800/internal/l7check" ]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
environment:
SW_HEALTH_CHECKER: default
SW_TELEMETRY: prometheus
JAVA_OPTS: "-Xms2048m -Xmx2048m"
SW_STORAGE: elasticsearch
SW_STORAGE_ES_CLUSTER_NODES: elasticsearch:9200
ui:
image: apache/skywalking-ui:9.7.0
# container_name: ui
depends_on:
- oap
links:
- oap
network_mode: bridge
restart: always
ports:
- "8080:8080"
environment:
SW_OAP_ADDRESS: http://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
输了你,赢了世界又如何...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!