欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NetCore使用skywalking实现实时性能监控

程序员文章站 2022-07-11 11:23:11
一、简介 很久之前写了一篇 《.Net Core 2.0+ InfluxDB+Grafana+App Metrics 实现跨平台的实时性能监控》关于NetCore性能监控的文章,使用Influxdb+AppMetrics进行项目性能监控,由于技术有限,在正式环境使用一段时间后,莫名的AppMetri ......

一、简介

很久之前写了一篇 《.net core 2.0+ influxdb+grafana+app metrics 实现跨平台的实时性能监控》关于netcore性能监控的文章,使用influxdb+appmetrics进行项目性能监控,由于技术有限,在正式环境使用一段时间后,莫名的appmetrics就没办法往influxdb中插入数据了,后来我也在app metrics作者的github上留言了,并且作者也根据我阐述的情况做了测试,没有复现我的问题,最后这个问题就不了了知了,然后项目性能监控这个事搁置了一段时间,直到2018年参加上海.net线下技术沙龙,在会场首次听到skywalking,那时候skywalking正在做netcore的支持,会后回到公司便开始关注skywalking,知道skywalking支持netcore后,第一时间在公司的项目中运用了skywalking。

二、安装环境

要想使用skywalking,首先得安装相关环境。本文以windows为例。

1、安装java sdk(如果不会配置java环境的话,请参考百度百科:)

2、java环境安装完成后,下载elasticsearch进行安装 https://www.elastic.co/downloads/elasticsearch (本文使用skywalking 6.x版本,6.x版本对应使用es 6.x版本,请自行下载对应版本)

3、下载完elasticsearch 后将elasticsearch解压到安装位置,以我电脑为例,我安装在d:\program files

4、修改es配置,进入es文件下的:\config,找到elasticsearch.yml,打开后修改如下配置:

.NetCore使用skywalking实现实时性能监控
 1 # ======================== elasticsearch configuration =========================
 2 #
 3 # note: elasticsearch comes with reasonable defaults for most settings.
 4 #       before you set out to tweak and tune the configuration, make sure you
 5 #       understand what are you trying to accomplish and the consequences.
 6 #
 7 # the primary way of configuring a node is via this file. this template lists
 8 # the most important settings you may want to configure for a production cluster.
 9 #
10 # please consult the documentation for further information on configuration options:
11 # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
12 #
13 # ---------------------------------- cluster -----------------------------------
14 #
15 # use a descriptive name for your cluster:
16 #
17 cluster.name: myskywalking
18 #
19 # ------------------------------------ node ------------------------------------
20 #
21 # use a descriptive name for the node:
22 #
23 node.name: node-1
24 #
25 # add custom attributes to the node:
26 #
27 #node.attr.rack: r1
28 #
29 # ----------------------------------- paths ------------------------------------
30 #
31 # path to directory where to store the data (separate multiple locations by comma):
32 #
33 path.data: d:/program files/elasticsearch-6.6.2/path/to/data
34 #
35 # path to log files:
36 #
37 path.logs: d:/program files/elasticsearch-6.6.2/path/to/logs
38 #
39 # ----------------------------------- memory -----------------------------------
40 #
41 # lock the memory on startup:
42 #
43 bootstrap.memory_lock: false
44 #
45 # make sure that the heap size is set to about half the memory available
46 # on the system and that the owner of the process is allowed to use this
47 # limit.
48 #
49 # elasticsearch performs poorly when the system is swapping the memory.
50 #
51 # ---------------------------------- network -----------------------------------
52 #
53 # set the bind address to a specific ip (ipv4 or ipv6):
54 #
55 network.host: 0.0.0.0
56 http.port: 9200
57 http.cors.enabled: true 
58 http.cors.allow-origin: "*" 
59 http.cors.allow-methods: options,head,get,post,put,delete
60 http.cors.allow-headers: "x-requested-with, content-type, content-length, x-users"
61 
62 #
63 # for more information, consult the network module documentation.
64 #
65 # --------------------------------- discovery ----------------------------------
66 #
67 # pass an initial list of hosts to perform discovery when new node is started:
68 # the default list of hosts is ["127.0.0.1", "[::1]"]
69 #
70 #discovery.zen.ping.unicast.hosts: ["host1", "host2"]
71 #
72 # prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
73 #
74 #discovery.zen.minimum_master_nodes: 
75 #
76 # for more information, consult the zen discovery module documentation.
77 #
78 # ---------------------------------- gateway -----------------------------------
79 #
80 # block initial recovery after a full cluster restart until n nodes are started:
81 #
82 #gateway.recover_after_nodes: 3
83 #
84 # for more information, consult the gateway module documentation.
85 #
86 # ---------------------------------- various -----------------------------------
87 #
88 # require explicit names when deleting indices:
89 #
90 #action.destructive_requires_name: true
view code

