轉(zhuǎn)載請(qǐng)標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/70162074
本文出自方志朋的博客
這篇文章主要講述服務(wù)追蹤組件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.
—— 摘自官網(wǎng)
Spring Cloud Sleuth 主要功能就是在分布式系統(tǒng)中提供追蹤解決方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相應(yīng)的依賴(lài)即可。
微服務(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ù)雜。
隨著服務(wù)的越來(lái)越多,對(duì)調(diào)用鏈的分析會(huì)越來(lái)越復(fù)雜。它們之間的調(diào)用關(guān)系也許如下:
將Span和Trace在一個(gè)系統(tǒng)中使用Zipkin注解的過(guò)程圖形化:
基本知識(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ù)追蹤了。
建一個(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@EnableZipkinServerpublic class ServerZipkinApplication { public static void main(String[] args) { SpringApplication.run(ServerZipkinApplication.class, args); }}
指定端口:
server.port=9411
在其pom引入依賴(lài):
<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=8988spring.zipkin.base-url=http://localhost:9411spring.application.name=service-hi
通過(guò)引入spring-cloud-starter-zipkin依賴(lài)和設(shè)置spring.zipkin.base-url就可以了。
對(duì)外暴露接口:
@SpringBootApplication@RestControllerpublic 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(); }}
創(chuàng)建過(guò)程痛service-hi,引入相同的依賴(lài),配置下spring.zipkin.base-url。
對(duì)外暴露接口:
@SpringBootApplication@RestControllerpublic 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(); }}
依次啟動(dòng)上面的三個(gè)工程,打開(kāi)瀏覽器訪問(wèn):http://localhost:9411/,會(huì)出現(xiàn)以下界面:
訪問(wèn):http://localhost:8989/miya,瀏覽器出現(xiàn):
i’m service-hi
再打開(kāi)http://localhost:9411/的界面,點(diǎn)擊Dependencies,可以發(fā)現(xiàn)服務(wù)的依賴(lài)關(guān)系:
點(diǎn)擊find traces,可以看到具體服務(wù)相互調(diào)用的數(shù)據(jù):
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter9
利用Zipkin對(duì)Spring Cloud應(yīng)用進(jìn)行服務(wù)追蹤分析
聯(lián)系客服