400ToJava:125-SkyWalking分布式調(diào)用鏈監(jiān)控介紹

一、介紹

SkyWalking是一種APM(應(yīng)用性能管理)工具,通過(guò)鏈路追蹤對(duì)分布式程序進(jìn)行性能管理,下面對(duì)鏈路追蹤及SkyWalking進(jìn)行簡(jiǎn)要介紹

1.1 技術(shù)介紹

1.1.1 鏈路追蹤

微服務(wù)架構(gòu)是通過(guò)業(yè)務(wù)來(lái)劃分服務(wù)的,對(duì)外暴露的一個(gè)接口可以使用可能需要很多個(gè)服務(wù)協(xié)同才能完成這個(gè)接口功能,如果鏈路上任何一個(gè)服務(wù)出現(xiàn)問(wèn)題或者網(wǎng)絡(luò)超時(shí),都會(huì)形成導(dǎo)致接口調(diào)用失敗。隨著業(yè)務(wù)的不斷擴(kuò)張,服務(wù)之間互相調(diào)用會(huì)越來(lái)越復(fù)雜,此時(shí)當(dāng)分布式項(xiàng)目的一個(gè)部分出現(xiàn)錯(cuò)誤時(shí),定位它就變得十分困難,這時(shí)我們就需要一個(gè)可以幫助理解系統(tǒng)行為、用于分析性能問(wèn)題的工具,以便發(fā)生故障的時(shí)候,能夠快速定位和解決問(wèn)題,這就是所謂的 APM(應(yīng)用性能管理)。

1.1.2 SkyWalking簡(jiǎn)介

SkyWalking是一個(gè)觀察性分析平臺(tái)和應(yīng)用性能管理系統(tǒng)。提供分布式追蹤、服務(wù)網(wǎng)格遙測(cè)分析、度量聚合和可視化一體化解決方案。具有如下幾個(gè)特性:

  • 多種監(jiān)控手段,語(yǔ)言探針和服務(wù)網(wǎng)格(Service Mesh)
  • 多語(yǔ)言自動(dòng)探針,Java,.NET Core 和 Node.JS
  • 輕量高效,不需要大數(shù)據(jù)
  • 模塊化,UI、存儲(chǔ)、集群管理多種機(jī)制可選
  • 支持告警
  • 優(yōu)秀的可視化方案
  • 國(guó)人開(kāi)源,Apache頂級(jí)項(xiàng)目,中文文檔支持優(yōu)秀

1.2 項(xiàng)目地址

官方網(wǎng)站(有中文):http://skywalking.apache.org/
GitHub地址:https://github.com/apache/incubator-skywalking

二、環(huán)境搭建

2.1 安裝

選擇6.1.0版本,下載地址,下載壓縮包。

圖-1

解壓后可直接啟動(dòng)

2.2 準(zhǔn)備

默認(rèn)使用h2數(shù)據(jù)庫(kù),數(shù)據(jù)存儲(chǔ)及使用效率不高,本例使用ElasticSearch進(jìn)行數(shù)據(jù)存儲(chǔ),啟動(dòng)ElasticSearch,啟動(dòng)及配置可參考ElasticSearch介紹,啟動(dòng)后訪問(wèn)http://localhost:9200,啟動(dòng)成功。

圖-2

2.3 配置

2.3.1 配置數(shù)據(jù)源為ElasticSearch

打開(kāi)config目錄下的application.yml文件,修改數(shù)據(jù)源相關(guān)配置

storage:
  selector: ${SW_STORAGE:elasticsearch}
  elasticsearch:
    nameSpace: ${SW_NAMESPACE:""}
    clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:localhost:9200}
    protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"}
    trustStorePath: ${SW_SW_STORAGE_ES_SSL_JKS_PATH:"../es_keystore.jks"}
    trustStorePass: ${SW_SW_STORAGE_ES_SSL_JKS_PASS:""}
    user: ${SW_ES_USER:""}
    password: ${SW_ES_PASSWORD:""}
    secretsManagementFile: ${SW_ES_SECRETS_MANAGEMENT_FILE:""} # Secrets management file in the properties format includes the username, password, which are managed by 3rd party tool.
    enablePackedDownsampling: ${SW_STORAGE_ENABLE_PACKED_DOWNSAMPLING:true} # Hour and Day metrics will be merged into minute index.
    dayStep: ${SW_STORAGE_DAY_STEP:1} # Represent the number of days in the one minute/hour/day index.
    indexShardsNumber: ${SW_STORAGE_ES_INDEX_SHARDS_NUMBER:2}
    indexReplicasNumber: ${SW_STORAGE_ES_INDEX_REPLICAS_NUMBER:0}
    # Those data TTL settings will override the same settings in core module.
    recordDataTTL: ${SW_STORAGE_ES_RECORD_DATA_TTL:7} # Unit is day
    otherMetricsDataTTL: ${SW_STORAGE_ES_OTHER_METRIC_DATA_TTL:45} # Unit is day
    monthMetricsDataTTL: ${SW_STORAGE_ES_MONTH_METRIC_DATA_TTL:18} # Unit is month
    # Batch process setting, refer to https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/java-docs-bulk-processor.html
    bulkActions: ${SW_STORAGE_ES_BULK_ACTIONS:1000} # Execute the bulk every 1000 requests
    flushInterval: ${SW_STORAGE_ES_FLUSH_INTERVAL:10} # flush the bulk every 10 seconds whatever the number of requests
    concurrentRequests: ${SW_STORAGE_ES_CONCURRENT_REQUESTS:2} # the number of concurrent requests
    resultWindowMaxSize: ${SW_STORAGE_ES_QUERY_MAX_WINDOW_SIZE:10000}
    metadataQueryMaxSize: ${SW_STORAGE_ES_QUERY_MAX_SIZE:5000}
    segmentQueryMaxSize: ${SW_STORAGE_ES_QUERY_SEGMENT_SIZE:200}
    profileTaskQueryMaxSize: ${SW_STORAGE_ES_QUERY_PROFILE_TASK_SIZE:200}
    advanced: ${SW_STORAGE_ES_ADVANCED:""}

