利用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

相关文章
相关标签/搜索