十、Eureka: 服务链路追踪Sleuth

1、简介

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

2、服务追踪分析

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

3、术语

Span:基本工作单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span通过一个64位ID唯一标识,trace以另一个64位ID表示,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(通常是IP地址)
span在不断的启动和停止,同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。
Trace:一系列spans组成的一个树状结构,例如,如果你正在跑一个分布式大数据工程,你可能需要创建一个trace。
Annotation:用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束

cs - Client Sent -客户端发起一个请求,这个annotion描述了这个span的开始
sr - Server Received -服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络延迟
ss - Server Sent -注解表明请求处理的完成(当请求返回客户端),如果ss减去sr时间戳便可得到服务端需要的处理请求时间
cr - Client Received -表明span的结束,客户端成功接收到服务端的回复,如果cr减去cs时间戳便可得到客户端从服务端获取回复的所有所需时间
将Span和Trace在一个系统中使用Zipkin注解的过程图形化:
在这里插入图片描述

4、构建工程

三个工程组成:一个server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示;一个service-hello,对外暴露hello接口;一个service-miya,对外暴露miya接口;这两个service可以相互调用;并且只有调用了,server-zipkin才会收集数据的,这就是为什么叫服务追踪了。

4.1 构建server-zipkin

在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可

C:
Cd C:\Users\zhoudoujun01\Downloads
java -jar zipkin-server-2.10.1-exec.jar
访问浏览器localhost:9494

4.2 创建service-hello

A.pom引入起步依赖spring-cloud-starter-zipkin
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

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

	</dependencies>

B.配置文件application.properties指定zipkin server的地址

头通过配置“spring.zipkin.base-url”指定:

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

D.对外暴露接口:

@SpringBootApplication
@RestController
public class ServiceHelloApplication {
	private static final Logger LOG = LoggerFactory.getLogger(ServiceHelloApplication.class.getName());
	
	public static void main(String[] args) {
		SpringApplication.run(ServiceHelloApplication.class, args);
	}

	


	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

	@RequestMapping("/hello")
	public String callHome(){
		LOG.info("calling trace service-hello  ");
		return restTemplate.getForObject("http://localhost:8989/miya", String.class);
	}
	
	
	@RequestMapping("/info")
	public String info(){
		LOG.info("calling trace service-hello ");
		return "i'm service-hello";

	}

	@Bean
	public Sampler defaultSampler() {
		return Sampler.ALWAYS_SAMPLE;
	}


}

}

4.3 创建service-miya

创建过程同service-hello,引入相同的依赖,配置下spring.zipkin.base-url。
对外暴露接口:

@SpringBootApplication
@RestController
public class ServiceMiyaApplication {

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

	private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName());


	@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);
	}

	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}


	@Bean
	public Sampler defaultSampler() {
		return Sampler.ALWAYS_SAMPLE;
	}
}

4.4 启动工程,演示追踪

a.依次启动上面的工程service-hello、service-miya,打开浏览器访问:http://localhost:9411/,会出现以下界面
在这里插入图片描述

b.访问:http://localhost:8989/miya,浏览器出现:
i’m service-hello
再打开http://localhost:9411/的界面,点击Dependencies,可以发现服务的依赖关系:
在这里插入图片描述
点击find traces,可以看到具体服务相互调用的数据:
在这里插入图片描述
c.再访问:http://localhost:8980/hello,浏览器出现:
i’m service-hello
在这里插入图片描述
点击find traces,可以看到具体服务相互调用的数据:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值