利用Skywalking-netcore监控你的应用性能
Skywalking
SkyWalking開(kāi)源項(xiàng)目由吳晟于2015年創(chuàng)建,同年10月在GitHub上作為個(gè)人項(xiàng)目開(kāi)源。
SkyWalking項(xiàng)目的核心目標(biāo),是針對(duì)微服務(wù)、Cloud Native、容器化架構(gòu),提供應(yīng)用性能監(jiān)控(APM)和分布式調(diào)用鏈追蹤能力。
2017年11月,SkyWalking社區(qū)正式?jīng)Q定,尋求加入Apache基金會(huì),希望能使項(xiàng)目成為更為開(kāi)放、全球化和強(qiáng)大的APM開(kāi)源產(chǎn)品,并加強(qiáng)來(lái)自社區(qū)的合作和交流。最終實(shí)現(xiàn)構(gòu)建一款功能強(qiáng)大、簡(jiǎn)單易用的開(kāi)源APM產(chǎn)品。
2017年12月8日,Apache軟件基金會(huì)孵化器項(xiàng)目管理委員會(huì) ASF IPMC宣布“SkyWalking全票通過(guò),進(jìn)入Apache孵化器”。
什么是APM
APM = Application Performance Management,即應(yīng)用性能管理,主要是針對(duì)企業(yè)級(jí)應(yīng)用軟件市場(chǎng),對(duì)企業(yè)系統(tǒng)實(shí)施即時(shí)監(jiān)控,以實(shí)現(xiàn)對(duì)應(yīng)用程序性能管理和故障管理的系統(tǒng)化的解決方案。
APM的覆蓋范圍包括五個(gè)層次的實(shí)現(xiàn):終端用戶(hù)體驗(yàn),應(yīng)用架構(gòu)映射,應(yīng)用事務(wù)的分析,深度應(yīng)用診斷,和數(shù)據(jù)分析。
過(guò)去,企業(yè)的IT部門(mén)在收集系統(tǒng)性能參數(shù)時(shí),一般重點(diǎn)關(guān)注為最終用戶(hù)提供服務(wù)的硬件組件的利用率,如CPU利用率、內(nèi)存占用、網(wǎng)絡(luò)吞吐量。雖然這種方法也提供了一些寶貴的信息,但卻忽視了最重要的因素:最終用戶(hù)的響應(yīng)時(shí)間。
現(xiàn)在,通過(guò)事務(wù)處理過(guò)程監(jiān)測(cè)、模擬等手段,可以真實(shí)測(cè)量用戶(hù)響應(yīng)時(shí)間,此外還可以報(bào)告誰(shuí)正在使用某一應(yīng)用、該應(yīng)用的使用頻率以及用戶(hù)所進(jìn)行的事務(wù)處理過(guò)程是否成功完成、發(fā)生錯(cuò)誤時(shí)的參數(shù)與堆棧調(diào)用信息、數(shù)據(jù)庫(kù)查詢(xún)語(yǔ)句跟蹤等。
.Net Core
自微軟發(fā)布 .net core 2.0以來(lái),.net開(kāi)發(fā)者迎來(lái)了幸福的春天,我們的程序?qū)⒉辉倬窒抻赪indows平臺(tái)。2.x版的.net core已趨于穩(wěn)定,各廠(chǎng)的配套也在逐步跟上,使得整個(gè)生態(tài)在逐漸的豐富起來(lái)。
SkyWalking-netcore是.net core平臺(tái)下的代理程序,在NCC社區(qū)的Lemon大神帶領(lǐng)下進(jìn)行開(kāi)發(fā),并于2018年4月17日正式加入OpenSkywalking,于2018年4月19日發(fā)布v0.1.0版本。
實(shí)驗(yàn)?zāi)繕?biāo)
布署一個(gè)基于SkyWalking的.net core應(yīng)用監(jiān)控系統(tǒng),監(jiān)控Web應(yīng)用的性能。
所需第三方軟件
XShell
WinSCP
Visual Studio 2017
.net Core 2.0.3 SDK
JDK8+
Elasticsearch 5.x
網(wǎng)絡(luò)結(jié)構(gòu)
本次實(shí)驗(yàn)采用三臺(tái)服務(wù)器 ,Elasticsearch 與 Collector 放在一臺(tái)服務(wù)器上,收集另外兩臺(tái)Web服務(wù)器提供的數(shù)據(jù),實(shí)驗(yàn)應(yīng)用架構(gòu)如下圖。
實(shí)驗(yàn)架構(gòu)
實(shí)驗(yàn)過(guò)程
安裝系統(tǒng)
Server-01、Web01、Web02安裝 CentOS系統(tǒng),安裝過(guò)程詳見(jiàn)《IT基礎(chǔ)設(shè)施:CentOS7安裝指南》
IP配置表
| Server-01 | 192.168.10.191 | 提供ES服務(wù)、Collector與WebUI |
| Web01 | 192.168.10.192 | 運(yùn)行App1 |
| Web02 | 192.168.10.193 | 運(yùn)行 App2 |
| MSSQL | 暫無(wú) | 因.net Core的代理程序暫未提供支持,故本文不演示此部分,待新版本發(fā)布后更新 |
| WebService | 暫無(wú) | 因.net Core的代理程序暫未提供支持,故本文不演示此部分,待新版本發(fā)布后更新 |
安裝 .net core 2.0 SDK
在Web01、Web02上安裝.net core 2.0 SDK,詳見(jiàn)《在CentOS7下安裝.Net Core 2.0.3 SDK》
安裝JDK8
因?yàn)閛racle現(xiàn)在要同意協(xié)議才能下載,直接使用wget加鏈接下載不到,所以要加上前面的那些代碼:
wget --no-check-certificate --no-cookie --header "Cookie: oraclelicense=accept-securebackup-cookie;" http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.rpm
也可以先用迅雷下到主機(jī),再通過(guò)局域網(wǎng)下載到虛擬機(jī)
使用rpm -ivh jdk-8u45-linux-x64.rpm進(jìn)行安裝
命令執(zhí)行完畢即安裝完成,使用java -version 檢查是否安裝成功
部署Elasticsearch
1、到官網(wǎng)下載5.x版,目前為5.6.9
下載5.x
2、使用WinSCP上傳到Server-01,解壓到/home/elasticsearch5.6.9下。
3、修改/home/elasticsearch5.6.9/config/elasticsearch.yml
設(shè)置 cluster.name: CollectorDBCluster。此名稱(chēng)需要和collector配置文件一致。
設(shè)置 node.name: masterNode, 節(jié)點(diǎn)名稱(chēng)可以設(shè)置為任意名字,如為集群模式,則每個(gè)節(jié)點(diǎn)名稱(chēng)需要不同。
增加如下配置
最終配置如下:
# 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.
#!/usr/bin/env sh
PRG="$0"
PRGDIR=`dirname "$PRG"`
[ -z "$WEBAPP_HOME" ] && WEBAPP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
WEBAPP_LOG_DIR="${WEBAPP_HOME}/logs"
JAVA_OPTS=" -Xms256M -Xmx512M"
JAR_PATH="${WEBAPP_HOME}/webapp"
if [ ! -d "${WEBAPP_HOME}/logs" ]; then
? ? mkdir -p "${WEBAPP_LOG_DIR}"
fi
_RUNJAVA=${JAVA_HOME}/bin/java
[ -z "$JAVA_HOME" ] && _RUNJAVA=java
eval exec "\"$_RUNJAVA\" ${JAVA_OPTS} -jar ${JAR_PATH}/skywalking-webapp.jar \
? ? ? ? ?--server.port=8080 --collector.ribbon.listOfServers=192.168.10.191:10800 \
? ? ? ? 2>${WEBAPP_LOG_DIR}/webapp.log 1> /dev/null &"
if [ $? -eq 0 ]; then
? ? sleep 1
? ? echo "Skywalking Web Application started successfully!"
else
? ? echo "Skywalking Web Application started failure!"
? ? exit 1
fi
運(yùn)行/home/elasticsearch5.6.9/bin/elasticsearch.sh啟動(dòng)Elasticsearch部署collector
下載release的版本https://github.com/OpenSkywalking/skywalking-netcore/releases
下載
將壓縮包解壓到Server-01的/home/collector/目錄
修改/home/collector/bin/webappService.sh中的127.0.0.1為Server-01的IP地址,目前為192.168.10.191。
最終腳本如下:
# 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.PRG="$0"PRGDIR=`dirname "$PRG"` [ -z "$WEBAPP_HOME" ] && WEBAPP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`WEBAPP_LOG_DIR="${WEBAPP_HOME}/logs"JAVA_OPTS=" -Xms256M -Xmx512M"JAR_PATH="${WEBAPP_HOME}/webapp"if [ ! -d "${WEBAPP_HOME}/logs" ]; thenmkdir -p "${WEBAPP_LOG_DIR}"fi_RUNJAVA=${JAVA_HOME}/bin/java [ -z "$JAVA_HOME" ] && _RUNJAVA=javaeval exec "\"$_RUNJAVA\" ${JAVA_OPTS} -jar ${JAR_PATH}/skywalking-webapp.jar \--server.port=8080 --collector.ribbon.listOfServers=192.168.10.191:10800 \2>${WEBAPP_LOG_DIR}/webapp.log 1> /dev/null &"if [ $? -eq 0 ]; thensleep 1 ? ?echo "Skywalking Web Application started successfully!"elseecho "Skywalking Web Application started failure!"exit 1fi修改/home/collector/config/application.yml中的所有l(wèi)ocalhost為Server-01的IP地址,目前為192.168.10.191。
最終配置文件如下:
# 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.#cluster:
# ?zookeeper:
# ? ?hostPort: localhost:2181
# ? ?sessionTimeout: 100000naming:jetty:host: 192.168.10.191port: 10800contextPath: / cache:# ?guava:caffeine: remote:gRPC:host: 192.168.10.191port: 11800agent_gRPC:gRPC:host: 192.168.10.191port: 11800#Set these two setting to open ssl#sslCertChainFile: $path#sslPrivateKeyFile: $path#Set your own token to active auth#authentication: xxxxxxagent_jetty:jetty:host: 192.168.10.191port: 12800contextPath: / analysis_register: ?default: analysis_jvm: ?default: analysis_segment_parser: ?default:bufferFilePath: ../buffer/bufferOffsetMaxFileSize: 10MbufferSegmentMaxFileSize: 500M ui:jetty:host: 192.168.10.191port: 12800contextPath: / storage:elasticsearch:clusterName: CollectorDBClusterclusterTransportSniffer: trueclusterNodes: localhost:9300indexShardsNumber: 2indexReplicasNumber: 0highPerformanceMode: truettl: 7
#storage:# ?h2:
# ? ?url: jdbc:h2:~/memorydb
# ? ?userName: saconfiguration: ?default
:# ? ? namespace: xxxxxapplicationApdexThreshold: 2000serviceErrorRateThreshold: 10.00serviceAverageResponseTimeThreshold: 2000instanceErrorRateThreshold: 10.00instanceAverageResponseTimeThreshold: 2000applicationErrorRateThreshold: 10.00applicationAverageResponseTimeThreshold: 2000
運(yùn)行/home/collector/bin/startup.sh啟動(dòng)WebUI和Collector。
創(chuàng)建WebApplication
接下來(lái),我們創(chuàng)建一個(gè)Web應(yīng)用,并在其中加入Skywalking for dotnet core的代理程序。
1、打開(kāi)VS2017,創(chuàng)建一個(gè).Net Core MVC應(yīng)用,請(qǐng)跟著我操作:VS中點(diǎn)擊菜單“文件”——“新建”——“項(xiàng)目”(按Ctrl+Shift+N同等效果),在彈出的對(duì)話(huà)框中從左到右進(jìn)行如下操作。
.Net Core Web應(yīng)用
選擇項(xiàng)目類(lèi)型
2、等項(xiàng)目建好,右鍵點(diǎn)擊“依賴(lài)項(xiàng)”——“管理Nuget程序包”,打開(kāi)Nuget管理器。
操作
在“瀏覽”選項(xiàng)卡,輸入“Skywalking.AspNetCore”搜索,并安裝
找到包
安裝前需要接受許可證,點(diǎn)擊“我接受”(不接受請(qǐng)關(guān)閉本文)
許可證
3、安裝完成后,打開(kāi)Startup.cs,在ConfigureServices函數(shù)中加入
? ? ? ? ? ?services.AddSkyWalking(option =>{ ? ? ? ? ? ? ? ?//這里填本應(yīng)用的名稱(chēng),每個(gè)應(yīng)用不同option.ApplicationCode = "OurApplication1"; ? ? ? ? ? ? ?? ? ? ? ? ??//這里填Collector的地址option.DirectServers = "192.168.10.191:11800";});
Startup.cs完整代碼如下
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using SkyWalking.AspNetCore;namespace WebApplication2 { ? ?public class Startup{public Startup(IConfiguration configuration){Configuration = configuration;} ? ? ? ?public IConfiguration Configuration { get; } ? ? ??// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){services.AddSkyWalking(option =>{ ? ? ? ? ? ? ? ?//這里填本應(yīng)用的名稱(chēng),每個(gè)應(yīng)用不同option.ApplicationCode = "OurApplication1"; ? ? ? ? ? ?
?? ?//這里填Collector的地址option.DirectServers = "192.168.10.191:11800";});services.AddMvc();} ? ? ? ?// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){ ? ? ? ? ? ?if (env.IsDevelopment()){app.UseDeveloperExceptionPage();app.UseBrowserLink();} ? ? ? ? ? ?else{app.UseExceptionHandler("/Home/Error");}app.UseStaticFiles();app.UseMvc(routes =>{routes.MapRoute(name: "default", ? ? ? ? ? ? ? ? ?
?? ? ?template: "{controller=Home}/{action=Index}/{id?}");});}} }
打開(kāi)Program.cs文件,允許非本地訪(fǎng)問(wèn),最終代碼如下
using System;using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace WebApplication2 { ? ?public class Program{public static void Main(string[] args){BuildWebHost(args).Run();} ? ? ? ?public static IWebHost BuildWebHost(string[] args) =>WebHost.CreateDefaultBuilder(args).UseUrls("http://*:5000").UseStartup<Startup>().Build();} }
4、發(fā)布:點(diǎn)擊我們的WEB應(yīng)用,右鍵——發(fā)布,接下來(lái)看圖操作。
發(fā)布
發(fā)布到文件夾
選擇目標(biāo)位置
發(fā)布
沒(méi)有失敗,恭喜,大吉大利,今晚吃雞
我們可以看到,發(fā)布完成后的文件,一大堆,我們把這個(gè)文件夾改名為App1
發(fā)布成功
5、接下來(lái),我們?cè)赟tartUp.cs中把ApplicationCode改一下,按上面的步驟再發(fā)布一次。
image.png
這次發(fā)布生成的Publish3文件夾,我們改名為App2
布署Web
1、打開(kāi)WinSCP,連上Web01,將App1拖到/home下。
2、打開(kāi)XShell,連上Web01,執(zhí)行如下命令
# 給予執(zhí)行權(quán)限chmod 777 /home/App1/WebApplication2.dll
# 在后臺(tái)運(yùn)行應(yīng)用nohup dotnet /home/App1/WebApplication2.dll &
同樣的操作,將App2布署到Web02上。
訪(fǎng)問(wèn)應(yīng)用
我們打開(kāi)兩個(gè)應(yīng)用,隨機(jī)點(diǎn)擊一下應(yīng)用頂部的鏈接。
http://192.168.10.192:5000,這是我們的app1
http://192.168.10.193:5000,這是我們的app2
查看監(jiān)測(cè)
打開(kāi)http://192.168.10.191:8080,查看監(jiān)測(cè)情況
監(jiān)測(cè)到的應(yīng)用
查看被訪(fǎng)問(wèn)的服務(wù)響應(yīng)速度
服務(wù)狀態(tài)
結(jié)語(yǔ)
Skywalking-netcore做為.Net core社區(qū)的新生命,具有無(wú)限的潛力,據(jù)可靠消息,新版本將更深入地監(jiān)測(cè)數(shù)據(jù)庫(kù)操作及狀態(tài),HTTP請(qǐng)求消息以及StackTrace跟蹤,讓我們一起為它打Call加油吧。猛戮這里,到Github給Skywalking-netcore點(diǎn)星星。 :)
原文地址: https://www.jianshu.com/p/3ddd986c7581
.NET社區(qū)新聞,深度好文,歡迎訪(fǎng)問(wèn)公眾號(hào)文章匯總 http://www.csharpkit.com
總結(jié)
以上是生活随笔為你收集整理的利用Skywalking-netcore监控你的应用性能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 微软Build 2018展示Visual
- 下一篇: 使用Swashbuckle构建RESTf