這篇文章主要講述服務追蹤組件zipkin,Spring Cloud Sleuth集成了zipkin組件。html
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.java
—— 摘自官網git
Spring Cloud Sleuth 主要功能就是在分佈式系統中提供追蹤解決方案,而且兼容支持了 zipkin,你只須要在pom文件中引入相應的依賴便可。github
微服務架構上經過業務來劃分服務的,經過REST調用,對外暴露的一個接口,可能須要不少個服務協同才能完成這個接口功能,若是鏈路上任何一個服務出現問題或者網絡超時,都會造成致使接口調用失敗。隨着業務的不斷擴張,服務之間互相調用會愈來愈複雜。web
隨着服務的愈來愈多,對調用鏈的分析會愈來愈複雜。它們之間的調用關係也許以下:spring
將Span和Trace在一個系統中使用Zipkin註解的過程圖形化:apache
基本知識講解完畢,下面咱們來實戰,本文的案例主要有三個工程組成:一個server-zipkin,它的主要做用使用ZipkinServer 的功能,收集調用數據,並展現;一個service-hi,對外暴露hi接口;一個service-miya,對外暴露miya接口;這兩個service能夠相互調用;而且只有調用了,server-zipkin纔會收集數據的,這就是爲何叫服務追蹤了。瀏覽器
4.1 構建server-zipkin微信
在spring Cloud爲F版本的時候,已經不須要本身構建Zipkin Server了,只須要下載jar便可,下載地址:網絡
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
也能夠在這裏下載:
連接: https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ 密碼: 26pf
下載完成jar 包以後,須要運行jar,以下:
java -jar zipkin-server-2.10.1-exec.jar
訪問瀏覽器localhost:9494
4.2 建立service-hi
在其pom引入起步依賴spring-cloud-starter-zipkin,代碼以下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.forezp</groupId> 7 <artifactId>service-zipkin</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>service-hi</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>com.forezp</groupId> 16 <artifactId>sc-f-chapter9</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </parent> 19 20 21 22 <dependencies> 23 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-zipkin</artifactId> 32 </dependency> 33 34 35 36 </dependencies> 37 38 <build> 39 <plugins> 40 <plugin> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-maven-plugin</artifactId> 43 </plugin> 44 </plugins> 45 </build> 46 47 </project>
在其配置文件application.yml指定zipkin server的地址,頭經過配置「spring.zipkin.base-url」指定:
1 server.port=8988 2 spring.zipkin.base-url=http://localhost:9411 3 spring.application.name=service-hi
經過引入spring-cloud-starter-zipkin依賴和設置spring.zipkin.base-url就能夠了。
對外暴露接口:
1 package com.forezp; 2 3 import brave.sampler.Sampler; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.SpringApplication; 6 import org.springframework.boot.autoconfigure.SpringBootApplication; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 import org.springframework.web.client.RestTemplate; 11 import java.util.logging.Level; 12 import java.util.logging.Logger; 13 14 @SpringBootApplication 15 @RestController 16 public class ServiceHiApplication { 17 18 public static void main(String[] args) { 19 SpringApplication.run(ServiceHiApplication.class, args); 20 } 21 22 private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName()); 23 24 25 @Autowired 26 private RestTemplate restTemplate; 27 28 @Bean 29 public RestTemplate getRestTemplate(){ 30 return new RestTemplate(); 31 } 32 33 @RequestMapping("/hi") 34 public String callHome(){ 35 LOG.log(Level.INFO, "calling trace service-hi "); 36 return restTemplate.getForObject("http://localhost:8989/miya", String.class); 37 } 38 @RequestMapping("/info") 39 public String info(){ 40 LOG.log(Level.INFO, "calling trace service-hi "); 41 42 return "i'm service-hi"; 43 44 } 45 46 @Bean 47 public Sampler defaultSampler() { 48 return Sampler.ALWAYS_SAMPLE; 49 } 50 51 52 }
4.3 建立service-miya
建立過程痛service-hi,引入相同的依賴,配置下spring.zipkin.base-url。
對外暴露接口:
1 2 package com.forezp; 3 4 import brave.sampler.Sampler; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.SpringApplication; 7 import org.springframework.boot.autoconfigure.SpringBootApplication; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 import org.springframework.web.client.RestTemplate; 12 13 import java.util.logging.Level; 14 import java.util.logging.Logger; 15 16 @SpringBootApplication 17 @RestController 18 public class ServiceMiyaApplication { 19 20 public static void main(String[] args) { 21 SpringApplication.run(ServiceMiyaApplication.class, args); 22 } 23 24 private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName()); 25 26 27 @RequestMapping("/hi") 28 public String home(){ 29 LOG.log(Level.INFO, "hi is being called"); 30 return "hi i'm miya!"; 31 } 32 33 @RequestMapping("/miya") 34 public String info(){ 35 LOG.log(Level.INFO, "info is being called"); 36 return restTemplate.getForObject("http://localhost:8988/info",String.class); 37 } 38 39 @Autowired 40 private RestTemplate restTemplate; 41 42 @Bean 43 public RestTemplate getRestTemplate(){ 44 return new RestTemplate(); 45 } 46 47 48 @Bean 49 public Sampler defaultSampler() { 50 return Sampler.ALWAYS_SAMPLE; 51 } 52 }
4.4 啓動工程,演示追蹤
依次啓動上面的工程,打開瀏覽器訪問:http://localhost:9411/,會出現如下界面:
訪問:http://localhost:8989/miya,瀏覽器出現:
i’m service-hi
再打開http://localhost:9411/的界面,點擊Dependencies,能夠發現服務的依賴關係:
點擊find traces,能夠看到具體服務相互調用的數據:
本文源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter9
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
感謝您的關注!可加QQ1羣:135430763,QQ2羣:454796847,QQ3羣:187424846。QQ羣進羣密碼:xttblog,想加微信羣的朋友,能夠微信搜索:xmtxtt,備註:「xttblog」,添加助理微信拉你進羣。備註錯誤不會贊成好友申請。再次感謝您的關注!後續有精彩內容會第一時間發給您!原創文章投稿請發送至532009913@qq.com郵箱。商務合做可添加助理微信進行溝通!