服務鏈路追蹤(Spring Cloud Sleuth)
這篇文章主要講述服務追蹤組件zipkin,spring Cloud Sleuth集成了zipkin組件。
一、簡介
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調用,對外暴露的一個接口,可能需要很多個服務協同才能完成這個接口功能,如果鏈路上任何一個服務出現問題或者網絡超時,都會形成導致接口調用失敗。隨著業務的不斷擴張,服務之間互相調用會越來越複雜。
隨著服務的越來越多,對調用鏈的分析會越來越複雜。它們之間的調用關係也許如下:
三、術語
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注解的過程圖形化:
將Span和Trace在一個係統中使用Zipkin注解的過程圖形化:
四、構建工程
基本知識講解完畢,下麵我們來實戰,本文的案例主要有三個工程組成:一個server-zipkin,它的主要作用使用ZipkinServer 的功能,收集調用數據,並展示;一個service-hi,對外暴露hi接口;一個service-miya,對外暴露miya接口;這兩個service可以相互調用;並且隻有調用了,server-zipkin才會收集數據的,這就是為什麼叫服務追蹤了。
4.1 構建server-zipkin
建一個spring-boot工程取名為server-zipkin,在其pom引入依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在其程序入口類, 加上注解@EnableZipkinServer,開啟ZipkinServer的功能:
@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ServerZipkinApplication.class, args);
}
}
指定端口:
server.port=9411
4.2 創建service-hi
在其pom引入依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--compile('org.springframework.cloud:spring-cloud-starter-zipkin')-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在其配置文件指定“spring.zipkin.base-url”:
server.port=8988
spring.zipkin.base-url=https://localhost:9411
spring.application.name=service-hi
通過引入spring-cloud-starter-zipkin依賴和設置spring.zipkin.base-url就可以了。
對外暴露接口:
@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("https://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 AlwaysSampler defaultSampler(){
return new AlwaysSampler();
}
}
4.3 創建service-miya
創建過程痛service-hi,引入相同的依賴,配置下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("https://localhost:8988/info",String.class);
}
@Autowired
private RestTemplate restTemplate;
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
4.4 啟動工程,演示追蹤
依次啟動上麵的三個工程,打開瀏覽器訪問:https://localhost:9411/,會出現以下界麵:
訪問:https://localhost:8989/miya,瀏覽器出現:
i’m service-hi
再打開https://localhost:9411/的界麵,點擊Dependencies,可以發現服務的依賴關係:
點擊find traces,可以看到具體服務相互調用的數據:
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter9
文章來源:https://blog.csdn.net/forezp/article/details/70162074
最後更新:2017-08-17 17:32:33