sleuth springboot
时间: 2023-11-16 10:00:10 浏览: 39
Spring Cloud Sleuth是一个分布式跟踪解决方案,它与Spring Boot 2功能端点集成。它可以帮助开发人员跟踪应用程序中的请求,并提供有关请求的详细信息,例如请求的来源和目标服务。同时,Sleuth还可以与logback的MDC特性集成,以便在日志中记录跟踪信息。如果您想要在Spring Boot 2应用程序中使用Sleuth,请参考官方文档进行配置和使用。
以下是相关问题:
相关问题
Sleuth 开源springboot
Sleuth是Spring Cloud生态中的一个组件,它是一个开源的分布式跟踪系统,基于Zipkin实现,可以帮助我们更好地跟踪和监控微服务的调用情况。而Spring Boot是一个开源的Java Web框架,它可以帮助我们更快、更简单地构建Web应用程序。在使用Spring Boot构建微服务时,我们通常会使用Spring Cloud来实现微服务架构,而Sleuth就是Spring Cloud生态中的一个组件,可以与Spring Boot无缝集成,方便我们进行分布式跟踪。因此,Sleuth和Spring Boot虽然不是同一个东西,但它们可以一起使用,以便更好地构建分布式系统。
Spring Cloud Sleuth(服务跟踪) springboot案例
好的,下面是一个使用Spring Cloud Sleuth进行服务跟踪的Spring Boot案例:
1. 添加依赖
在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
```
2. 添加配置
在application.yml文件中添加以下配置:
```yaml
spring:
application:
name: sleuth-demo
sleuth:
sampler:
probability: 1.0
zipkin:
base-url: http://localhost:9411
```
其中,sleuth.sampler.probability配置为1.0表示采样率为100%。
3. 编写代码
编写一个Controller,调用其他服务:
```java
@RestController
public class MyController {
private final RestTemplate restTemplate;
public MyController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/hello")
public String hello() {
String result = restTemplate.getForObject("http://localhost:8080/world", String.class);
return "Hello " + result;
}
}
```
4. 启动服务
启动服务后,访问http://localhost:8080/hello,可以看到控制台输出类似于以下日志:
```
2021-05-28 11:08:26.306 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] c.e.sleuthdemo.controller.MyController : hello
2021-05-28 11:08:26.311 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient : http://localhost:8080/world HTTP/1.1
2021-05-28 11:08:26.311 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient : Content-Length: 0
2021-05-28 11:08:26.311 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient : Host: localhost:8080
2021-05-28 11:08:26.316 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient : HTTP/1.1 200
2021-05-28 11:08:26.316 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient : Content-Type: application/json
2021-05-28 11:08:26.316 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient : Transfer-Encoding: chunked
2021-05-28 11:08:26.316 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient : Date: Fri, 28 May 2021 03:08:26 GMT
2021-05-28 11:08:26.316 INFO [sleuth-demo,9c3a3f80e0516d7f,9c3a3f80e0516d7f,false] 14292 --- [nio-8080-exec-1] o.apache.http.client.HttpClient :
```
这里的日志中,sleuth-demo为当前服务名,9c3a3f80e0516d7f为当前请求的Trace ID,false表示当前请求没有被采样。
5. 查看服务跟踪
访问http://localhost:9411/zipkin/,可以看到服务跟踪页面。在搜索框中输入服务名(sleuth-demo),可以看到当前服务的请求链路和请求耗时等信息。