springboot整合skywalking

Skywalking主要分為oap、webapp和agent三部分,xxxxxx:xxxxx/hmall/skywalking-ui分別用于匯總數(shù)據(jù)和展示,這兩塊共同組成了Skywalking的平臺;agent是探針,部署在需要收集數(shù)據(jù)的應用服務器上,并將數(shù)據(jù)同步到Skywalking的平臺

記錄一下一個坑,之前使用的是6.5.0的skywalking,由于一些原因需要重裝oap和webapp,想偷個懶,oap和webapp裝8.x,agent還是用原來的6.5.0,結(jié)果agent的日志就報錯了,說找不到注冊的方法。所以還是建議使用同一版本的SW,減少不必要麻煩。


下面開始正題:現(xiàn)在擬搭建oap和webapp在虛機,agent在容器使用,oap和webapp在同一臺虛擬機運行:

1.配置oap(暫時使用單機,用ES作為存儲)

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賬號
    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的,這個配置文件有專門的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頁面出錯時記得設置

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.啟動oap和webapp:
在/bin目錄下運行sh startup .sh

4.agent使用:
將agent文件夾拷貝到需要監(jiān)控的應用所在服務器,配置agentagent.config

# 不同的namespace會導致調(diào)用鏈路追蹤中斷
agent.namespace=${SW_AGENT_NAMESPACE:xxx}

# 頁面上展示的service的名稱,也可以通過-Dskywalking.agent.service_name=xxx指定
agent.service_name=${SW_AGENT_NAME:xxxxx}

# 平臺的調(diào)用地址,也可以通過-Dskywalking.collector.backend_service=127.0.0.1:80指定
collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES:1xxx.xxx.xxx.xxx:11800}

# 忽略指定后綴的請求收集
agent.ignore_suffix=${SW_AGENT_IGNORE_SUFFIX:.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg}


# 每3秒的采樣率,負數(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 /

再運行打鏡像的命令:在Dockerfile所在目錄運行docker build -t 名稱

如果需要動態(tài)配置,可以在k8s部署yaml加上環(huán)境變量,且在需要監(jiān)控的應用(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,默認8080端口。地址為裝oap和webapp的主機ip:8080

基本流程:

1.agent收集trace(在skywalking中,一個trace由多個tracesegment構(gòu)成,一個tracesegment由多個 span 構(gòu)成的,span可理解為當前所在的一個對象,如果調(diào)用兩一個服務,那另一個服務就是新的一個span)

一次調(diào)用過程,每個traceId都是一致的,在非跨線程/進程情況下(在同一個線程中),children通過parentSpanId關(guān)聯(lián)父span的spanId,就像一個鏈條一樣把一條鏈路給串起來,如果是跨線程/進程,又會創(chuàng)建出一個新的鏈條,這時就用 refs 把這幾段鏈條給接連接起來。(為了標識出 children 的父親到底是誰,使用屬性 refs,關(guān)聯(lián)上父節(jié)點的所有 ID 信息)


image.png

相同顏色的小球在同一個線程中 , 在同一個線程中 的 segmentId 都是相同的,使用spanId以及parentSpanId去連接鏈路,線程、進程中不同的鏈路使用refs去連接,同時多個線程鏈路組成一個trace,他們的 traceID都是相同的??偨Y(jié)一下:

  • spanId : 同一個線程中唯一, 從0始,按照調(diào)用鏈路從0遞增
  • parentSpanId : 在同一個線程的鏈路中,用來連接 span
  • segmentId : 同一個 線程鏈路中,這個值都是相同的,不同線程鏈路中 這個值不同
  • traceId : 在一個 鏈路 中 traceId 唯一

agent里自帶插件mvc-annotation-commons,會去代理有@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)-消費模型,將產(chǎn)生的span對象們通過GRPC批量發(fā)送。
2.agent發(fā)送trace給oap(controller)
3.oap接收并構(gòu)建
4.oap將數(shù)據(jù)持久化(ES/H2等)


以下是運行時候出現(xiàn)的一些問題:
1.skywalking運行一段時間后,發(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服務的吞吐量太弱,存儲性能跟不上,最簡單的方法就是增加集群數(shù)量,如果不行則設置:
默認grpc server的線程池大小是4cpu數(shù),排隊隊列長度是10000,可以調(diào)整這兩個參數(shù)大?。憾ㄎ坏絘pplication.yml文件。在core的default下增加gRPCThreadPoolSize: 默認是4倍的cpu,這里可以調(diào)高,例如8核默認是48=32,可以調(diào)成40或更大gRPCThreadPoolQueueSize:默認是10000,可以調(diào)高


參考:
http://www.itdecent.cn/p/4f4c182bcbd8
http://www.itdecent.cn/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運行時的問題)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容