其中selector: ${SW_STORAGE:elasticsearch}默認(rèn)為h2clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:localhost:9200}可修改ES地址。

2.3.2 配置UI界面

打開(kāi)webapp目錄下的webapp.yml文件,默認(rèn)啟動(dòng)端口為8080,修改為想要使用的端口,本例使用了9099端口

server:
  port: 9099

collector:
  path: /graphql
  ribbon:
    ReadTimeout: 10000
    # Point to all backend's restHost:restPort, split by ,
    listOfServers: 127.0.0.1:12800

2.4 啟動(dòng)

打開(kāi)bin目錄下的,的startup.bat(如果是Linux系統(tǒng)則打開(kāi)startup.sh),系統(tǒng)自動(dòng)啟動(dòng)服務(wù)及UI界面

圖-3

啟動(dòng)日志可在logs目錄下進(jìn)行查看。

2020-08-31 10:02:25.645  WARN 20356 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with key 'routesEndpoint' has been registered as an MBean but has no exposed attributes or operations
2020-08-31 10:02:25.645  INFO 20356 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'routesMvcEndpoint': registering with JMX server as MBean [org.springframework.cloud.netflix.zuul:name=routesMvcEndpoint,type=RoutesMvcEndpoint]
2020-08-31 10:02:25.659  INFO 20356 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'filtersEndpoint': registering with JMX server as MBean [org.springframework.cloud.netflix.zuul:name=filtersEndpoint,type=FiltersEndpoint]
2020-08-31 10:02:25.663  WARN 20356 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with key 'filtersEndpoint' has been registered as an MBean but has no exposed attributes or operations
2020-08-31 10:02:25.663  INFO 20356 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]
2020-08-31 10:02:25.687  INFO 20356 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=4566e5bd,type=ConfigurationPropertiesRebinder]
2020-08-31 10:02:25.695  INFO 20356 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'refreshEndpoint': registering with JMX server as MBean [org.springframework.cloud.endpoint:name=refreshEndpoint,type=RefreshEndpoint]
2020-08-31 10:02:25.745  INFO 20356 --- [main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2020-08-31 10:02:25.848  INFO 20356 --- [main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2020-08-31 10:02:25.862  INFO 20356 --- [main] ration$HystrixMetricsPollerConfiguration : Starting poller
2020-08-31 10:02:25.986  INFO 20356 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9099 (http)

打開(kāi)UI界面地址http://localhost:9099,啟動(dòng)成功

圖-4

三、Demo開(kāi)發(fā)演示

本Demo主要是對(duì)SkyWalking監(jiān)控服務(wù)進(jìn)行簡(jiǎn)單介紹。

3.1 Demo概述

3.1.1 Demo功能

主要演示SkyWalking的功能,Demo比較簡(jiǎn)單,主要通過(guò)兩個(gè)get請(qǐng)求對(duì)外輸出兩個(gè)字符串,但是第二個(gè)get請(qǐng)求在返回字符串時(shí)先進(jìn)行了一個(gè)“除0”操作,顯然該請(qǐng)求會(huì)報(bào)錯(cuò),下面給出該服務(wù)控制層的相關(guān)代碼。

package cn.toj.agenttest.controller;

import cn.toj.agenttest.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Carlos
 * @description
 * @Date 2020/8/24
 */

@RestController
public class AgentTestController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/getConfig")
    public ResponseResult<String> getConfig() {
        return new ResponseResult<>(Integer.valueOf(HttpStatus.OK.value()), HttpStatus.OK.toString(), "Hello, I'm in port " + port + ".");
    }

    @GetMapping("/exception")
    public ResponseResult<String> getException() {
        int i = 1/0;
        return new ResponseResult<>(Integer.valueOf(HttpStatus.OK.value()), HttpStatus.OK.toString(), "Hello, I'm in port " + port + ".");
    }

}

3.1.2 添加程序探針agent

將SkyWalking根目錄下的agent文件夾復(fù)制到指定位置,并打開(kāi)該文件夾下config目錄下的agent.config文件,修改agent.service_name=${SW_AGENT_NAME:服務(wù)名}屬性,本例命名為agent-test
啟動(dòng)時(shí)需要添加探針地址,使用IDEA等IDE工具時(shí),需要添加啟動(dòng)參數(shù),下面以IDEA為例。
打開(kāi)啟動(dòng)配置界面

圖-5

在Enviornment中添加agent的jar文件地址,使SkyWalking可以對(duì)該服務(wù)進(jìn)行監(jiān)控。
圖-6

保存并啟動(dòng)該服務(wù)。

3.2 調(diào)用鏈演示

分別在瀏覽器訪問(wèn)http://localhost:9011/getConfighttp://localhost:9011/getException,第一個(gè)返回正確的數(shù)據(jù),第二個(gè)報(bào)錯(cuò),查看SkyWalking的UI界面,可以看到服務(wù)的兩個(gè)訪問(wèn)情況

圖-7

點(diǎn)擊追蹤選項(xiàng)卡,可以看到第一個(gè)請(qǐng)求成功訪問(wèn),第二個(gè)請(qǐng)求訪問(wèn)失敗。
圖-8

3.3 Demo下載地址

  • 使用Git下載項(xiàng)目的命令:
git clone https://github.com/diyzhang/42j125-swdemo.git
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。