
1. 概念與架構
SkyWalking是一個開源監控平臺,用于從服務和云原生基礎設施收集、分析、聚合和可視化數據。SkyWalking提供了一種簡單的方法來維護分布式系統的清晰視圖,甚至可以跨云查看。它是一種現代APM,專門為云原生、基于容器的分布式系統設計。
SkyWalking從三個維度對應用進行監視:service(服務), service instance(實例), endpoint(端點)
【資料圖】
服務和實例就不多說了,端點是服務中的某個路徑或者說URI
SkyWalking allows users to understand the topology relationship between Services and Endpoints, to view the metrics of every Service/Service Instance/Endpoint and to set alarm rules.
SkyWalking允許用戶了解服務和端點之間的拓撲關系,查看每個服務/服務實例/端點的度量,并設置警報規則。
1.1. 架構
SkyWalking邏輯上分為四個部分:Probes(探針), Platform backend(平臺后端), Storage(存儲), UI
這個結構就很清晰了,探針就是Agent負責采集數據并上報給服務端,服務端對數據進行處理和存儲,UI負責展示
2. 下載與安裝
SkyWalking有兩中版本,ES版本和非ES版。如果我們決定采用ElasticSearch作為存儲,那么就下載es版本。
??https://skywalking.apache.org/downloads/??
???https://archive.apache.org/dist/skywalking/???
agent目錄將來要拷貝到各服務所在機器上用作探針bin目錄是服務啟動腳本config目錄是配置文件oap-libs目錄是oap服務運行所需的jar包webapp目錄是web服務運行所需的jar包接下來,要選擇存儲了,支持的存儲有:
H2ElasticSearch 6, 7MySQLTiDBInfluxDB作為監控系統,首先排除H2和MySQL,這里推薦InfluxDB,它本身就是時序數據庫,非常適合這種場景
但是InfluxDB我不是很熟悉,所以這里先用ElasticSearch7
??https://github.com/apache/skywalking/blob/master/docs/en/setup/backend/backend-storage.md??
2.1. 安裝ElasticSearch
???https://www.elastic.co/guide/en/elasticsearch/reference/7.10/targz.html???
?啟動
./bin/elasticsearch-d-ppid
#停止
pkill-Fpid?
ElasticSearch7.x需要Java 11以上的版本,但是如果你設置了環境變量JAVA_HOME的話,它會用你自己的Java版本
通常,啟動過程中會報以下三個錯誤:
[1]:maxfiledescriptors[4096]forelasticsearchprocessistoolow,increasetoatleast[65535]
[2]:maxvirtualmemoryareasvm.max_map_count[65530]istoolow,increasetoatleast[262144]
[3]:thedefaultdiscoverysettingsareunsuitableforproductionuse;atleastoneof[discovery.seed_hosts,discovery.seed_providers,cluster.initial_master_nodes]mustbeconfigured?
解決方法:
在 /etc/security/limits.conf 文件中追加以下內容:
在 /etc/security/limits.conf 文件中追加以下內容:
?*softnofile65536
*hardnofile65536
*softnproc4096
*hardnproc4096
可通過以下四個命令查看修改結果:
ulimit-Hn
ulimit-Sn
ulimit-Hu
ulimit-Su
修改/etc/sysctl.conf 文件,追加以下內容:
vm.max_map_count=262144
修改es配置文件 elasticsearch.yml 取消注釋,保留一個節點
cluster.initial_master_nodes:["node-1"]
為了能夠ip:port方式訪問,還需修改網絡配置
network.host:0.0.0.0?
修改完是這樣的:
至此,ElasticSearch算是啟動成功了
?接下來,在 config/application.yml 中配置es地址即可
storage:
selector:${SW_STORAGE:elasticsearch7}
elasticsearch7:
clusterNodes:${SW_STORAGE_ES_CLUSTER_NODES:192.168.100.19:9200}?
2.2. 安裝Agent
??https://github.com/apache/skywalking/blob/v8.2.0/docs/en/setup/service-agent/java-agent/README.md??
將agent目錄拷貝至各服務所在的機器上
scp-r./agentchengjs@192.168.100.12:~/?
這里,我將它拷貝至各個服務目錄下:
?plugins是探針用到各種插件,SkyWalking插件都是即插即用的,可以把optional-plugins中的插件放到plugins中
修改 agent/config/agent.config 配置文件,也可以通過命令行參數指定
主要是配置服務名稱和后端服務地址
agent.service_name=${SW_AGENT_NAME:user-center}
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:192.168.100.17:11800}
當然,也可以通過環境變量或系統屬性的方式來設置,例如:
exportSW_AGENT_COLLECTOR_BACKEND_SERVICES=127.0.0.1:11800
最后,在服務啟動的時候用命令行參數 -javaagent 來指定探針
java-javaagent:/path/to/skywalking-agent/skywalking-agent.jar-jaryourApp.jar
例如:
java-javaagent:./agent/skywalking-agent.jar-Dspring.profiles.active=dev-Xms512m-Xmx1024m-jardemo-0.0.1-SNAPSHOT.jar?
3. 啟動服務
修改 webapp/webapp.yml 文件,更改端口號及后端服務地址
?server:
port:8080
collector:
path:/graphql
ribbon:
ReadTimeout:10000
#Pointtoallbackend"srestHost:restPort,splitby,
listOfServers:127.0.0.1:12800
啟動服務
bin/startup.sh
或者分別依次啟動
bin/oapService.sh
bin/webappService.sh?
查看logs目錄下的日志文件,看是否啟動成功
?瀏覽器訪問 ????http://127.0.0.1:8080???
4 告警
編輯 alarm-settings.yml 設置告警規則和通知
??https://github.com/apache/skywalking/blob/v8.2.0/docs/en/setup/backend/backend-alarm.md??
重點說下告警通知
?為了使用釘釘機器人通知,接下來,新建一個項目
"1.0"encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.0
com.wt.monitor
skywalking-alarm
1.0.0-SNAPSHOT
skywalking-alarm
1.8
org.springframework.boot
spring-boot-starter-web
com.aliyun
alibaba-dingtalk-service-sdk
1.0.1
commons-codec
commons-codec
1.15
com.alibaba
fastjson
1.2.75
org.projectlombok
lombok
true?
org.springframework.boot
spring-boot-maven-plugin
定義告警消息實體類
?packagecom.wt.monitor.skywalking.alarm.domain;importlombok.Data;importjava.io.Serializable;/***@authorChengJianSheng
*@date2020/12/1*/@DatapublicclassAlarmMessageDTOimplementsSerializable{privateintscopeId;privateStringscope;/***Targetscopeentityname*/
privateStringname;privateStringid0;privateStringid1;privateStringruleName;/***Alarmtextmessage*/
privateStringalarmMessage;/***Alarmtimemeasuredinmilliseconds*/
privatelongstartTime;
}?
發送釘釘機器人消息
?packagecom.wt.monitor.skywalking.alarm.service;importcom.dingtalk.api.DefaultDingTalkClient;importcom.dingtalk.api.DingTalkClient;importcom.dingtalk.api.request.OapiRobotSendRequest;importcom.taobao.api.ApiException;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.codec.binary.Base64;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Service;importjavax.crypto.Mac;importjavax.crypto.spec.SecretKeySpec;importjava.io.UnsupportedEncodingException;importjava.net.URLEncoder;importjava.security.InvalidKeyException;importjava.security.NoSuchAlgorithmException;/***https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
*@author
*@data
@ServicepublicclassDingTalkAlarmService{
@Value("${dingtalk.webhook}")privateStringwebhook;
@Value("${dingtalk.secret}")privateStringsecret;publicvoidsendMessage(Stringcontent){try{
Longtimestamp=System.currentTimeMillis();
StringstringToSign=timestamp+"\n"+secret;
?Macmac=Mac.getInstance("HmacSHA256");
mac.init(newSecretKeySpec(secret.getBytes("UTF-8"),"HmacSHA256"));byte[]signData=mac.doFinal(stringToSign.getBytes("UTF-8"));
Stringsign=URLEncoder.encode(newString(Base64.encodeBase64(signData)),"UTF-8");
StringserverUrl=webhook+"×tamp="+timestamp+"&sign="+sign;
DingTalkClientclient=newDefaultDingTalkClient(serverUrl);
OapiRobotSendRequestrequest=newOapiRobotSendRequest();
request.setMsgtype("text");
OapiRobotSendRequest.Texttext=newOapiRobotSendRequest.Text();
text.setContent(content);
request.setText(text);
client.execute(request);
}catch(ApiExceptione){
e.printStackTrace();
log.error(e.getMessage(),e);
}catch(NoSuchAlgorithmExceptione){
e.printStackTrace();
log.error(e.getMessage(),e);
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
log.error(e.getMessage(),e);
}catch(InvalidKeyExceptione){
e.printStackTrace();
log.error(e.getMessage(),e);
}
}
}
AlarmController.java
packagecom.wt.monitor.skywalking.alarm.controller;importcom.alibaba.fastjson.JSON;importcom.wt.monitor.skywalking.alarm.domain.AlarmMessageDTO;importcom.wt.monitor.skywalking.alarm.service.DingTalkAlarmService;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;importjava.text.MessageFormat;importjava.util.List;/***@authorChengJianSheng
*@date
@RestController
@RequestMapping("/skywalking")publicclassAlarmController{
@AutowiredprivateDingTalkAlarmServicedingTalkAlarmService;
@PostMapping("/alarm")publicvoidalarm(@RequestBodyListalarmMessageDTOList){
log.info("收到告警信息:{}",JSON.toJSONString(alarmMessageDTOList));if(null!=alarmMessageDTOList){
alarmMessageDTOList.forEach(e->dingTalkAlarmService.sendMessage(MessageFormat.format("-----來自SkyWalking的告警-----\n【名稱】:{0}\n【消息】:{1}\n",e.getName(),e.getAlarmMessage())));
}
}
}?