我们如何将 Spring Cloud Sleuth 与 spring MVC 项目一起使用?
How can we use Spring Cloud Sleuth with spring MVC project?
我正在尝试将 spring 云侦探与 spring web mvc 项目一起使用。我们正在将请求从 spring web mvc 发送到另一个 spring 引导项目。我需要在 UI 中显示 traceID 和 spanId。我们怎样才能做到这一点?
编辑
POM.xml 用于 spring 引导应用程序。我正在使用最新版本的 Spring Boot 并添加了 spring cloud sleuth 的依赖项。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Web MVC 代码:我正在使用 spring rest 模板并从 spring web mvc 应用程序调用 spring 引导服务。
@Controller
public class HelloController {
@Autowired
RestTemplate restTemplate;
private Logger logger = Logger.getLogger(HelloController.class);
@GetMapping("/hello")
public String hello(Model model) {
logger.info("Hello from controller");
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8092/test", String.class);
String response = entity.getBody();
logger.info("Response from Service: "+ response);
model.addAttribute("name", "Test");
return "welcome";
}
}
为此,您需要注入 Tracer
bean,然后如果您调用 tracer.currentSpan().context().traceIdString()
和 tracer.currentSpan().context().spanIdString()
。
我正在尝试将 spring 云侦探与 spring web mvc 项目一起使用。我们正在将请求从 spring web mvc 发送到另一个 spring 引导项目。我需要在 UI 中显示 traceID 和 spanId。我们怎样才能做到这一点?
编辑
POM.xml 用于 spring 引导应用程序。我正在使用最新版本的 Spring Boot 并添加了 spring cloud sleuth 的依赖项。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Web MVC 代码:我正在使用 spring rest 模板并从 spring web mvc 应用程序调用 spring 引导服务。
@Controller
public class HelloController {
@Autowired
RestTemplate restTemplate;
private Logger logger = Logger.getLogger(HelloController.class);
@GetMapping("/hello")
public String hello(Model model) {
logger.info("Hello from controller");
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:8092/test", String.class);
String response = entity.getBody();
logger.info("Response from Service: "+ response);
model.addAttribute("name", "Test");
return "welcome";
}
}
为此,您需要注入 Tracer
bean,然后如果您调用 tracer.currentSpan().context().traceIdString()
和 tracer.currentSpan().context().spanIdString()
。