我将创建两个module,然后一个调用另一个,然后就可以搭建出环境来,看看链路追踪输出的日志到底长啥样。
maven依赖
摘取部分:
<groupId>com.example</groupId>
<artifactId>sleuth-traceA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sleuth-traceA</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
main函数改造
项目A
@EnableDiscoveryClient
@RestController
@Slf4j
@SpringBootApplication
public class SleuthTraceAApplication {
@LoadBalanced
@Bean
public RestTemplate lb(){
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/traceA")
private String traceA(){
log.info("-------Trace A");
return restTemplate.getForEntity("http://sleuth-traceB/traceB",String.class).getBody();
}
public static void main(String[] args) {
SpringApplication.run(SleuthTraceAApplication.class, args);
}
}
项目B
@EnableDiscoveryClient
@RestController
@Slf4j
@SpringBootApplication
public class SleuthTraceBApplication {
@LoadBalanced
@Bean
public RestTemplate lb(){
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@GetMapping(value = "/traceB")
private String traceA(){
log.info("-------Trace B");
return "Trace B";
}
public static void main(String[] args) {
SpringApplication.run(SleuthTraceBApplication.class, args);
}
}
properties配置
A:
server.port= 63001
spring.application.name=sleuth-traceA
eureka.client.service-url.defaultZone = http://localhost:2001/eureka/
logging.file.name=${spring.application.name}.log
info.app.name = sleuth-traceA
info.app.description=test
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#sleuth config 1就是百分百的记录
spring.sleuth.sampler.probability=1
B:
server.port= 63002
spring.application.name=sleuth-traceB
eureka.client.service-url.defaultZone = http://localhost:2001/eureka/
logging.file.name=${spring.application.name}.log
info.app.name = sleuth-traceB
info.app.description=test
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#sleuth config 1就是百分百的记录
spring.sleuth.sampler.probability=1
启动项目
首先启动Eureka,然后把两个项目都启动起来,然后首先调用一下项目B的接口,然后再调用一下项目A的接口,可以发现日志:
项目A:
2021-09-17 13:59:38.966 INFO [sleuth-traceA,273cd04e9aa0c900,273cd04e9aa0c900] 13836 --- [io-63001-exec-1] c.e.s.SleuthTraceAApplication : -------Trace A
项目B:
2021-09-17 13:59:30.682 INFO [sleuth-traceB,5d2a4ae7658d99cf,5d2a4ae7658d99cf] 11736 --- [io-63002-exec-2] c.e.s.SleuthTraceBApplication : -------Trace B
2021-09-17 13:59:39.115 INFO [sleuth-traceB,273cd04e9aa0c900,d6cc57db7cf1f11b] 11736 --- [io-63002-exec-3] c.e.s.SleuthTraceBApplication : -------Trace B
我们可以分析一下,项目B第一次我们调用了一下,因为B接口调用完只是自己内部打印了一下日志,并没有跟其他服务有所交互,所以可以看到sleuth打印的两个 字符串都一致,根据之前帖子来看,第一个字符串是 调用链id,第二个是spanid。
而看项目A的打印日志可以看到,第一个是调用链id,然后对应B项目第二回打印的日志的调用链id,都是一样的。
这就很好的模拟了调用链的追踪。
网友评论