分布式服务跟踪 spring-cloud-Sleuth集成zipkin

通常一个由客户端发起的请求在后端系统中会经过多个不同的微服务调用来协同产生最后的请求结果,在复杂的微服务架构系统中,几乎每一个前端请求都会形成一条复杂的分布式服务调用链路,在每条链路中任何一个依赖服务出现延迟过高或错误的时候都有可能引起请求最后的失败。这时候,对于每个请求,全链路调用的跟踪就变得越来越重要,通过实现对请求调用的跟踪可以帮助我们快速发现错误根源以及监控分析每条请求链路上的性能瓶颈等spring cloud sleuth 为spring cloud 提供了分布式跟踪的解决方案,它大量借用了google dapper, twitter zipkin 和 apache htrace的设计

当我们的应用 引入

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-sleuth</artifactId>

</dependency>

依赖后,所打印的信息就会带有 INFO [sleuthone,f6b6c9e59b7d9969,f6b6c9e59b7d9969,false] 的格式

其中 类似于 [sleuthtwo,e129ff9beafe793e,56dda5882c3d1057,false] 是 sleuth作用的结果

第一个值:sleuthtwo是应用名称,也就是 spring.application.name参数配置的属性

第二个值:e129ff9beafe793e,spring cloud sleuth生成的一个ID,称为Trace ID,它用来标识一条请求链路。一条请求链路中包含一个trace id,多个span id

第三个值:56dda5882c3d1057,spring cloud sleuth生成的另外一个ID,称为span id,它表示一个基本的工作单元,比如发送一个HTTP请求,对于每个span来说,它必须有开始和结束两个节点,通过记录开始span和结束span的时间戳,就能统计出该span的时间延迟,除了时间戳记录之外,它还可以包含一些其他元数据,比如事件名称,请求信息等

第四个值:false,表示是否要将该信息输出到zipkin等服务中来收集和展示

在一个服务请求链路的调用过程,会保持并传递同一个trace id,从而将整个分布式不同微服务进程中的请求跟踪信息串联起来

spring cloud sleuth 集成 zipkin

版本 spring boot 1.5.14

eureka 注册中心

配置文件

server.port=1111
eureka.instance.hostname=localhost
#由于该应用为注册中心,所有设置为false,代表不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

启动类

package springcloud.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer // 注解启动一个服务注册中心
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

pom 文件

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

zipkin 服务

创建一个基础的spring boot应用,并在pom.xml 中引入zipkin的相关依赖

配置文件

spring.application.name=zipkin-server
server.port=9411

启动类

package springcloud.zipkinserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.internal.EnableZipkinServer;


@EnableZipkinServer
@SpringBootApplication
public class ZipkinserverApplication {

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

pom 文件

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

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

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

sleuth 应用一

配置文件

spring.application.name=sleuthone
server.port=9101
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

controller

package springcloud.sleuth_one;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class SleuthOneController {
private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
protected RestTemplate restTemplate;
@RequestMapping(value = "/trace-1",method = RequestMethod.GET)
public String trace(){
logger.info("=====call trace-1=======");
return restTemplate.getForEntity("http://sleuthtwo/trace-2",String.class).getBody();
}
}

启动类

package springcloud.sleuth_one;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class SleuthOneApplication {


@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}

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

pom 文件

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

sleuth 应用二

配置文件

spring.application.name=sleuthtwo
server.port=9102
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

controller

package springcloud.sleuth_two;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SleuthTwoController {
private Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping(value = "/trace-2",method = RequestMethod.GET)
public String trace(){
logger.info("========<call trace-2>=======");
throw new RuntimeException();

//return "Trace";
}

}

启动类

package springcloud.sleuth_two;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class SleuthTwoApplication {

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

pom 依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

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


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

访问几次 http://localhost:9101/trace-1 后,直到后台 最后一个参数为true为止

返回 zipkin server的ui http://localhost:9411/

猜你喜欢

转载自blog.csdn.net/weixin_39639119/article/details/81629464