目录
应用修改
引入依赖
目的是实现手动获取链路信息、链路信息日志记录等扩展功能。
<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>
或者直接引入com.lms:common-log
:
<properties>
<lms.version>main-4.0.0</lms.version>
</properties>
<dependencies>
<!-- 省略其他依赖 -->
<dependency>
<groupId>com.lms</groupId>
<artifactId>common-log</artifactId>
<version>${lms.version}</version>
</dependency>
</dependencies>
日志模板
修改logback配置文件,以实现将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函数有细微差别,如有需要可查阅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>
注意事项
-
在程序中手动获取链路信息
// traceId在整个链路中唯一,一个trace包含一个或多个segment String traceId = TraceContext.traceId(); // segmentId在单个进程内唯一,一个segment包含一个或多个span String segmentId = TraceContext.segmentId(); // span包含在segment内,通常表示一个线程或一个组件(如spring-mvc,mq,jdbc等) String spanId = TraceContext.spanId();
-
链路信息跨线程解决方案
默认情况下,链路信息只能在线程内传递,如果应用代码中有跨线程逻辑,需要手动处理:-
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"); }));
-
部署修改
Dockerfile
一般应用
修改FROM
基础镜像和WORKDIR
,示例:
FROM apache/skywalking-java-agent:8.14.0-java11
MAINTAINER lms [email protected]
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 [email protected]
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
# 添加网关所需插件
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
至少需要添加以下三个配置项,以环境变量形式注入:
配置项 | 说明 | 推荐配置 |
---|---|---|
SW_AGENT_NAME | 服务名称,格式为 |
项目名称::应用名称,如: lms::alarm-sender |
SW_AGENT_INSTANCE_NAME | 实例(节点)名称 | 应用名词@部署地址,如: [email protected] |
SW_AGENT_COLLECTOR_BACKEND_SERVICES | skywalking后端地址 | ip:port |
更多配置项参考官方文档。
示例:
docker run -d -p 8806:8806 -e SW_AGENT_NAME=lms::analysis -e [email protected] -e SW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.3.39: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
- [email protected]
- SW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.3.39:11800
标签:SW,lms,jar,analysis,AGENT,链路,Skywalking,skywalking,追踪
From: https://www.cnblogs.com/cheap-cabbage/p/17384391.html