服務化改造實踐(三) | Dubbo + Zipkin
隨著業務的發展,應用的規模不斷的擴大,傳統的應用架構無法滿足訴求,服務化架構改造勢在必行,以 Dubbo 為代表的分散式服務框架成為了服務化改造架構中的基石。隨著微服務理念逐漸被大眾接受,應用進一步向更細粒度拆分,並且,不同的應用由不同的開發團隊獨立負責,整個分散式系統變得十分複雜。沒有人能夠清晰及時的知道當前系統整體的依賴關係。當出現問題時,也無法及時知道具體是鏈路上的哪個環節出了問題。
在這個背景下,Google 發表了 ofollow,noindex" target="_blank">Dapper 的論文,描述瞭如何通過一個分散式追蹤系統解決上述問題。基於該論文,各大網際網路公司實現並部署了自己的分散式追蹤系統,其中比較出名的有阿里巴巴的 EagleEye。本文中提到的 Zipkin 是 Twitter 公司開源的分散式追蹤系統。下面會詳細介紹如何在 Dubbo 中使用 Zipkin 來實現分散式追蹤。
Zipkin 簡介
Zipkin 是基於 Dapper 論文實現,由 Twitter 開源的分散式追蹤系統,通過收集分散式服務執行時間的資訊來達到追蹤服務呼叫鏈路、以及分析服務執行延遲等目的。
Zipkin 架構
Collector 收集器、Storage 儲存、API、UI 使用者介面等幾部分構成了 Zipkin Server 部分,對應於 GitHub 上 openzipkin/zipkin 這個專案。而收集應用中呼叫的耗時資訊並將其上報的元件與應用共生,並擁有各個語言的實現版本,其中 Java 的實現是 GitHub 上 openzipkin/brave 。除了 Java 客戶端實現之外,openzipkin 還提供了許多其他語言的實現,其中包括了 go、php、JavaScript、.net、ruby 等,具體列表可以參閱 Zipkin 的 Exiting instrumentations 。
Zipkin 的工作過程
當用戶發起一次呼叫時,Zipkin 的客戶端會在入口處為整條呼叫鏈路生成一個全域性唯一的 trace id,併為這條鏈路中的每一次分散式呼叫生成一個 span id。span 與 span 之間可以有父子巢狀關係,代表分散式呼叫中的上下游關係。span 和 span 之間可以是兄弟關係,代表當前呼叫下的兩次子呼叫。一個 trace 由一組 span 組成,可以看成是由 trace 為根節點,span 為若干個子節點的一棵樹。
Span 由呼叫邊界來分隔,在 Zipkin 中,呼叫邊界由以下四個 annotation 來表示:
- cs - Clent Sent 客戶端傳送了請求
- sr - Server Receive 服務端接受到請求
- ss - Server Send 服務端處理完畢,向客戶端傳送迴應
- cr - Client Receive 客戶端收到結果
顯然,通過這四個 annotation 上的時間戳,可以輕易的知道一次完整的呼叫在不同階段的耗時,比如:
- sr - cs 代表了請求在網路上的耗時
- ss - sr 代表了服務端處理請求的耗時
- cr - ss 代表了迴應在網路上的耗時
- cr - cs 代表了一次呼叫的整體耗時
Zipkin 會將 trace 相關的資訊在呼叫鏈路上傳遞,並在每個呼叫邊界結束時非同步的把當前呼叫的耗時資訊上報給 Zipkin Server。Zipkin Server 在收到 trace 資訊後,將其儲存起來,Zipkin 支援的儲存型別有 inMemory、MySql、Cassandra、以及 ElasticsSearch 幾種方式。隨後 Zipkin 的 Web UI 會通過 API 訪問的方式從儲存中將 trace 資訊提取出來分析並展示,如下圖所示:
在 Dubbo 中使用
由於 Brave 對 Dubbo 已經主動做了支援,在 Dubbo 中整合基於 Zipkin 的鏈路追蹤變的十分簡單。下面會按照 Brave 中關於 Dubbo RPC 支援的指引 來說明如何在 Dubbo 中使用 Zipkin。
安裝 Zipkin Server
按照 Zipkin 官方文件中的快速開始 來安裝 Zipkin,如下所示:
$ curl -sSL https://zipkin.io/quickstart.sh | bash -s $ java -jar zipkin.jar
按照這種方式安裝的 Zipkin Server 使用的儲存型別是 inMemory 的。當伺服器停機之後,所有收集到的 trace 資訊會丟失,不適用於生產系統。如果在生產系統中使用,需要配置另外的儲存型別。Zipkin 支援 MySql、Cassandra、和 ElasticSearch。推薦使用 Cassandra 和 ElasticSearch,相關的配置請自行查閱 官方文件 。
本文為了演示方便,使用的儲存是 inMemory 型別。成功啟動之後,可以在終端看到如下的提示:
$ java -jar zipkin.jar Picked up JAVA_TOOL_OPTIONS: -Djava.awt.headless=true ******** **** ** **** **** **** **** ******** **** **** ******** ************* **************************************************************************** ************** ******** ** ** ************** ******** **************** ** ******************** ******************** :: Powered by Spring Boot ::(v2.0.5.RELEASE) ... o.s.b.w.e.u.UndertowServletWebServer: Undertow started on port(s) 9411 (http) with context path '' 2018-10-10 18:40:31.605INFO 21072 --- [main] z.s.ZipkinServer: Started ZipkinServer in 6.835 seconds (JVM running for 8.35)
然後在瀏覽器中訪問 http://localhost:9411 驗證 WEB 介面。
配置 Maven 依賴
引入 Brave 依賴
新建一個新的 Java 工程,並在 pom.xml 中引入 Brave 相關的依賴如下:
<properties> <brave.version>5.4.2</brave.version> <zipkin-reporter.version>2.7.9</zipkin-reporter.version> </properties> <dependencyManagement> <dependencies> <!-- 引入 zipkin brave 的 BOM 檔案 --> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-bom</artifactId> <version>${brave.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 引入 zipkin repoter 的 BOM 檔案 --> <dependency> <groupId>io.zipkin.reporter2</groupId> <artifactId>zipkin-reporter-bom</artifactId> <version>${zipkin-reporter.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 1. brave 對 dubbo 的整合 --> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-instrumentation-dubbo-rpc</artifactId> </dependency> <!-- 2. brave 的 spring bean 支援 --> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-spring-beans</artifactId> </dependency> <!-- 3. 在 SLF4J 的 MDC (Mapped Diagnostic Context) 中支援 traceId 和 spanId --> <dependency> <groupId>io.zipkin.brave</groupId> <artifactId>brave-context-slf4j</artifactId> </dependency> <!-- 4. 使用 okhttp3 作為 reporter --> <dependency> <groupId>io.zipkin.reporter2</groupId> <artifactId>zipkin-sender-okhttp3</artifactId> </dependency> </dependencies>
其中:
- 引入 brave-instrumentation-dubbo-rpc,brave 對 dubbo 的支援: https://github.com/openzipkin/brave/blob/master/instrumentation/dubbo-rpc/README.md
- 引入 brave-spring-beans,brave 對 spring bean 的支援: https://github.com/openzipkin/brave/blob/master/spring-beans/README.md
- 引入 brave-context-slf4j,brave 對 SLF4J 的支援,可以在 MDC 中使用 traceId 和 spanId: https://github.com/openzipkin/brave/blob/master/context/slf4j/README.md
- 引入 zipkin-sender-okhttp3,使用 okhttp3 上報資料: https://github.com/openzipkin/zipkin-reporter-java
引入 Dubbo 相關依賴
Dubbo 相關的依賴是 Dubbo 本身以及 Zookeeper 客戶端,在下面的例子中,我們將會使用獨立的 Zookeeper Server 作為服務發現。
<dependencies> <!-- 1. Zookeeper 客戶端依賴 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> <exclusions> <exclusion> <groupId>io.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <!-- 2. Dubbo 依賴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> </dependencies>
其中:
- Dubbo 這裡依賴獨立的 Zookeeper Server 做服務發現,這裡使用的客戶端是 Curator
- 引入 Dubbo 框架的依賴,原則上 2.6 的任何版本都是工作的,這裡使用的是 2.6.2 版本
實現
我們這裡構造的場景是一個有兩個節點的服務依賴鏈,也就是,當一個 Dubbo 客戶端呼叫服務 A 時,服務 A 將會繼續呼叫服務 B。在這個例子中,服務 A 是 greeting service,它所依賴的下游服務服務 B 是 hello service。
定義服務介面
為此需要事先定義兩個服務介面 GreetingService 以及 HelloService
-
com.alibaba.dubbo.samples.api.GreetingService
package com.alibaba.dubbo.samples.api; public interface GreetingService { String greeting(String message); }
-
com.alibaba.dubbo.samples.api.HelloService
package com.alibaba.dubbo.samples.api; public interface HelloService { String hello(String message); }
實現服務介面
為了區分對待,所有和 HelloService 相關的實現程式碼都放在 hello 子包下,同理 GreetingService 相關的放在 greeting 子包下。
-
實現 com.alibaba.dubbo.samples.api.HelloService
package com.alibaba.dubbo.samples.service.hello; import com.alibaba.dubbo.samples.api.HelloService; import java.util.Random; public class HelloServiceImpl implements HelloService { @Override public String hello(String message) { try { // 通過 sleep 模擬業務邏輯處理時間 Thread.sleep(new Random(System.currentTimeMillis()).nextInt(1000)); } catch (InterruptedException e) { // no op } return "hello, " + message; } }
-
實現 com.alibaba.dubbo.samples.api.GreetingService
package com.alibaba.dubbo.samples.service.greeting; import com.alibaba.dubbo.samples.api.GreetingService; import com.alibaba.dubbo.samples.api.HelloService; import java.util.Random; public class GreetingServiceImpl implements GreetingService { // 下游依賴服務,執行時靠 spring 容器注入 HelloService 的服務代理 private HelloService helloService; public void setHelloService(HelloService helloService) { this.helloService = helloService; } @Override public String greeting(String message) { try { // 通過 sleep 模擬業務邏輯處理時間 Thread.sleep(new Random(System.currentTimeMillis()).nextInt(1000)); } catch (InterruptedException e) { // no op } return "greeting, " + helloService.hello(message); } }
這裡需要注意的是,GreetingServiceImpl 的實現中聲明瞭一個型別是 HelloService 的成員變數,並在 greeting 方法中,執行完自己邏輯之後又呼叫了 HelloService 上的 hello 方法。這裡的 helloService 的實現將會在執行態由外部注入,注入的不是 HelloServiceImpl 的實現,而是 HelloService 的遠端呼叫代理。通過這樣的方式,完成了在一個 Dubbo 服務中繼續呼叫另一個遠端 Dubbo 服務的目的。從鏈路追蹤的角度來說,客戶端呼叫 GreetingService 是一個 span,GreetingService 呼叫 HelloService 是另一個 span,並且兩者有父子關係,同屬於一個 trace,也就是屬於同一條呼叫鏈路。
另外,在 GreetingServiceImpl 和 HelloServiceImpl 的實現中,通過 Thread.sleep 來模擬了處理業務邏輯的耗時,以便在 Zipkin UI 上更好的展示。
配置
為了專注在展示如何使用 Zipkin 這一點上,本文在配置和程式設計模型上沒有采用更多的高階技術,而是使用了最傳統的 Spring XML 的配置方式,幫助讀者理解。更高階的通過 annotation 甚至 spring boot 的方式,讀者可以自行查閱 Dubbo 和 Zipkin 相關的文件。
-
暴露 HelloService 服務
在 resouces/spring/hello-service.xml 中增加以下的配置來將 HelloServiceImpl 暴露成一個 Dubbo 服務:
- 使用了本地啟動的 Zookeeper Server 作為註冊中心,地址為預設值 zookeeper://127.0.0.1:2181
- 用 Dubbo 原生服務在埠 20880 上暴露服務
- 將 HelloServiceImpl 註冊成 id 是
helloService
的 Spring Bean,這樣就可以在後續的<dubbo:service>
中引用到這個實現類 - 通過
<dubbo:service>
將 HelloServiceImpl 暴露成 Dubbo 服務
<!-- 定義 HelloService 的應用名 --> <dubbo:application name="hello-service-provider"/> <!-- 指定註冊中心地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 使用 Dubbo 原生協議在 20880 埠上暴露服務 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- 將 HelloServiceImpl 的實現宣告成一個 spring bean --> <bean id="helloService" class="com.alibaba.dubbo.samples.service.hello.HelloServiceImpl"/> <!-- 將 HelloServiceImpl 宣告成一個 Dubbo 服務 --> <dubbo:service interface="com.alibaba.dubbo.samples.api.HelloService" ref="helloService"/>
-
增加 Zipkin 相關的配置
在 resources/spring/hello-service.xml 中增加 Zipkin 相關的配置:
- 修改 dubbo 服務暴露的配置,新增 Zipkin 的 tracing filter 到 Dubbo 的 filter chain 中
- 按照 https://github.com/openzipkin/brave/blob/master/spring-beans/README.md 來配置 Zipkin 的 sender 和 tracing 的 spring bean
<!-- 1. 修改 dubbo 服務暴露配置,在 filter chain 中增加 zipkin 的 tracing 過濾器 --> <dubbo:service interface="com.alibaba.dubbo.samples.api.HelloService" ref="helloService" filter="tracing"/> <!-- 2. zipkin 相關的配置 --> <!-- 使用 OKHttp 來發送 trace 資訊到 Zipkin Server。這裡的 Zipkin Server 啟動在本地 --> <bean id="sender" class="zipkin2.reporter.beans.OkHttpSenderFactoryBean"> <property name="endpoint" value="http://localhost:9411/api/v2/spans"/> </bean> <bean id="tracing" class="brave.spring.beans.TracingFactoryBean"> <property name="localServiceName" value="hello-service"/> <property name="spanReporter"> <bean class="zipkin2.reporter.beans.AsyncReporterFactoryBean"> <property name="sender" ref="sender"/> <!-- wait up to half a second for any in-flight spans on close --> <property name="closeTimeout" value="500"/> </bean> </property> <property name="currentTraceContext"> <bean class="brave.spring.beans.CurrentTraceContextFactoryBean"> <property name="scopeDecorators"> <bean class="brave.context.slf4j.MDCScopeDecorator" factory-method="create"/> </property> </bean> </property> </bean>
-
增加 HelloService 的啟動類
在 com.alibaba.dubbo.samples.service.hello.Application 中通過 ClassPathXmlApplicationContext 讀取 剛才配置的 spring/hello-service.xml 來初始化一個 spring context 並啟動
package com.alibaba.dubbo.samples.service.hello; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Application { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/hello-service.xml"); context.start(); System.out.println("Hello service started"); // press any key to exit System.in.read(); } }
-
暴露 GreetingService 服務,並使用 Zipkin
在 resources/spring/greeting-service.xml 中配置 GreetingService。相關步驟與 HelloService 類似,不再贅述,重點關注如何在 GreetingService 中配置下游服務的依賴。完整的 XML 配置如下:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 1. 定義 GreetingService 的應用名 --> <dubbo:application name="greeting-service-provider"/> <!-- 2. 指定註冊中心地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 3. 使用 Dubbo 原生協議在 20881 埠上暴露服務 --> <dubbo:protocol name="dubbo" port="20881"/> <!-- 4. 宣告 HelloService 的遠端代理,並在 Dubbo 的 filter chain 中增加 tracing filter --> <dubbo:reference id="helloService" check="false" interface="com.alibaba.dubbo.samples.api.HelloService" filter="tracing"/> <!-- 5. 將 GreetingServiceImpl 的實現宣告成一個 spring bean,並將 HelloService 的遠端代理裝配進去 --> <bean id="greetingService" class="com.alibaba.dubbo.samples.service.greeting.GreetingServiceImpl"> <property name="helloService" ref="helloService"/> </bean> <!-- 6. 將 GreetingServiceImpl 宣告成一個 Dubbo 服務,並在 Dubbo 的 filter chain 中增加 tracing filter --> <dubbo:service interface="com.alibaba.dubbo.samples.api.GreetingService" ref="greetingService" filter="tracing"/> <!-- 7. zipkin 相關的配置 --> <bean id="sender" class="zipkin2.reporter.beans.OkHttpSenderFactoryBean"> <property name="endpoint" value="http://localhost:9411/api/v2/spans"/> </bean> <bean id="tracing" class="brave.spring.beans.TracingFactoryBean"> <property name="localServiceName" value="greeting-service"/> <property name="spanReporter"> <bean class="zipkin2.reporter.beans.AsyncReporterFactoryBean"> <property name="sender" ref="sender"/> <!-- wait up to half a second for any in-flight spans on close --> <property name="closeTimeout" value="500"/> </bean> </property> <property name="currentTraceContext"> <bean class="brave.spring.beans.CurrentTraceContextFactoryBean"> <property name="scopeDecorators"> <bean class="brave.context.slf4j.MDCScopeDecorator" factory-method="create"/> </property> </bean> </property> </bean> </beans>
這裡的配置與上面的 HelloService 類似,需要重點關注的有兩點:
- 第 3 步中注意服務需要暴露在不同的埠上,否則會和 HelloService 衝突,本例中選擇的是 20881 這個埠
- 通過第 4 步先宣告 HelloService 的遠端代理,然後在第 5 步中將其組裝給 GreetingService 來完成服務上下游依賴的宣告
增加 GreeeingService 的啟動類,與 HelloService 類似,通過 spring/greeting-service.xml 的配置來初始化一個新的 spring context 來完成。
package com.alibaba.dubbo.samples.service.greeting; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class Application { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/greeting-service.xml"); context.start(); System.out.println("Greeting service started"); // press any key to exit System.in.read(); } }
-
實現客戶端
通過 resources/spring/client.xml 初始化一個 spring context,從其中獲取 GreetingService 的遠端代理,發起遠端呼叫。
package com.alibaba.dubbo.samples.client; import com.alibaba.dubbo.samples.api.GreetingService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/client.xml"); context.start(); // 獲取遠端代理併發起呼叫 GreetingService greetingService = (GreetingService) context.getBean("greetingService"); System.out.println(greetingService.greeting("world")); } }
resource/spring/client.xml 中的配置與 Dubbo 服務的配置類似,主要是配置遠端代理,以及配置 Zipkin
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 1. 定義 dubbo 客戶端的應用名 --> <dubbo:application name="dubbo-client"/> <!-- 2. 指定註冊中心地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 3. 宣告 GreetingService 的遠端代理,並在 Dubbo 的 filter chain 中增加 tracing filter --> <dubbo:reference id="greetingService" check="false" interface="com.alibaba.dubbo.samples.api.GreetingService" filter="tracing"/> <!-- 4. zipkin 相關的配置 --> <bean id="sender" class="zipkin2.reporter.beans.OkHttpSenderFactoryBean"> <property name="endpoint" value="http://localhost:9411/api/v2/spans"/> </bean> <bean id="tracing" class="brave.spring.beans.TracingFactoryBean"> <property name="localServiceName" value="client"/> <property name="spanReporter"> <bean class="zipkin2.reporter.beans.AsyncReporterFactoryBean"> <property name="sender" ref="sender"/> <!-- wait up to half a second for any in-flight spans on close --> <property name="closeTimeout" value="500"/> </bean> </property> <property name="currentTraceContext"> <bean class="brave.spring.beans.CurrentTraceContextFactoryBean"> <property name="scopeDecorators"> <bean class="brave.context.slf4j.MDCScopeDecorator" factory-method="create"/> </property> </bean> </property> </bean> </beans>
完成之後的工程的目錄結構如下:
執行
現在讓我們把整個鏈路執行起來,看看 Zipkin 鏈路追蹤的效果。
啟動 Zookeeper Server
執行以下命令在本地啟動一個 Zookeeper Server,如果沒有安裝,請自行從 ZooKeeper 官網 下載:
$ zkServer start
啟動 Zipkin Server
執行以下命令在本地啟動一個 Zipkin Server:
$ curl -sSL https://zipkin.io/quickstart.sh | bash -s $ java -jar zipkin.jar
啟動 HelloService
使用下面的命令啟動 HelloService,當然也可以直接在 IDE 中啟動:
$ mvn exec:java -Dexec.mainClass=com.alibaba.dubbo.samples.service.hello.Application
啟動成功後應該可以在終端上看到 “Hello service started” 的字樣。
啟動 GreetingService
使用下面的命令啟動 GreetingService,當然也可以直接在 IDE 中啟動:
$ mvn exec:java -Dexec.mainClass=com.alibaba.dubbo.samples.service.greeting.Application
啟動成功後應該可以在終端上看到 “Greeting service started” 的字樣。
執行 Dubbo 客戶端
使用下面的命令執行 Dubbo 客戶端向 GreetingService 發起遠端呼叫,當然也可以直接在 IDE 中執行:
$ mvn exec:java -Dexec.mainClass=com.alibaba.dubbo.samples.client.Application
執行成功後,客戶端會在終端上輸出 “greeting, hello, world”。
鏈路追蹤
開啟瀏覽器訪問 " http://localhost:9411 " 並通過 "Find Traces" 按鈕來搜尋,可以找到剛剛呼叫的鏈路追蹤,效果如下圖所示:
還可以進一步的選擇每一個 span 來檢視本次呼叫邊界內的詳情,比如,hello-service 這個 span 的詳情如下:
總結
本文介紹了鏈路追蹤的基本概念以及 Zipkin 的基本用法,然後用 Dubbo 構建了一條最簡單的呼叫鏈路,並引入了 Zipkin 做全鏈路追蹤。由於 Zipkin 對 Dubbo 做了很好的支援,整個整合的過程還是十分簡單明瞭的。
Zipkin 對 Dubbo 的支援是構建在 Dubbo 的 filter 擴充套件機制上的,有興趣的讀者可以通過 https://github.com/openzipkin/brave/blob/master/instrumentation/dubbo-rpc/src/main/java/brave/dubbo/rpc/TracingFilter.java 瞭解其實現細節。
本文中涉及的例子可以從 https://github.com/dubbo/dubbo-samples 中的 "dubbo-samples-zipkin" 子模組中獲取。另外,spring-cloud-sleth 2.0 中開始 正式支援 Dubbo ,相關的文章和例子後續計劃提供。