利用Skywalking-netcore監控你的應用性能

Skywalking

SkyWalking開源項目由吳晟於2015年建立,同年10月在GitHub上做爲我的項目開源。javascript

SkyWalking項目的核心目標,是針對微服務、Cloud Native、容器化架構,提供應用性能監控(APM)和分佈式調用鏈追蹤能力。php

2017年11月,SkyWalking社區正式決定,尋求加入Apache基金會,但願能使項目成爲更爲開放、全球化和強大的APM開源產品,並增強來自社區的合做和交流。最終實現構建一款功能強大、簡單易用的開源APM產品。css

2017年12月8日,Apache軟件基金會孵化器項目管理委員會 ASF IPMC宣佈「SkyWalking全票經過,進入Apache孵化器」。java

什麼是APM

APM = Application Performance Management,即應用性能管理,主要是針對企業級應用軟件市場,對企業系統實施即時監控,以實現對應用程序性能管理和故障管理的系統化的解決方案。node

APM的覆蓋範圍包括五個層次的實現:終端用戶體驗,應用架構映射,應用事務的分析,深度應用診斷,和數據分析。linux

過去,企業的IT部門在收集系統性能參數時,通常重點關注爲最終用戶提供服務的硬件組件的利用率,如CPU利用率、內存佔用、網絡吞吐量。雖然這種方法也提供了一些寶貴的信息,但卻忽視了最重要的因素:最終用戶的響應時間。git

如今,經過事務處理過程監測、模擬等手段,能夠真實測量用戶響應時間,此外還能夠報告誰正在使用某一應用、該應用的使用頻率以及用戶所進行的事務處理過程是否成功完成、發生錯誤時的參數與堆棧調用信息、數據庫查詢語句跟蹤等。github

 

.Net Core

自微軟發佈 .net core 2.0以來,.net開發者迎來了幸福的春天,咱們的程序將再也不侷限於Windows平臺。2.x版的.net core已趨於穩定,各廠的配套也在逐步跟上,使得整個生態在逐漸的豐富起來。web

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服務器提供的數據,實驗應用架構以下圖。

640?wx_fmt=png

實驗架構

實驗過程

安裝系統

Server-0一、Web0一、Web02安裝 CentOS系統,安裝過程詳見《IT基礎設施:CentOS7安裝指南》

IP配置表

服務器 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

在Web0一、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

一、到官網下載5.x版,目前爲5.6.9

640?wx_fmt=png

下載5.x

 

二、使用WinSCP上傳到Server-01,解壓到/home/elasticsearch5.6.9下。
三、修改/home/elasticsearch5.6.9/config/elasticsearch.yml

  • 設置 cluster.name: CollectorDBCluster。此名稱須要和collector配置文件一致。

  • 設置 node.name: masterNode, 節點名稱能夠設置爲任意名字,如爲集羣模式,則每一個節點名稱須要不一樣。
    增長以下配置

cluster.name: CollectorDBClusternetwork.host: 0.0.0.0thread_pool.bulk.queue_size: 1000cluster.name: CollectorDBClusternetwork.host: 0.0.0.0thread_pool.bulk.queue_size: 1000

最終配置以下:

# 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

運行啓動Elasticsearch/home/elasticsearch5.6.9/bin/elasticsearch.sh

部署collector

下載release的版本https://github.com/OpenSkywalking/skywalking-netcore/releases

 

下載

將壓縮包解壓到Server-01/home/collector/目錄

  • 修改/home/collector/bin/webappService.sh中的127.0.0.1Server-01的IP地址,目前爲192.168.10.191

