利用Skywalking-netcore监控你的应用性能
Skywalking
SkyWalking開源項目由吳晟于2015年創建,同年10月在GitHub上作為個人項目開源。
SkyWalking項目的核心目標,是針對微服務、Cloud Native、容器化架構,提供應用性能監控(APM)和分布式調用鏈追蹤能力。
2017年11月,SkyWalking社區正式決定,尋求加入Apache基金會,希望能使項目成為更為開放、全球化和強大的APM開源產品,并加強來自社區的合作和交流。最終實現構建一款功能強大、簡單易用的開源APM產品。
2017年12月8日,Apache軟件基金會孵化器項目管理委員會 ASF IPMC宣布“SkyWalking全票通過,進入Apache孵化器”。
什么是APM
APM = Application Performance Management,即應用性能管理,主要是針對企業級應用軟件市場,對企業系統實施即時監控,以實現對應用程序性能管理和故障管理的系統化的解決方案。
APM的覆蓋范圍包括五個層次的實現:終端用戶體驗,應用架構映射,應用事務的分析,深度應用診斷,和數據分析。
過去,企業的IT部門在收集系統性能參數時,一般重點關注為最終用戶提供服務的硬件組件的利用率,如CPU利用率、內存占用、網絡吞吐量。雖然這種方法也提供了一些寶貴的信息,但卻忽視了最重要的因素:最終用戶的響應時間。
現在,通過事務處理過程監測、模擬等手段,可以真實測量用戶響應時間,此外還可以報告誰正在使用某一應用、該應用的使用頻率以及用戶所進行的事務處理過程是否成功完成、發生錯誤時的參數與堆棧調用信息、數據庫查詢語句跟蹤等。
.Net Core
自微軟發布 .net core 2.0以來,.net開發者迎來了幸福的春天,我們的程序將不再局限于Windows平臺。2.x版的.net core已趨于穩定,各廠的配套也在逐步跟上,使得整個生態在逐漸的豐富起來。
SkyWalking-netcore是.net core平臺下的代理程序,在NCC社區的Lemon大神帶領下進行開發,并于2018年4月17日正式加入OpenSkywalking,于2018年4月19日發布v0.1.0版本。
實驗目標
布署一個基于SkyWalking的.net core應用監控系統,監控Web應用的性能。
所需第三方軟件
XShell
WinSCP
Visual Studio 2017
.net Core 2.0.3 SDK
JDK8+
Elasticsearch 5.x
網絡結構
本次實驗采用三臺服務器 ,Elasticsearch 與 Collector 放在一臺服務器上,收集另外兩臺Web服務器提供的數據,實驗應用架構如下圖。
實驗架構
實驗過程
安裝系統
Server-01、Web01、Web02安裝 CentOS系統,安裝過程詳見《IT基礎設施:CentOS7安裝指南》
IP配置表
| Server-01 | 192.168.10.191 | 提供ES服務、Collector與WebUI |
| Web01 | 192.168.10.192 | 運行App1 |
| Web02 | 192.168.10.193 | 運行 App2 |
| MSSQL | 暫無 | 因.net Core的代理程序暫未提供支持,故本文不演示此部分,待新版本發布后更新 |
| WebService | 暫無 | 因.net Core的代理程序暫未提供支持,故本文不演示此部分,待新版本發布后更新 |
安裝 .net core 2.0 SDK
在Web01、Web02上安裝.net core 2.0 SDK,詳見《在CentOS7下安裝.Net Core 2.0.3 SDK》
安裝JDK8
因為oracle現在要同意協議才能下載,直接使用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
也可以先用迅雷下到主機,再通過局域網下載到虛擬機
使用rpm -ivh jdk-8u45-linux-x64.rpm進行安裝
命令執行完畢即安裝完成,使用java -version 檢查是否安裝成功
部署Elasticsearch
1、到官網下載5.x版,目前為5.6.9
下載5.x
2、使用WinSCP上傳到Server-01,解壓到/home/elasticsearch5.6.9下。
3、修改/home/elasticsearch5.6.9/config/elasticsearch.yml
設置 cluster.name: CollectorDBCluster。此名稱需要和collector配置文件一致。
設置 node.name: masterNode, 節點名稱可以設置為任意名字,如為集群模式,則每個節點名稱需要不同。
增加如下配置
最終配置如下:
# 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
運行/home/elasticsearch5.6.9/bin/elasticsearch.sh啟動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中的所有localhost為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
運行/home/collector/bin/startup.sh啟動WebUI和Collector。
創建WebApplication
接下來,我們創建一個Web應用,并在其中加入Skywalking for dotnet core的代理程序。
1、打開VS2017,創建一個.Net Core MVC應用,請跟著我操作:VS中點擊菜單“文件”——“新建”——“項目”(按Ctrl+Shift+N同等效果),在彈出的對話框中從左到右進行如下操作。
.Net Core Web應用
選擇項目類型
2、等項目建好,右鍵點擊“依賴項”——“管理Nuget程序包”,打開Nuget管理器。
操作
在“瀏覽”選項卡,輸入“Skywalking.AspNetCore”搜索,并安裝
找到包
安裝前需要接受許可證,點擊“我接受”(不接受請關閉本文)
許可證
3、安裝完成后,打開Startup.cs,在ConfigureServices函數中加入
? ? ? ? ? ?services.AddSkyWalking(option =>{ ? ? ? ? ? ? ? ?//這里填本應用的名稱,每個應用不同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 =>{ ? ? ? ? ? ? ? ?//這里填本應用的名稱,每個應用不同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?}");});}} }
打開Program.cs文件,允許非本地訪問,最終代碼如下
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、發布:點擊我們的WEB應用,右鍵——發布,接下來看圖操作。
發布
發布到文件夾
選擇目標位置
發布
沒有失敗,恭喜,大吉大利,今晚吃雞
我們可以看到,發布完成后的文件,一大堆,我們把這個文件夾改名為App1
發布成功
5、接下來,我們在StartUp.cs中把ApplicationCode改一下,按上面的步驟再發布一次。
image.png
這次發布生成的Publish3文件夾,我們改名為App2
布署Web
1、打開WinSCP,連上Web01,將App1拖到/home下。
2、打開XShell,連上Web01,執行如下命令
# 給予執行權限chmod 777 /home/App1/WebApplication2.dll
# 在后臺運行應用nohup dotnet /home/App1/WebApplication2.dll &
同樣的操作,將App2布署到Web02上。
訪問應用
我們打開兩個應用,隨機點擊一下應用頂部的鏈接。
http://192.168.10.192:5000,這是我們的app1
http://192.168.10.193:5000,這是我們的app2
查看監測
打開http://192.168.10.191:8080,查看監測情況
監測到的應用
查看被訪問的服務響應速度
服務狀態
結語
Skywalking-netcore做為.Net core社區的新生命,具有無限的潛力,據可靠消息,新版本將更深入地監測數據庫操作及狀態,HTTP請求消息以及StackTrace跟蹤,讓我們一起為它打Call加油吧。猛戮這里,到Github給Skywalking-netcore點星星。 :)
原文地址: https://www.jianshu.com/p/3ddd986c7581
.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com
總結
以上是生活随笔為你收集整理的利用Skywalking-netcore监控你的应用性能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软Build 2018展示Visual
- 下一篇: 使用Swashbuckle构建RESTf