修改好elasticsearch.yml文件后,打开cmd命令,进入到d:\program files\elasticsearch-6.6.2\bin,bin文件夹下,输入如下命令:  elasticsearch-service.bat install  将es安装成windows,这样就可以方便系统重启后自动启动

然后将服务启动后即可

5、接下来下载skywalking,

选择版本为 :6.0.0-ga 的下载

三、配置和效果

1、在本地电脑中创建一个文件夹(注意:本人亲自躺过的坑,skywalking服务必须放在无空格的文件夹,比如:program files这个文件是绝对不能放的,不然服务运行的时候只会一闪而过,连log日志都不会生成,切记!切记!切记!

我在d盘下创建了一个叫skyworkingservice文件,路径如下:d:\skyworkingservice

将下好的skywalking解压到该目录下,命名为skywalking-apm-ga,路径如下:d:\skyworkingservice\skywalking-apm-ga

接着,打开config文件,找到application.yml文件,修改其配置如下:

.NetCore使用skywalking实现实时性能监控
 1 # licensed to the apache software foundation (asf) under one
 2 # or more contributor license agreements.  see the notice file
 3 # distributed with this work for additional information
 4 # regarding copyright ownership.  the asf licenses this file
 5 # to you under the apache license, version 2.0 (the
 6 # "license"); you may not use this file except in compliance
 7 # with the license.  you may obtain a copy of the license at
 8 #
 9 #     http://www.apache.org/licenses/license-2.0
10 #
11 # unless required by applicable law or agreed to in writing, software
12 # distributed under the license is distributed on an "as is" basis,
13 # without warranties or conditions of any kind, either express or implied.
14 # see the license for the specific language governing permissions and
15 # limitations under the license.
16 
17 cluster:
18   standalone:
19   # please check your zookeeper is 3.5+, however, it is also compatible with zookeeper 3.4.x. replace the zookeeper 3.5+
20   # library the oap-libs folder with your zookeeper 3.4.x library.
21 #  zookeeper:
22 #    namespace: ${sw_namespace:""}
23 #    hostport: ${sw_cluster_zk_host_port:localhost:2181}
24 #    #retry policy
25 #    basesleeptimems: ${sw_cluster_zk_sleep_time:1000} # initial amount of time to wait between retries
26 #    maxretries: ${sw_cluster_zk_max_retries:3} # max number of times to retry
27 #  kubernetes:
28 #    watchtimeoutseconds: ${sw_cluster_k8s_watch_timeout:60}
29 #    namespace: ${sw_cluster_k8s_namespace:default}
30 #    labelselector: ${sw_cluster_k8s_label:app=collector,release=skywalking}
31 #    uidenvname: ${sw_cluster_k8s_uid:skywalking_collector_uid}
32 #  consul:
33 #    servicename: ${sw_service_name:"skywalking_oap_cluster"}
34 #     consul cluster nodes, example: 10.0.0.1:8500,10.0.0.2:8500,10.0.0.3:8500
35 #    hostport: ${sw_cluster_consul_host_port:localhost:8500}
36 core:
37   default:
38     resthost: ${sw_core_rest_host:0.0.0.0}
39     restport: ${sw_core_rest_port:12800}
40     restcontextpath: ${sw_core_rest_context_path:/}
41     grpchost: ${sw_core_grpc_host:0.0.0.0}
42     grpcport: ${sw_core_grpc_port:11800}
43     downsampling:
44     - hour
45     - day
46     - month
47     # set a timeout on metric data. after the timeout has expired, the metric data will automatically be deleted.
48     recorddatattl: ${sw_core_record_data_ttl:90} # unit is minute
49     minutemetricsdatattl: ${sw_core_minute_metric_data_ttl:90} # unit is minute
50     hourmetricsdatattl: ${sw_core_hour_metric_data_ttl:36} # unit is hour
51     daymetricsdatattl: ${sw_core_day_metric_data_ttl:45} # unit is day
52     monthmetricsdatattl: ${sw_core_month_metric_data_ttl:18} # unit is month
53 storage:
54   # h2:
55     # driver: ${sw_storage_h2_driver:org.h2.jdbcx.jdbcdatasource}
56     # url: ${sw_storage_h2_url:jdbc:h2:mem:skywalking-oap-db}
57     # user: ${sw_storage_h2_user:sa}
58  elasticsearch:
59    namespace: ${sw_namespace:"myskywalking"}
60    clusternodes: ${sw_storage_es_cluster_nodes:localhost:9200}
61    indexshardsnumber: ${sw_storage_es_index_shards_number:2}
62    indexreplicasnumber: ${sw_storage_es_index_replicas_number:0}
63    # batch process setting, refer to https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-docs-bulk-processor.html
64    bulkactions: ${sw_storage_es_bulk_actions:2000} # execute the bulk every 2000 requests
65    bulksize: ${sw_storage_es_bulk_size:20} # flush the bulk every 20mb
66    flushinterval: ${sw_storage_es_flush_interval:10} # flush the bulk every 10 seconds whatever the number of requests
67    concurrentrequests: ${sw_storage_es_concurrent_requests:2} # the number of concurrent requests
68 receiver-register:
69   default:
70 receiver-trace:
71   default:
72     bufferpath: ${sw_receiver_buffer_path:../trace-buffer/}  # path to trace buffer files, suggest to use absolute path
73     bufferoffsetmaxfilesize: ${sw_receiver_buffer_offset_max_file_size:100} # unit is mb
74     bufferdatamaxfilesize: ${sw_receiver_buffer_data_max_file_size:500} # unit is mb
75     bufferfilecleanwhenrestart: ${sw_receiver_buffer_file_clean_when_restart:false}
76     samplerate: ${sw_trace_sample_rate:10000} # the sample rate precision is 1/10000. 10000 means 100% sample in default.
77 receiver-jvm:
78   default:
79 #service-mesh:
80 #  default:
81 #    bufferpath: ${sw_service_mesh_buffer_path:../mesh-buffer/}  # path to trace buffer files, suggest to use absolute path
82 #    bufferoffsetmaxfilesize: ${sw_service_mesh_offset_max_file_size:100} # unit is mb
83 #    bufferdatamaxfilesize: ${sw_service_mesh_buffer_data_max_file_size:500} # unit is mb
84 #    bufferfilecleanwhenrestart: ${sw_service_mesh_buffer_file_clean_when_restart:false}
85 #istio-telemetry:
86 #  default:
87 #receiver_zipkin:
88 #  default:
89 #    host: ${sw_receiver_zipkin_host:0.0.0.0}
90 #    port: ${sw_receiver_zipkin_port:9411}
91 #    contextpath: ${sw_receiver_zipkin_context_path:/}
92 query:
93   graphql:
94     path: ${sw_query_graphql_path:/graphql}
95 alarm:
96   default:
97 telemetry:
98   none:
view code

 修改完成后,进入到bin文件中,右键单击startup.bat,以管理员权限运行,即可看到如下弹框

.NetCore使用skywalking实现实时性能监控

弹出这两个框说明服务已经启动了

这个时候访问http://localhost:8080,即可看到如下界面:

.NetCore使用skywalking实现实时性能监控

默认账号admin,密码admin,登录后看看到想要的监控数据和各服务直接的拓扑图,因为我的服务跑了一段时间,所以下面的界面是有数据的:

.NetCore使用skywalking实现实时性能监控

.NetCore使用skywalking实现实时性能监控

2、由于启动skywalking后会弹出两个命令窗口,所以如果运维人员不小心关了窗口的话服务自然就停掉了,所以为了避免这种问题,我们还可以将bin文件夹下的oapservice.bat和webappservice.bat进行配置,如下:

.NetCore使用skywalking实现实时性能监控
 1 @rem
 2 @rem  licensed to the apache software foundation (asf) under one or more
 3 @rem  contributor license agreements.  see the notice file distributed with
 4 @rem  this work for additional information regarding copyright ownership.
 5 @rem  the asf licenses this file to you under the apache license, version 2.0
 6 @rem  (the "license"); you may not use this file except in compliance with
 7 @rem  the license.  you may obtain a copy of the license at
 8 @rem
 9 @rem      http://www.apache.org/licenses/license-2.0
10 @rem
11 @rem  unless required by applicable law or agreed to in writing, software
12 @rem  distributed under the license is distributed on an "as is" basis,
13 @rem  without warranties or conditions of any kind, either express or implied.
14 @rem  see the license for the specific language governing permissions and
15 @rem  limitations under the license.
16 
17 @echo off
18 
19 setlocal
20 set oap_process_title=skywalking-collector
21 set oap_home=%~dp0%..
22 set oap_opts="-xms256m -xmx512m -doap.logdir=%oap_home%\logs"
23 
24 set classpath=%oap_home%\config;.;
25 set classpath=%oap_home%\oap-libs\*;%classpath%
26 
27 if defined java_home (
28  set _execjava="%java_home%\bin\javaw"
29 )
30 
31 if not defined java_home (
32  echo "java_home not set."
33  set _execjava=javaw
34 )
35 
36 start "%oap_process_title%" %_execjava% "%oap_opts%" -cp "%classpath%" org.apache.skywalking.oap.server.starter.oapserverstartup
37 endlocal
oapservice.bat
.NetCore使用skywalking实现实时性能监控
 1 @rem
 2 @rem  licensed to the apache software foundation (asf) under one or more
 3 @rem  contributor license agreements.  see the notice file distributed with
 4 @rem  this work for additional information regarding copyright ownership.
 5 @rem  the asf licenses this file to you under the apache license, version 2.0
 6 @rem  (the "license"); you may not use this file except in compliance with
 7 @rem  the license.  you may obtain a copy of the license at
 8 @rem
 9 @rem      http://www.apache.org/licenses/license-2.0
10 @rem
11 @rem  unless required by applicable law or agreed to in writing, software
12 @rem  distributed under the license is distributed on an "as is" basis,
13 @rem  without warranties or conditions of any kind, either express or implied.
14 @rem  see the license for the specific language governing permissions and
15 @rem  limitations under the license.
16 
17 @echo off
18 
19 setlocal
20 set webapp_process_title=skywalking-webapp
21 set webapp_home=%~dp0%..
22 set jarpath=%webapp_home%\webapp
23 set webapp_log_dir=%webapp_home%\logs
24 
25 if exist "%webapp_log_dir%" (
26     mkdir "%webapp_log_dir%"
27 )
28 
29 set log_file_location=%webapp_log_dir%\webapp.log
30 
31 if defined java_home (
32  set _execjava="%java_home%\bin\javaw"
33 )
34 
35 if not defined java_home (
36  echo "java_home not set."
37  set _execjava=javaw
38 )
39 
40 start "%webapp_process_title%" %_execjava%  -jar %jarpath%/skywalking-webapp.jar --spring.config.location=%jarpath%/webapp.yml --logging.file=%log_file_location%
41 endlocal
webappservice.bat

其实只是将文件里的java改成了javaw,这样就可以在后台运行了,保存后再次运行startup.bat文件,这个时候界面上会有个cmd命令界面一闪而过,不要慌,我们打开资源管理器看看,会发现进程中多了两个名为“javaw.exe”的进程

这个时候访问:http://localhost:8080 一样可以看到上面的ui界面!

至此,skywalking的所有环境皆搭建完毕,接下来,在我们项目中添加skywalking的探针,方便skywalking收集我们项目中的数据

四、项目引用skywalking探针

新建一个netcore的webapi,然后在引用中引用skywalking.aspnetcore,如图:

.NetCore使用skywalking实现实时性能监控

项目引用后,在startup.cs中注入skywalking。

在头部引用:using skywalking.aspnetcore;

然后找到public void configureservices(iservicecollection services)下输入一下代码即可:

.NetCore使用skywalking实现实时性能监控
1 services.addskywalking(option =>
2 {
3       option.applicationcode = "areaserver";
4       option.directservers = 127.0.0.1:11800;
5  });
view code

运行代码后,控制台内每隔几秒就会有以下信息输出

.NetCore使用skywalking实现实时性能监控

证明skywalking探针已经成功,接下来请求一下你的接口,然后进入skywalking的ui中看看你的成果吧!