Spring Cloud如何实现链路追踪Sleuth

发布时间:2021-12-24 10:42:25 作者:小新
来源:亿速云 阅读:269

这篇文章给大家分享的是有关Spring Cloud如何实现链路追踪Sleuth的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、简介

Add sleuth to the classpath of a Spring Boot application (see below for Maven and Gradle examples), and you will see the correlation data being collected in logs, as long as you are logging requests.

—— 摘自官网

Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可。

二、服务追踪分析

微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。

Spring Cloud如何实现链路追踪Sleuth

随着服务的越来越多,对调用链的分析会越来越复杂。它们之间的调用关系也许如下:

Spring Cloud如何实现链路追踪Sleuth三、术语

将Span和Trace在一个系统中使用Zipkin注解的过程图形化:

Spring Cloud如何实现链路追踪Sleuth四、构建工程

基本知识讲解完毕,下面我们来实战,本文的案例主要有三个工程组成:一个server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示;一个service-hi,对外暴露hi接口;一个service-miya,对外暴露miya接口;这两个service可以相互调用;并且只有调用了,server-zipkin才会收集数据的,这就是为什么叫服务追踪了。

4.1 构建server-zipkin

建一个spring-boot工程取名为server-zipkin,在其pom引入依赖:

<dependency>
	<groupId>io.zipkin.java</groupId>
	<artifactId>zipkin-server</artifactId>
</dependency>

<dependency>
	<groupId>io.zipkin.java</groupId>
	<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>

在其程序入口类, 加上注解@EnableZipkinServer,开启ZipkinServer的功能:

@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServerZipkinApplication.class, args);
	}
}

在配置文件application.yml指定服务端口为:

server.port=9411
4.2 创建service-hi

在其pom引入起步依赖spring-cloud-starter-zipkin,代码如下:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

在其配置文件application.yml指定zipkin server的地址,头通过配置“spring.zipkin.base-url”指定:

server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi

通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了。

对外暴露接口:

@RequestMapping("/hi")
	public String callHome(){
		LOG.log(Level.INFO, "calling trace service-hi  ");
		return restTemplate.getForObject("http://localhost:8989/miya", String.class);
	}
	@RequestMapping("/info")
	public String info(){
		LOG.log(Level.INFO, "calling trace service-hi ");

		return "i'm service-hi";
	}
4.3 创建service-miya

创建过程痛service-hi,引入相同的依赖,配置下spring.zipkin.base-url。

对外暴露接口:

@RequestMapping("/hi")
	public String home(){
		LOG.log(Level.INFO, "hi is being called");
		return "hi i'm miya!";
	}

	@RequestMapping("/miya")
	public String info(){
		LOG.log(Level.INFO, "info is being called");
		return restTemplate.getForObject("http://localhost:8988/info",String.class);
	}
4.4 启动工程,演示追踪

依次启动上面的三个工程,打开浏览器访问:http://localhost:9411/,会出现以下界面:

Spring Cloud如何实现链路追踪Sleuth

访问:http://localhost:8989/miya,浏览器出现:

i’m service-hi

再打开http://localhost:9411/的界面,点击Dependencies,可以发现服务的依赖关系:

Spring Cloud如何实现链路追踪Sleuth

点击find traces,可以看到具体服务相互调用的数据:

Spring Cloud如何实现链路追踪Sleuth

感谢各位的阅读!关于“Spring Cloud如何实现链路追踪Sleuth”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. Spring Cloud 微服务开发系列整理
  2. 从架构演进的角度聊聊Spring Cloud都做了些什么?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring cloud sleuth

上一篇:IKEA.com本地文件包含漏洞以及PDF解析的巧妙利用是怎样的

下一篇:linux中如何删除用户组

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》