Spring Cloud项目中实现分布式日志链路追踪

大家好,我是升仔

在微服务架构中,分布式日志链路追踪对于监控和理解服务间复杂的调用关系至关重要。本文详细介绍如何在Spring Cloud项目中使用Spring Cloud Sleuth和Zipkin实现分布式链路追踪。

搭建Zipkin Server

Zipkin Server是存储和展示追踪数据的中心。使用Docker是搭建Zipkin Server的一种简便方法。

使用Docker运行Zipkin

运行以下命令以启动Zipkin服务器:

复制代码
docker run -d -p 9411:9411 openzipkin/zipkin

这条命令会在本地的9411端口提供Zipkin服务。

创建Spring Boot微服务

我们将创建两个Spring Boot微服务应用,一个作为服务提供者(user-service),另一个作为服务消费者(client-app)。

服务提供者:user-service

添加依赖

user-servicepom.xml中添加Spring Cloud Sleuth和Zipkin的依赖:

复制代码
<dependencies>
    <!-- Spring Cloud Sleuth and Zipkin -->
    <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>
    <!-- Spring Boot Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
创建应用主类

定义应用的主类UserServiceApplication

复制代码
@SpringBootApplication
public class UserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}
创建控制器

定义一个简单的REST控制器UserController

复制代码
@RestController
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable String id) {
        logger.info("Fetching user with id {}", id);
        return "User " + id;
    }
}
配置文件

application.yml中配置应用名和Zipkin服务器地址:

复制代码
server:
  port: 8081
spring:
  application:
    name: user-service
  zipkin:
    base-url: http://localhost:9411

服务消费者:client-app

添加相同的依赖

client-apppom.xml中添加与user-service相同的依赖。

创建应用主类

定义应用的主类ClientAppApplication

创建控制器

定义一个REST控制器ClientController来调用user-service

复制代码
@RestController
public class ClientController {

    private final RestTemplate restTemplate;

    public ClientController(RestTemplateBuilder builder) {
        this.restTemplate = builder.build();
    }

    @GetMapping("/fetch-user/{id}")
    public String fetchUser(@PathVariable String id) {
        return restTemplate.getForObject("http://localhost:8081/user/" + id, String.class);
    }
}
配置文件

application.yml中配置应用名和Zipkin服务器地址:

复制代码
server:
  port: 8080
spring:
  application:
    name: client-app
  zipkin:
    base-url: http://localhost:9411

测试链路追踪

  1. 启动Zipkin Server。
  2. 分别启动user-serviceclient-app
  3. 访问client-app/fetch-user/{id}端点来间接调用user-service
  4. 在Zipkin UI(通常位于http://localhost:9411)查看追踪信息。
最后说一句(求关注,求赞,别白嫖)

最近无意间获得一份阿里大佬写的刷题笔记,一下子打通了我的任督二脉,进大厂原来没那么难。

这是大佬写的,7701页的BAT大佬写的刷题笔记,让我offer拿到手软

本文已收录于我的技术网站,next-java.com, 有大厂完整面经,工作技术等经验分享

求一键三连:点赞、分享、收藏

点赞对我真的非常重要!在线求赞,加个关注非常感激

相关推荐
NE_STOP1 天前
springMVC-HTTP消息转换器与文件上传、下载、异常处理
spring
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
初次攀爬者2 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
玹外之音2 天前
Spring AI MCP 实战:将你的服务升级为 AI 可调用的智能工具
spring·ai编程
来一斤小鲜肉2 天前
Spring AI入门:第一个AI应用跑起来
spring·ai编程
NE_STOP2 天前
springMVC-常见视图组件与RESTFul编程风格
spring
what丶k3 天前
Spring AI 多模态开发全解析:从入门到企业级落地
后端·spring·ai编程
追风筝的人er3 天前
企业管理系统如何实现自定义首页与千人千面?RuoYi Office 给出了完整方案
vue.js·spring boot·spring cloud
NE_STOP3 天前
springMVC-获取前端请求的数据与三个作用域
spring
莫寒清3 天前
Spring MVC:@PathVariable 注解详解
java·spring·mvc