這篇文章主要講述服務追蹤組件zipkin,Spring Cloud Sleuth集成了zipkin組件。java
1、簡介web
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
------ 摘自官網apache
Spring Cloud Sleuth 主要功能就是在分佈式系統中提供追蹤解決方案,而且兼容支持了 zipkin,你只須要在pom文件中引入相應的依賴便可。瀏覽器
2、服務追蹤分析
微服務架構上經過業務來劃分服務的,瞭解springcloud架構能夠加求求:三五三六二四七二五九,經過REST調用,對外暴露的一個接口,可能須要不少個服務協同才能完成這個接口功能,若是鏈路上任何一個服務出現問題或者網絡超時,都會造成致使接口調用失敗。隨着業務的不斷擴張,服務之間互相調用會愈來愈複雜。
網絡
隨着服務的愈來愈多,對調用鏈的分析會愈來愈複雜。架構
3、術語
Span:基本工做單元,例如,在一個新建的span中發送一個RPC等同於發送一個迴應請求給RPC,span經過一個64位ID惟一標識,trace以另外一個64位ID表示,span還有其餘數據信息,好比摘要、時間戳事件、關鍵值註釋(tags)、span的ID、以及進度ID(一般是IP地址)span在不斷的啓動和中止,同時記錄了時間信息,當你建立了一個span,你必須在將來的某個時刻中止它。app
4.1 構建server-zipkin
在spring Cloud爲F版本的時候,已經不須要本身構建Zipkin Server了,只須要下載jar便可,下載地址:maven
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,代碼以下:
<?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>
在其配置文件application.yml指定zipkin server的地址,頭經過配置「spring.zipkin.base-url」指定:
server.port=8988 spring.zipkin.base-url=http://localhost:9411 spring.application.name=service-hi
經過引入spring-cloud-starter-zipkin依賴和設置spring.zipkin.base-url就能夠了。
對外暴露接口:
package com.forezp; 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; } }
4.3 建立service-miya
建立過程痛service-hi,引入相同的依賴,配置下spring.zipkin.base-url。
對外暴露接口:
package com.forezp; 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; } }
4.4 啓動工程,演示追蹤
依次啓動上面的工程,打開瀏覽器訪問:http://localhost:9411/,會出現如下界面:
訪問:http://localhost:8989/miya,瀏覽器出現:
i’m service-hi
再打開http://localhost:9411/的界面,點擊Dependencies,能夠發現服務的依賴關係:
點擊find traces,能夠看到具體服務相互調用的數據: