-
Notifications
You must be signed in to change notification settings - Fork 25
Tracing
睿驰 edited this page Mar 26, 2021
·
5 revisions
分布式链路追踪示例。
完整示例代码:juice-samples
首先,添加juice-spring-boot-starter依赖:
<dependency>
<groupId>io.infinityclub</groupId>
<artifactId>juice-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
application.yml增加如下配置:
# juice
juice:
tracing:
enable: true
pathPatterns: /api/*
应用启动后,juice-spring-boot-starter会自动注入juice.tracing.filter.TracingFilter,接口响应头会出现 X-TraceId
。
如果希望RestTemplate发送请求时自动携带traceId和spanId,可以在RestTemplate上加上@DistributedTracing,如下:
import juice.tracing.annotation.DistributedTracing;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@DistributedTracing
@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(30000);//单位为ms
factory.setConnectTimeout(20000);//单位为ms
return new RestTemplate(factory);
}
}
如果项目中使用的是logback日志框架,只需要在logback-spring.xml 加上相应配置即可在日志中输出TraceId
。
完整logback-spring.xml示例:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="projectName" value="@project.artifactId@" />
<property name="LOG_LEVEL" value="INFO" />
<property name="LOG_PATH" value="logs/${projectName}"/>
<property name="LOG_FILE_NAME" value="${projectName}"/>
<property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%X{X-TraceId}] %-5level %logger - %msg%n"/>
<property name="LOG_STASH_HOST" value="10.10.37.2:5045" />
<!--配置规则类的位置-->
<conversionRule conversionWord="ip" converterClass="juice.core.logback.IPLogConfig" />
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>${LOG_STASH_HOST}</destination>
<keepAliveDuration>5 minutes</keepAliveDuration>
<writeBufferSize>81920</writeBufferSize>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp/>
<version/>
<pattern>
<pattern>
<!-- ${hostIp}是应用服务所在系统IP,${prodName} 是服务项目名称 ,需要提前配置logback变量;这些格式是固定的,如果需要调整,需要提前沟通 -->
{"host": "%ip","project": "${projectName}","log_level": "%level","trace": "%X{X-TraceId:-}","span": "%X{X-SpanId:-}","parent": "%X{X-ParentSpanId:-}","thread": "%thread","class": "%logger","message": "%msg","exception_stack": "%ex{full}"}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${LOG_FILE_NAME}-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>500MB</maxFileSize>
<maxHistory>7</maxHistory>
<totalSizeCap>4GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--async appender-->
<appender name ="asyncLogStash" class= "ch.qos.logback.classic.AsyncAppender">
<discardingThreshold >0</discardingThreshold>
<queueSize>256</queueSize>
<appender-ref ref ="LOGSTASH"/>
</appender>
<appender name ="asyncFile" class= "ch.qos.logback.classic.AsyncAppender">
<discardingThreshold >0</discardingThreshold>
<queueSize>256</queueSize>
<appender-ref ref ="FILE"/>
</appender>
<appender name ="asyncConsole" class= "ch.qos.logback.classic.AsyncAppender">
<discardingThreshold >0</discardingThreshold>
<queueSize>128</queueSize>
<appender-ref ref ="CONSOLE"/>
</appender>
<root level="${LOG_LEVEL}">
<appender-ref ref="asyncLogStash"/>
<appender-ref ref="asyncFile"/>
<appender-ref ref="asyncConsole"/>
</root>
<logger name="juice" level="DEBUG" />
</configuration>
@Copyright Ricky Fung 2012-2021 [但行好事,莫问前程]