31、Spring Cloud 中整合Zipkin进行服务跟踪zipkin-client

简介: 今天讲解一下如何在微服务中引入Zipkin Client,然后结合Zipkin Server监控各微服务间的调用链路。

上一篇简介了Zipkin Server的搭建,但是从Spring boot 2.x版本后,Zipkin官网已经不再推荐自己搭建定制Zipkin,而是直接提供了编译好的jar包。详情可以查看官网:


https://zipkin.io/pages/quickstart.html


微信图片_20220501223350.png


        有了Zipkin Server还不能对微服务的调用链路进行人祸监控,Zipkin Server可以被认为是一个数据处理和展示中心,那它的数据哪里来呢?需要Zipkin Client作为代理连接到Zipkin Server源源不断的上送过来。今天讲解一下如何在微服务中引入Zipkin Client,然后结合Zipkin Server监控各微服务间的调用链路。整体调用链路图如下:


微信图片_20220501223354.png


涉及的项目:


注册中心:sc-eureka-server

Zipkin serversc-zipkin-server

微服务:sc-zipkin-client-websc-zipkin-client-service

 

1、 新建项目sc-zipkin-client-service,对应的pom.xml文件如下


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>sc-zipkin-client-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-zipkin-client-service</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>


备注:主要引入了spring-cloud-starter-zipkin,说明这是一个zipkin client

 

2、 新建配置文件application.yml


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/
server:
  port: 9201
spring:
  application:
    name: sc-zipkin-client-service
  zipkin:
base-url: http://localhost:9000


微信图片_20220501223401.png


3、 sc-zipkin-client-service(普通的微服务)项目其他项目文件如下图


微信图片_20220501223405.png


4、 新建项目sc-zipkin-client-web,对应的pom.xml文件如下


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>sc-zipkin-client-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-zipkin-client-web</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>


备注:同样引入了spring-cloud-starter-zipkin,说明是一个zipkin client

 

5、 新建配置文件application.yml


eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/
server:
  port: 9202
spring:
  application:
    name: sc-zipkin-client-web
  zipkin:
    base-url: http://localhost:9000


微信图片_20220501223414.png


6、 sc-zipkin-client-web(普通的微服务)项目其他项目文件如下图


微信图片_20220501223418.png

 

 

7、 验证


项目启动顺序:


sc-eureka-server
  sc-zipkin-server
  sc-zipkin-client-service
  sc-zipkin-client-web


访问注册中心:http://127.0.0.1:5001/


服务都已经注册成功


微信图片_20220501223422.png


访问Zinkin Serverhttp://localhost:9000/zipkin/


微信图片_20220501223425.png


目前zipkin server没有记录任何的微服务调用链路数据。


分别访问接口:


http://127.0.0.1:9202/user/listUser


微信图片_20220501223430.png


http://127.0.0.1:9202/user/getUser/1


微信图片_20220501223434.png


再次查看Zipkin Server(如果没有出现可以多访问几次接口,Zipkin需要更多的监控数据)


微信图片_20220501223438.png

 

微信图片_20220501223441.png


相关文章
|
1月前
|
负载均衡 算法 Java
【SpringCloud(4)】OpenFeign客户端:OpenFeign服务绑定;调用服务接口;Feign和OpenFeign
Feign是一个WebService客户端。使用Feign能让编写WebService客户端更加简单。 它的使用方法是定义一个服务接口然后再上面添加注解。Feign也支持可拔插式的编码器和解码器。SpringCloud对Feign进行了封装,十七支持了SpringMVC标准注解和HttpMessageConverters。 Feign可用于Eureka和Ribbon组合使用以支持负载均衡
632 138
|
人工智能 Java Serverless
【MCP教程系列】搭建基于 Spring AI 的 SSE 模式 MCP 服务并自定义部署至阿里云百炼
本文详细介绍了如何基于Spring AI搭建支持SSE模式的MCP服务,并成功集成至阿里云百炼大模型平台。通过四个步骤实现从零到Agent的构建,包括项目创建、工具开发、服务测试与部署。文章还提供了具体代码示例和操作截图,帮助读者快速上手。最终,将自定义SSE MCP服务集成到百炼平台,完成智能体应用的创建与测试。适合希望了解SSE实时交互及大模型集成的开发者参考。
12067 60
|
4月前
|
Prometheus 监控 Cloud Native
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务
Docker 部署 Prometheus 和 Grafana 监控 Spring Boot 服务实现步骤
523 0
|
8月前
|
人工智能 自然语言处理 Java
对话即服务:Spring Boot整合MCP让你的CRUD系统秒变AI助手
本文介绍了如何通过Model Context Protocol (MCP) 协议将传统Spring Boot服务改造为支持AI交互的智能系统。MCP作为“万能适配器”,让AI以统一方式与多种服务和数据源交互,降低开发复杂度。文章以图书管理服务为例,详细说明了引入依赖、配置MCP服务器、改造服务方法(注解方式或函数Bean方式)及接口测试的全流程。最终实现用户通过自然语言查询数据库的功能,展示了MCP在简化AI集成、提升系统易用性方面的价值。未来,“对话即服务”有望成为主流开发范式。
6118 7
|
9月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
621 7
|
11月前
|
Java 关系型数据库 Nacos
微服务SpringCloud链路追踪之Micrometer+Zipkin
SpringCloud+Openfeign远程调用,并用Mircrometer+Zipkin进行链路追踪
1372 20
|
11月前
|
消息中间件 监控 Java
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + RabbitMQ应用程序部署到Pivotal Cloud Foundry (PCF)
261 6
|
11月前
|
Java 关系型数据库 MySQL
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot + MySQL应用程序部署到Pivotal Cloud Foundry (PCF)
192 5
|
11月前
|
缓存 监控 Java
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
如何将Spring Boot应用程序部署到Pivotal Cloud Foundry (PCF)
229 5