最終腳本以下:

  1.  
    # 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 shPRG="$0"PRGDIR=`dirname "$PRG"`
  2.  
    [ -z "$WEBAPP_HOME" ] && WEBAPP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`
  3.  
     
  4.  
    WEBAPP_LOG_DIR= "${WEBAPP_HOME}/logs"JAVA_OPTS=" -Xms256M -Xmx512M"JAR_PATH="${WEBAPP_HOME}/webapp"if [ ! -d "${WEBAPP_HOME}/logs" ]; then
  5.  
       mkdir -p "${WEBAPP_LOG_DIR}"fi_RUNJAVA=${JAVA_HOME}/bin/java
  6.  
    [ -z "$JAVA_HOME" ] && _RUNJAVA=javaeval exec "\"$_RUNJAVA\" ${JAVA_OPTS} -jar ${JAR_PATH}/skywalking-webapp.jar \
  7.  
            --server.port=8080 --collector.ribbon.listOfServers=192.168.10.191:10800 \
  8.  
           2>${WEBAPP_LOG_DIR}/webapp.log 1> /dev/null &"if [ $? -eq 0 ]; then
  9.  
       sleep 1     echo "Skywalking Web Application started successfully!"else
  10.  
        echo "Skywalking Web Application started failure!"
  11.  
        exit 1fi
  • 修改/home/collector/config/application.yml中的全部localhostServer-01的IP地址,目前爲192.168.10.191

最終配置文件以下:

  1.  
    # 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:
  2.  
     jetty:
  3.  
       host: 192.168.10.191
  4.  
       port: 10800
  5.  
       contextPath: /
  6.  
    cache:#  guava:
  7.  
     caffeine:
  8.  
    remote:
  9.  
     gRPC:
  10.  
       host: 192.168.10.191
  11.  
       port: 11800agent_gRPC:
  12.  
     gRPC:
  13.  
       host: 192.168.10.191
  14.  
       port: 11800
  15.  
       #Set these two setting to open ssl
  16.  
       #sslCertChainFile: $path
  17.  
       #sslPrivateKeyFile: $path
  18.  
     
  19.  
       #Set your own token to active auth
  20.  
       #authentication: xxxxxxagent_jetty:
  21.  
     jetty:
  22.  
       host: 192.168.10.191
  23.  
       port: 12800
  24.  
       contextPath: /
  25.  
    analysis_register:  default:
  26.  
    analysis_jvm:  default:
  27.  
    analysis_segment_parser:  default:
  28.  
       bufferFilePath: ../buffer/
  29.  
       bufferOffsetMaxFileSize: 10M
  30.  
       bufferSegmentMaxFileSize: 500M
  31.  
    ui:
  32.  
     jetty:
  33.  
       host: 192.168.10.191
  34.  
       port: 12800
  35.  
       contextPath: /
  36.  
    storage:
  37.  
     elasticsearch:
  38.  
       clusterName: CollectorDBCluster
  39.  
       clusterTransportSniffer: true
  40.  
       clusterNodes: localhost:9300
  41.  
       indexShardsNumber: 2
  42.  
       indexReplicasNumber: 0
  43.  
       highPerformanceMode: true
  44.  
       ttl: 7

    #storage:#  h2:
    #    url: jdbc:h2:~/memorydb
    #    userName: saconfiguration:  default
    :#     namespace: xxxxx
  45.  
       applicationApdexThreshold: 2000
  46.  
       serviceErrorRateThreshold: 10.00
  47.  
       serviceAverageResponseTimeThreshold: 2000
  48.  
       instanceErrorRateThreshold: 10.00
  49.  
       instanceAverageResponseTimeThreshold: 2000
  50.  
       applicationErrorRateThreshold: 10.00
  51.  
       applicationAverageResponseTimeThreshold: 2000

運行/home/collector/bin/startup.sh啓動WebUI和Collector。

建立WebApplication

接下來,咱們建立一個Web應用,並在其中加入Skywalking for dotnet core的代理程序。

一、打開VS2017,建立一個.Net Core MVC應用,請跟着我操做:VS中點擊菜單「文件」——「新建」——「項目」(按Ctrl+Shift+N同等效果),在彈出的對話框中從左到右進行以下操做。

 

640?wx_fmt=png

.Net Core Web應用

640?wx_fmt=png

選擇項目類型

二、等項目建好,右鍵點擊「依賴項」——「管理Nuget程序包」,打開Nuget管理器。

640?wx_fmt=png

操做

在「瀏覽」選項卡,輸入「Skywalking.AspNetCore」搜索,並安裝

640?wx_fmt=png

找到包

安裝前須要接受許可證,點擊「我接受」(不接受請關閉本文)

 

640?wx_fmt=png

許可證

三、安裝完成後,打開Startup.cs,在ConfigureServices函數中加入

  1.  
               services.AddSkyWalking(option =>
  2.  
               {                //這裏填本應用的名稱,每一個應用不一樣
  3.  
                   option.ApplicationCode = "OurApplication1";              
                //這裏填Collector的地址
  4.  
                   option.DirectServers = "192.168.10.191:11800";
  5.  
     
  6.  
               });

Startup.cs完整代碼以下

  1.  
    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
  2.  
    {    public class Startup
  3.  
       {
  4.  
           public Startup(IConfiguration configuration)
  5.  
           {
  6.  
               Configuration = configuration;
  7.  
           }        public IConfiguration Configuration { get; }      
     // This method gets called by the runtime. Use this method to add services to the container.
  8.  
           public void ConfigureServices(IServiceCollection services)
  9.  
           {
  10.  
               services.AddSkyWalking(option =>
  11.  
               {                //這裏填本應用的名稱,每一個應用不一樣
  12.  
                   option.ApplicationCode = "OurApplication1";            
        //這裏填Collector的地址
  13.  
                   option.DirectServers = "192.168.10.191:11800";
  14.  
     
  15.  
               });
  16.  
     
  17.  
               services.AddMvc();
  18.  
     
  19.  
           }        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  20.  
           public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  21.  
           {            if (env.IsDevelopment())
  22.  
               {
  23.  
                   app.UseDeveloperExceptionPage();
  24.  
                   app.UseBrowserLink();
  25.  
               }            else
  26.  
               {
  27.  
                   app.UseExceptionHandler("/Home/Error");
  28.  
               }
  29.  
     
  30.  
               app.UseStaticFiles();
  31.  
     
  32.  
               app.UseMvc(routes =>
  33.  
               {
  34.  
                   routes.MapRoute(
  35.  
                       name: "default",                  
          template: "{controller=Home}/{action=Index}/{id?}");
  36.  
               });
  37.  
           }
  38.  
       }
  39.  
    }

打開Program.cs文件,容許非本地訪問,最終代碼以下

  1.  
    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
  2.  
    {    public class Program
  3.  
       {
  4.  
           public static void Main(string[] args)
  5.  
           {
  6.  
               BuildWebHost(args).Run();
  7.  
           }        public static IWebHost BuildWebHost(string[] args) =>
  8.  
               WebHost.CreateDefaultBuilder(args)
  9.  
                   .UseUrls("http://*:5000")
  10.  
                   .UseStartup<Startup>()
  11.  
                   .Build();
  12.  
       }
  13.  
    }

四、發佈:點擊咱們的WEB應用,右鍵——發佈,接下來看圖操做。

640?wx_fmt=png

發佈

640?wx_fmt=png

發佈到文件夾

640?wx_fmt=png

選擇目標位置

640?wx_fmt=png

發佈

640?wx_fmt=png

沒有失敗,恭喜,大吉大利,今晚吃雞

咱們能夠看到,發佈完成後的文件,一大堆,咱們把這個文件夾更名爲App1

640?wx_fmt=png

發佈成功

五、接下來,咱們在StartUp.cs中把ApplicationCode改一下,按上面的步驟再發布一次。

640?wx_fmt=png

image.png

此次發佈生成的Publish3文件夾,咱們更名爲App2

佈署Web

一、打開WinSCP,連上Web01,將App1拖到/home下。
二、打開XShell,連上Web01,執行以下命令

# 關掉防火牆(生產環境不宜)systemctl stop firewalld
# 給予執行權限chmod 777 /home/App1/WebApplication2.dll
# 在後臺運行應用nohup dotnet /home/App1/WebApplication2.dll &
  1. 一樣的操做,將App2佈署到Web02上。

訪問應用

咱們打開兩個應用,隨機點擊一下應用頂部的連接。

http://192.168.10.192:5000,這是咱們的app1
http://192.168.10.193:5000,這是咱們的app2

查看監測

打開http://192.168.10.191:8080,查看監測狀況

640?wx_fmt=png

監測到的應用

查看被訪問的服務響應速度

640?wx_fmt=png

服務狀態

結語

Skywalking-netcore作爲.Net core社區的新生命,具備無限的潛力,據可靠消息,新版本將更深刻地監測數據庫操做及狀態,HTTP請求消息以及StackTrace跟蹤,讓咱們一塊兒爲它打Call加油吧。猛戮這裏,到Github給Skywalking-netcore點星星。 :)

原文地址: https://www.jianshu.com/p/3ddd986c7581

相關文章
相關標籤/搜索