Skywalking主要分為oap、webapp和agent三部分,xxxxxx:xxxxx/hmall/skywalking-ui分別用于匯總數(shù)據(jù)和展示,這兩塊共同組成了Skywalking的平臺(tái);agent是探針,部署在需要收集數(shù)據(jù)的應(yīng)用服務(wù)器上,并將數(shù)據(jù)同步到Skywalking的平臺(tái)
記錄一下一個(gè)坑,之前使用的是6.5.0的skywalking,由于一些原因需要重裝oap和webapp,想偷個(gè)懶,oap和webapp裝8.x,agent還是用原來的6.5.0,結(jié)果agent的日志就報(bào)錯(cuò)了,說找不到注冊(cè)的方法。所以還是建議使用同一版本的SW,減少不必要麻煩。
下面開始正題:現(xiàn)在擬搭建oap和webapp在虛機(jī),agent在容器使用,oap和webapp在同一臺(tái)虛擬機(jī)運(yùn)行:
1.配置oap(暫時(shí)使用單機(jī),用ES作為存儲(chǔ))
cluster:
selector: ${SW_CLUSTER:standalone}
standalone:
storage:
selector: ${SW_STORAGE:elasticsearch}
elasticsearch:
clusterName: esCluster
nameSpace: ${SW_NAMESPACE:""}
clusterNodes: ${SW_STORAGE_ES_CLUSTER_NODES:xxx.xx.xx.xx:9200} #你的ES地址
protocol: ${SW_STORAGE_ES_HTTP_PROTOCOL:"http"}
user: ${SW_ES_USER:"xxxxxx"} #ES賬號(hào)
password: ${SW_ES_PASSWORD:"xxxx"} #ES密碼
trustStorePath: ${SW_STORAGE_ES_SSL_JKS_PATH:""}
trustStorePass: ${SW_STORAGE_ES_SSL_JKS_PASS:""}
secretsManagementFile: ${SW_ES_SECRETS_MANAGEMENT_FILE:""} # Secrets management file in the properties format includes the username, password, which are managed by 3rd party tool.
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:1} # Shard number of new indexes
indexReplicasNumber: ${SW_STORAGE_ES_INDEX_REPLICAS_NUMBER:1} # Replicas number of new indexes
# Super data set has been defined in the codes, such as trace segments.The following 3 config would be improve es performance when storage super size data in es.
superDatasetDayStep: ${SW_SUPERDATASET_STORAGE_DAY_STEP:-1} # Represent the number of days in the super size dataset record index, the default value is the same as dayStep when the value is less than 0
superDatasetIndexShardsFactor: ${SW_STORAGE_ES_SUPER_DATASET_INDEX_SHARDS_FACTOR:5} # This factor provides more shards for the super data set, shards number = indexShardsNumber * superDatasetIndexShardsFactor. Also, this factor effects Zipkin and Jaeger traces.
superDatasetIndexReplicasNumber: ${SW_STORAGE_ES_SUPER_DATASET_INDEX_REPLICAS_NUMBER:0} # Represent the replicas number in the super size dataset record index, the default value is 0.
bulkActions: ${SW_STORAGE_ES_BULK_ACTIONS:1000} # Execute the async bulk record data every ${SW_STORAGE_ES_BULK_ACTIONS} requests
syncBulkActions: ${SW_STORAGE_ES_SYNC_BULK_ACTIONS:50000} # Execute the sync bulk metrics data every ${SW_STORAGE_ES_SYNC_BULK_ACTIONS} 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:""}
注意,有些版本的SW是需要使用ES7的,這個(gè)配置文件有專門的elasticsearch7配置,且使用7.x.x版本的es需要另外下載apache-skywalking-bin-es7.tar.gz包,詳見官方github
出于性能考慮,官方推薦在ES配置新增:
thread_pool.index.queue_size: 1000 #只適用于ElasticSearch 6
thread_pool.write.queue_size: 1000 #適用于ElasticSearch 6 and 7
index.max_result_window: 1000000 #trace頁面出錯(cuò)時(shí)記得設(shè)置
PS:如果沒有ES的es_keystore.jks,需要注釋以下配置:
# trustStorePath: ${SW_SW_STORAGE_ES_SSL_JKS_PATH:"../es_keystore.jks"}
# trustStorePass: ${SW_SW_STORAGE_ES_SSL_JKS_PASS:""}
2.webapp配置:
位置在/webapp/webapp.yml
3.啟動(dòng)oap和webapp:
在/bin目錄下運(yùn)行sh startup .sh
4.agent使用:
將agent文件夾拷貝到需要監(jiān)控的應(yīng)用所在服務(wù)器,配置agentagent.config
# 不同的namespace會(huì)導(dǎo)致調(diào)用鏈路追蹤中斷
agent.namespace=${SW_AGENT_NAMESPACE:xxx}
# 頁面上展示的service的名稱,也可以通過-Dskywalking.agent.service_name=xxx指定
agent.service_name=${SW_AGENT_NAME:xxxxx}
# 平臺(tái)的調(diào)用地址,也可以通過-Dskywalking.collector.backend_service=127.0.0.1:80指定
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:1xxx.xxx.xxx.xxx:11800}
# 忽略指定后綴的請(qǐng)求收集
agent.ignore_suffix=${SW_AGENT_IGNORE_SUFFIX:.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg}
# 每3秒的采樣率,負(fù)數(shù)代表100%
agent.sample_n_per_3_secs=${SW_AGENT_SAMPLE:-1}
將agent打成鏡像供拉取Dockerfile:
FROM busybox:latest
ENV LANG=C.UTF-8
RUN set -eux && mkdir -p /opt/skywalking/agent/
ADD agent/ /opt/skywalking/agent/
WORKDIR /
再運(yùn)行打鏡像的命令:在Dockerfile所在目錄運(yùn)行docker build -t 名稱
如果需要?jiǎng)討B(tài)配置,可以在k8s部署yaml加上環(huán)境變量,且在需要監(jiān)控的應(yīng)用(java)打鏡像的dockerfile加:
FROM java:8
VOLUME /tmp
ARG JAR_FILE
ADD target/${JAR_FILE} app.jar
ENTRYPOINT exec java $JAVA_OPTS -Dapp.id=$APP_ID \
-javaagent:/opt/skywalking/agent/skywalking-agent.jar \
-Dskywalking.agent.service_name=$APP_ID \
-Dskywalking.collector.backend_service=$SKYWALKING_ADDR
最后查看dashboard,默認(rèn)8080端口。地址為裝oap和webapp的主機(jī)ip:8080
基本流程:
1.agent收集trace(在skywalking中,一個(gè)trace由多個(gè)tracesegment構(gòu)成,一個(gè)tracesegment由多個(gè) span 構(gòu)成的,span可理解為當(dāng)前所在的一個(gè)對(duì)象,如果調(diào)用兩一個(gè)服務(wù),那另一個(gè)服務(wù)就是新的一個(gè)span)
一次調(diào)用過程,每個(gè)traceId都是一致的,在非跨線程/進(jìn)程情況下(在同一個(gè)線程中),children通過parentSpanId關(guān)聯(lián)父span的spanId,就像一個(gè)鏈條一樣把一條鏈路給串起來,如果是跨線程/進(jìn)程,又會(huì)創(chuàng)建出一個(gè)新的鏈條,這時(shí)就用 refs 把這幾段鏈條給接連接起來。(為了標(biāo)識(shí)出 children 的父親到底是誰,使用屬性 refs,關(guān)聯(lián)上父節(jié)點(diǎn)的所有 ID 信息)
相同顏色的小球在同一個(gè)線程中 , 在同一個(gè)線程中 的 segmentId 都是相同的,使用spanId以及parentSpanId去連接鏈路,線程、進(jìn)程中不同的鏈路使用refs去連接,同時(shí)多個(gè)線程鏈路組成一個(gè)trace,他們的 traceID都是相同的??偨Y(jié)一下:
- spanId : 同一個(gè)線程中唯一, 從0始,按照調(diào)用鏈路從0遞增
- parentSpanId : 在同一個(gè)線程的鏈路中,用來連接 span
- segmentId : 同一個(gè) 線程鏈路中,這個(gè)值都是相同的,不同線程鏈路中 這個(gè)值不同
- traceId : 在一個(gè) 鏈路 中 traceId 唯一
agent里自帶插件mvc-annotation-commons
,會(huì)去代理有@requestMapping
注解的方法:
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
return new InstanceMethodsInterceptPoint[] {
new DeclaredInstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.RequestMapping"));
}
@Override
public String getMethodsInterceptor() {
return "org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.RequestMappingMethodInterceptor";
}
@Override
public boolean isOverrideArgs() {
return false;
}
},
new DeclaredInstanceMethodsInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getMethodsMatcher() {
return byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.GetMapping"))
.or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.PostMapping")))
.or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.PutMapping")))
.or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.DeleteMapping")))
.or(byMethodInheritanceAnnotationMatcher(named("org.springframework.web.bind.annotation.PatchMapping")));
}
@Override
public String getMethodsInterceptor() {
return "org.apache.skywalking.apm.plugin.spring.mvc.commons.interceptor.RequestMappingMethodInterceptor";
}
@Override
public boolean isOverrideArgs() {
return false;
}
}
};
}
將有注解的方法加上代理方法RequestMappingMethodInterceptor
,主要去新增span。之后通過生產(chǎn)-消費(fèi)模型,將產(chǎn)生的span對(duì)象們通過GRPC批量發(fā)送。
2.agent發(fā)送trace給oap(controller)
3.oap接收并構(gòu)建
4.oap將數(shù)據(jù)持久化(ES/H2等)
以下是運(yùn)行時(shí)候出現(xiàn)的一些問題:
1.skywalking運(yùn)行一段時(shí)間后,發(fā)現(xiàn)沒有新的數(shù)據(jù)顯示
首先查看日志:
2021-02-18 02:27:51,673 - org.apache.skywalking.oap.server.library.server.grpc.GRPCServer - 118 [grpc-default-worker-ELG-3-3] WARN [] - Grpc server thread pool is full, rejecting the task
Grpc線程池滿了,什么意思呢?可以理解為oap服務(wù)的吞吐量太弱,存儲(chǔ)性能跟不上,最簡(jiǎn)單的方法就是增加集群數(shù)量,如果不行則設(shè)置:
默認(rèn)grpc server的線程池大小是4cpu數(shù),排隊(duì)隊(duì)列長度是10000,可以調(diào)整這兩個(gè)參數(shù)大?。憾ㄎ坏絘pplication.yml文件。在core的default下增加gRPCThreadPoolSize: 默認(rèn)是4倍的cpu,這里可以調(diào)高,例如8核默認(rèn)是48=32,可以調(diào)成40或更大gRPCThreadPoolQueueSize:默認(rèn)是10000,可以調(diào)高
參考:
http://www.lxweimin.com/p/4f4c182bcbd8
http://www.lxweimin.com/p/8b9aad4210c5
https://juejin.cn/post/6844903556772954125#heading-11
https://blog.csdn.net/u010928589/article/details/106542794
https://www.cnblogs.com/kebibuluan/p/13153819.html
https://my.oschina.net/osgit/blog/4558674 (一些skywalking運(yùn)行時(shí)的問題)