记录下接入的过程:
1.skywalking agent的下载
在官网下载Downloads | Apache SkyWalking
下载之后解压,会发现有这个这样的,这个agent会在后面使用到
2.jar包依赖
springboot中添加依赖jar包
implementation 'org.apache.skywalking:apm-toolkit-logback-1.x:8.5.0' implementation 'org.apache.skywalking:apm-toolkit-trace:8.5.0'
3.logback配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-spring-skywalking.xml"/>
<springProperty name="APP_NAME" source="spring.application.name" defaultValue="1234"/>
<springProperty name="LOG_ROOT" source="log.root" defaultValue="${user.home}"/>
<springProperty name="LOG_LEVEL" source="log.level" defaultValue="info"/>
<!--定义参数,后面可以通过${APP_NAME}使用-->
<property name="LOG_PATH" value="${LOG_ROOT}/logs/${APP_NAME}"/>
<property name="LOG_FILE" value="${LOG_PATH}/application.log"/>
<property name="ERROR_LOG_FILE" value="${LOG_PATH}/error.log"/>
<appender name="APPLICATION"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--定义日志输出的路径-->
<file>${LOG_FILE}</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${FILE_LOG_PATTERN}</pattern>
</layout>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>3</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
</appender>
<appender name="ERROR_LOG"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--定义日志输出的路径-->
<file>${ERROR_LOG_FILE}</file>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${FILE_LOG_PATTERN}</pattern>
</layout>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ERROR_LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>3</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>error</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</layout>
<charset>utf8</charset>
</encoder>
</appender>
<!--rootLogger是默认的logger-->
<root level="${LOG_LEVEL}">
<!--定义了两个appender,日志会通过往这两个appender里面写-->
<appender-ref ref="CONSOLE"/>
<appender-ref ref="APPLICATION"/>
<appender-ref ref="ERROR_LOG"/>
</root>
</configuration>
在每个appender中添加了encoder的内容;logback-spring-skywalking.xml中内容如下
<?xml version="1.0" encoding="UTF-8"?>
<included>
<!-- 定义日志 mdc 配置 -->
<springProperty name="LOG_MDC" source="log.mdc" defaultValue="%tid %5p" />
<springProperty name="LOG_SKYWALKING_LEVEL" source="log.skywalking.level" defaultValue="INFO" />
<property name="LOG_LEVEL_PATTERN" value="${LOG_MDC}" />
<!-- 引入 spring 默认配置文件 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<!-- 输出 skywalking 日志 -->
<appender name="SKYWALKING"
class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
<!-- 日志输出编码 -->
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>${FILE_LOG_PATTERN}</pattern>
</layout>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${LOG_SKYWALKING_LEVEL}</level>
</filter>
</appender>
<!-- dev 环境不允许使用 -->
<springProfile name="!dev">
<root level="${LOG_SKYWALKING_LEVEL}">
<appender-ref ref="SKYWALKING" />
</root>
</springProfile>
</included>
4.测试
在intellij中测试的时候,需要在启动的时候加载agent;
这个里面第一行是agent的路径,下面的分别是agent的名字和skywalking的后端服务地址和端口;
项目启动的时候会有这个日志信息:
启动两个服务,模拟服务1,调用服务2的rest接口,然后打印两边的日志,查看traceid是否相同;
例如服务1的代码:
服务2的代码:
启动之后,打印日志:
可以看到服务1中日志
TID:290681366f064ce7b7b035a0c28f1552.59.16642570595410001 INFO 9961 --- [nio-8501-exec-1] com.example.demo.TestController
服务1调用服务2里面的日志
TID:290681366f064ce7b7b035a0c28f1552.59.16642570595410001 INFO 9890 --- [nio-8099-exec-5] com.example.demo.TestController : test3
说明这两个的服务是可以串起来的;