Spring Cloud 学习系列:(十五)微服务跟踪链——Spring Cloud Sleuth

一、简介

Spring Cloud SleuthSpring Cloud 提供了分布式跟踪的解决方案,它大量借用了 Google DapperTwitter ZipkinApache HTrace 的设计。

先来了解一下 Sleuth的术语,Sleuth借用了 Dapper的术语。

  • Span:基本工作单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span通过一个64位ID唯一标识,trace以另一个64位ID表示,span还有其他数据信息,比如摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(通常是IP地址)
    span在不断的启动和停止,同时记录了时间信息,当你创建了一个span,你必须在未来的某个时刻停止它。

  • Trace:一系列spans组成的一个树状结构,例如,如果你正在跑一个分布式大数据工程,你可能需要创建一个trace。

  • Annotation:用来及时记录一个事件的存在,一些核心annotations用来定义一个请求的开始和结束

    1、cs - Client Sent -客户端发起一个请求,这个annotion描述了这个span的开始
    2、sr - Server Received -服务端获得请求并准备开始处理它,如果将其sr减去cs时间戳便可得到网络延迟
    3、ss - Server Sent -注解表明请求处理的完成(当请求返回客户端),如果ss减去sr时间戳便可得到服务端需要的处理请求时间
    4、cr - Client Received -表明span的结束,客户端成功接收到服务端的回复,如果cr减去cs时间戳便可得到客户端从服务端获取回复的所有所需时间

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

在这里插入图片描述

二、整合 Spring Cloud Sleuth

本节由之前的 Spring Cloud 学习系列:(二)微服务的注册与发现——Eureka 改造而来。

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

1、在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可,下载地址:
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

下载完成jar 包之后,需要运行jar,如下:
下载后放入lib中便于以后使用
右键点击+shift如下:
在这里插入图片描述

java -jar zipkin-server-2.12.2-exec.jar

显示如下则成功
在这里插入图片描述

访问浏览器 localhost:9411

在这里插入图片描述
2、microservice-service-zipkin-hi

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

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.riemann</groupId>
		<artifactId>microservice-sleuth-zipkin</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.riemann</groupId>
	<artifactId>microservice-service-zipkin-hi</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>microservice-service-zipkin-hi</name>
	<description>service-zipkin-hi</description>

	<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>

</project>

(2)、在其配置文件application.properties指定zipkin server的地址,通过配置spring.zipkin.base-url指定:
通过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就可以了。

(3)、对外暴露接口:

package com.riemann.microserviceproviderservicehi;

import java.util.logging.Level;
import java.util.logging.Logger;
import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
public class MicroserviceProviderServiceHiApplication {

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

	private static final Logger logger = Logger.getLogger(MicroserviceProviderServiceHiApplication.class.getName());

	@Autowired
	private RestTemplate restTemplate;

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

	@RequestMapping("/hi")
	public String callName() {
		logger.log(Level.INFO,"calling trace microservice-service-zipkin-hi");
		return restTemplate.getForObject("http://localhost:8989/riemann", String.class);
	}

	@RequestMapping("/info")
	public String info() {
		logger.log(Level.INFO,"calling trace microservice-service-zipkin-hi");
		return "I'm microservice-service-zipkin-hi";
	}

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

3、microservice-service-zipkin-riemann

(1)、创建过程如同 microservice-service-zipkin-hi 引入相同的依赖,配置下spring.zipkin.base-url

(2)、对外暴露接口:

package com.riemann.microserviceproviderserviceriemann;

import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.logging.Level;
import java.util.logging.Logger;

@SpringBootApplication
@RestController
public class MicroserviceProviderServiceRiemannApplication {

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

    private static final Logger logger = Logger.getLogger(MicroserviceProviderServiceRiemannApplication.class.getName());

    @Autowired
    private RestTemplate restTemplate;

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

    @RequestMapping("/hi")
    public String callName() {
        logger.log(Level.INFO,"hi is being called");
        return "hi, I'm riemann";
    }

    @RequestMapping("/riemann")
    public String info() {
        logger.log(Level.INFO,"info is being called");
        return restTemplate.getForObject("http://localhost:8988/info",String.class);
    }

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

4、启动工程,演示追踪

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

在这里插入图片描述

(2)、访问:http://localhost:8989/riemann,浏览器出现:

I’m microservice-service-zipkin-hi

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

在这里插入图片描述

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

在这里插入图片描述

发布了332 篇原创文章 · 获赞 198 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/riemann_/article/details/103518802