電子商務(wù)社交平臺(tái)源碼請(qǐng)加企鵝求求:一零三八七七四六二六。這篇文章主要講述服務(wù)追蹤組件zipkin,Spring Cloud Sleuth集成了zipkin組件。
一、簡(jiǎn)介
Spring cloud B2B2C電子商務(wù)平臺(tái)源碼請(qǐng)加企鵝求求:一零三八七七四六二六。Spring Cloud Sleuth 主要功能就是在分布式系統(tǒng)中提供追蹤解決方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相應(yīng)的依賴(lài)即可。
二、服務(wù)追蹤分析
微服務(wù)架構(gòu)上通過(guò)業(yè)務(wù)來(lái)劃分服務(wù)的,通過(guò)REST調(diào)用,對(duì)外暴露的一個(gè)接口,可能需要很多個(gè)服務(wù)協(xié)同才能完成這個(gè)接口功能,如果鏈路上任何一個(gè)服務(wù)出現(xiàn)問(wèn)題或者網(wǎng)絡(luò)超時(shí),都會(huì)形成導(dǎo)致接口調(diào)用失敗。隨著業(yè)務(wù)的不斷擴(kuò)張,服務(wù)之間互相調(diào)用會(huì)越來(lái)越復(fù)雜。
[圖片上傳失敗...(image-9a1437-1558681190276)]
隨著服務(wù)的越來(lái)越多,對(duì)調(diào)用鏈的分析會(huì)越來(lái)越復(fù)雜。它們之間的調(diào)用關(guān)系也許如下:
三、術(shù)語(yǔ)
- Span:基本工作單元,例如,在一個(gè)新建的span中發(fā)送一個(gè)RPC等同于發(fā)送一個(gè)回應(yīng)請(qǐng)求給RPC,span通過(guò)一個(gè)64位ID唯一標(biāo)識(shí),trace以另一個(gè)64位ID表示,span還有其他數(shù)據(jù)信息,比如摘要、時(shí)間戳事件、關(guān)鍵值注釋(tags)、span的ID、以及進(jìn)度ID(通常是IP地址)
span在不斷的啟動(dòng)和停止,同時(shí)記錄了時(shí)間信息,當(dāng)你創(chuàng)建了一個(gè)span,你必須在未來(lái)的某個(gè)時(shí)刻停止它。 - Trace:一系列spans組成的一個(gè)樹(shù)狀結(jié)構(gòu),例如,如果你正在跑一個(gè)分布式大數(shù)據(jù)工程,你可能需要?jiǎng)?chuàng)建一個(gè)trace。
- Annotation:用來(lái)及時(shí)記錄一個(gè)事件的存在,一些核心annotations用來(lái)定義一個(gè)請(qǐng)求的開(kāi)始和結(jié)束
- cs - Client Sent -客戶(hù)端發(fā)起一個(gè)請(qǐng)求,這個(gè)annotion描述了這個(gè)span的開(kāi)始
- sr - Server Received -服務(wù)端獲得請(qǐng)求并準(zhǔn)備開(kāi)始處理它,如果將其sr減去cs時(shí)間戳便可得到網(wǎng)絡(luò)延遲
- ss - Server Sent -注解表明請(qǐng)求處理的完成(當(dāng)請(qǐng)求返回客戶(hù)端),如果ss減去sr時(shí)間戳便可得到服務(wù)端需要的處理請(qǐng)求時(shí)間
- cr - Client Received -表明span的結(jié)束,客戶(hù)端成功接收到服務(wù)端的回復(fù),如果cr減去cs時(shí)間戳便可得到客戶(hù)端從服務(wù)端獲取回復(fù)的所有所需時(shí)間
將Span和Trace在一個(gè)系統(tǒng)中使用Zipkin注解的過(guò)程圖形化:
將Span和Trace在一個(gè)系統(tǒng)中使用Zipkin注解的過(guò)程圖形化:
[圖片上傳失敗...(image-b11961-1558681190275)]
四、構(gòu)建工程
基本知識(shí)講解完畢,下面我們來(lái)實(shí)戰(zhàn),本文的案例主要有三個(gè)工程組成:一個(gè)server-zipkin,它的主要作用使用ZipkinServer 的功能,收集調(diào)用數(shù)據(jù),并展示;一個(gè)service-hi,對(duì)外暴露hi接口;一個(gè)service-miya,對(duì)外暴露miya接口;這兩個(gè)service可以相互調(diào)用;并且只有調(diào)用了,server-zipkin才會(huì)收集數(shù)據(jù)的,這就是為什么叫服務(wù)追蹤了。
4.1 構(gòu)建server-zipkin
建一個(gè)spring-boot工程取名為server-zipkin,在其pom引入依賴(lài):
<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>
在其程序入口類(lèi), 加上注解@EnableZipkinServer,開(kāi)啟ZipkinServer的功能:
@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ServerZipkinApplication.class, args);
}
}
在配置文件application.yml指定服務(wù)端口為:
server.port=9411
4.2 創(chuàng)建service-hi
在其pom引入起步依賴(lài)spring-cloud-starter-zipkin,代碼如下:
<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>
在其配置文件application.yml指定zipkin server的地址,頭通過(guò)配置“spring.zipkin.base-url”指定:
server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi
通過(guò)引入spring-cloud-starter-zipkin依賴(lài)和設(shè)置spring.zipkin.base-url就可以了。
對(duì)外暴露接口:
@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 AlwaysSampler defaultSampler(){
return new AlwaysSampler();
}
}
需要JAVASpring Cloud大型企業(yè)分布式微服務(wù)云構(gòu)建的B2B2C電子商務(wù)平臺(tái)源碼請(qǐng)加企鵝求求:一零三八七七四六二六