應(yīng)用修改
引入依賴
目的是實(shí)現(xiàn)手動獲取鏈路信息、鏈路信息日志記錄等擴(kuò)展功能棍厌。
<properties>
<skywalking.version>8.14.0</skywalking.version>
</properties>
<dependencies>
<!-- 省略其他依賴 -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>${skywalking.version}</version>
</dependency>
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>${skywalking.version}</version>
</dependency>
</dependencies>
日志模板
修改logback配置文件,以實(shí)現(xiàn)將traceId
和spanId(segmentId)
輸出到日志嗤堰。
-
LOG_PATTERN
<property name="LOG_PATTERN" value= "|%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%replace(%X{tid}){'TID:',''}|%replace(%X{sw_ctx}){'(([\\w\\[\\-\\.:@/]*,){3})|(,-?\\d+\\])',''}|${APP_NAME}|%t|%C|%M|%L|%m%n"/>
log4j2與logback的replace函數(shù)有細(xì)微差別冷溃,如有需要可查閱log4j2文檔。
-
appender.encoder
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout"> <Pattern>${LOG_PATTERN}</Pattern> </layout> <charset>UTF-8</charset> </encoder>
注意事項(xiàng)
-
鏈路信息跨線程解決方案
默認(rèn)情況下乓土,鏈路信息只能在線程內(nèi)傳遞宪潮,如果應(yīng)用代碼中有跨線程邏輯溯警,需要手動處理:-
usage 1
@TraceCrossThread public static class MyCallable<String> implements Callable<String> { @Override public String call() throws Exception { return null; } } ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.submit(new MyCallable());
-
usage 2
@TraceCrossThread public static class MyRunnable implements Runnable { @Override public void run() { // TODO } } ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.submit(new MyRunnable());
-
usage 3
ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.submit(CallableWrapper.of(new Callable<String>() { @Override public String call() throws Exception { return null; } }));
-
usage 4
ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.execute(RunnableWrapper.of(new Runnable() { @Override public void run() { //TODO } }));
-
usage 5
CompletableFuture.supplyAsync(SupplierWrapper.of(() -> { return "SupplierWrapper"; })).thenApplyAsync(FunctionWrapper.of(f -> { return "FunctionWrapper"; })).thenAcceptAsync(ConsumerWrapper.of(c -> { System.out.println("ConsumerWrapper"); }));
-
-
在程序中手動獲取鏈路信息
String traceId = TraceContext.traceId(); String segmentId = TraceContext.segmentId(); String spanId = TraceContext.spanId();
部署修改
Dockerfile
一般應(yīng)用
修改FROM
基礎(chǔ)鏡像并修改WORKDIR
,示例:
FROM apache/skywalking-java-agent:8.14.0-java11
MAINTAINER lms lmstech@163.com
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
WORKDIR /
ADD target/analysis-boot.jar analysis-boot.jar
ENTRYPOINT ["java", "-Xmx98m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/analysis-boot.jar"]
EXPOSE 8806
spring-cloud-gateway
skywalking-java-agent
以插件的形式提供對不同框架的支持狡相,而spring-cloud-gateway
和spring-webflux
插件是可選插件梯轻,需要手動引入。示例:
FROM apache/skywalking-java-agent:8.14.0-java11
MAINTAINER lms lmstech@163.com
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
# 添加網(wǎng)關(guān)所需插件
RUN cp /skywalking/agent/optional-plugins/apm-spring-cloud-gateway-3.x-plugin-8.14.0.jar /skywalking/agent/plugins/
RUN cp /skywalking/agent/optional-plugins/apm-spring-webflux-5.x-plugin-8.14.0.jar /skywalking/agent/plugins/
WORKDIR /
ADD target/lms-gateway.jar lms-gateway.jar
ENTRYPOINT ["java", "-Xmx98m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/lms-gateway.jar"]
EXPOSE 9999
部署腳本
docker
至少需要添加以下三個配置項(xiàng)尽棕,以環(huán)境變量形式注入:
配置項(xiàng) | 說明 | 推薦配置 |
---|---|---|
SW_AGENT_NAME | 服務(wù)名稱喳挑,格式為<group name>::<logic name> | 項(xiàng)目名稱::應(yīng)用名詞,如: lms::alarm-sender
|
SW_AGENT_INSTANCE_NAME | 實(shí)例(節(jié)點(diǎn))名稱 | 應(yīng)用名詞@部署地址滔悉,如: alarm-sender@192.168.0.1
|
SW_AGENT_COLLECTOR_BACKEND_SERVICES | skywalking后端地址 | ip:port |
更多配置項(xiàng)參考官方文檔伊诵。
示例:
docker run -d -p 8806:8806 -e SW_AGENT_NAME=lms::analysis -e SW_AGENT_INSTANCE_NAME=analysis@192.168.0.1 -e SW_AGENT_COLLECTOR_BACKEND_SERVICES=114.252.40.238:11800 lms-anlysis:latest
docker-compose
示例:
version: '3.1'
services:
analysis:
build:
context: ./
dockerfile: Dockerfile
image: analysis:latest
container_name: lms-analysis
ports:
- 8806:8806
volumes:
- /app/logs/analysis:/logs
environment:
- SW_AGENT_NAME=lms::analysis
- SW_AGENT_INSTANCE_NAME=analysis@192.168.0.1
- SW_AGENT_COLLECTOR_BACKEND_SERVICES=114.252.40.238:11800