1. 程式人生 > >spring cloud 服務鏈路追蹤

spring cloud 服務鏈路追蹤

簡介

Spring cloud Sleuth主要功能就是在分散式系統中提供追蹤解決方案,並且相容支援zipkin,你只需要在pom檔案中引入相應的依賴即可。

1、span

基本工作單元,span在不斷的啟動和停止,同時記錄了時間資訊,當你建立一相span,你必須在未來的某個時刻停止它。

2、trace

一系列span組成的一個樹狀結構。

3、annotation

用來及時記錄一個事件的存在。

(1)cs,client sent,

客戶發起一個請求,描述了這個span的開始。

(2)sr,server received

服務端獲得請求並準備開始處理它,sr-cs時間戳便可得到網路延遲。

(3)ss,server sent

請求返回客戶端,ss-sr時間戳可得到服務端需要的處理請求時間。

(4)cr , client received

表示span的結束,客戶端成功接收到服務端的回覆。

實戰

主要由三個工程組成:一個server-zipkin,它的主要作用是使用ZipKinServer的功能,收集資料,並展示,一個server-hi對外暴露hi介面,一個service-miya,對外暴露miya介面,這兩個service可以相互呼叫。

1、構建server-zipkin

(1)下載jar包,https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/

(2)執行

java -jar zipkin-server-2.10.1-exec.jar

(3)訪問localhost:9411

2、建立service-hi

(1)pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.forezp</groupId>
	<artifactId>service-zipkin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>service-hi</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.forezp</groupId>
		<artifactId>sc-f-chapter9</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>



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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

(2)application.yml

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

(3)對外介面

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

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

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


	@Autowired
	private RestTemplate restTemplate;

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

	@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";

	}

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


}

3、建立service-miya

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

依次啟動上面的工程,訪問http://localhost:8989/miya,檢視http://localhost:9411/