如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤

這篇文章主要介紹“如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤”,在日常操作中,相信很多人在如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

成都創(chuàng)新互聯(lián)主要從事做網(wǎng)站、網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)將樂,十年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108

Zipkin 簡介

Zipkin 是基于 Dapper 論文實現(xiàn),由 Twitter 開源的分布式追蹤系統(tǒng),通過收集分布式服務(wù)執(zhí)行時間的信息來達到追蹤服務(wù)調(diào)用鏈路、以及分析服務(wù)執(zhí)行延遲等目的。

Zipkin 架構(gòu)

如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤cdn.nlark.com/lark/0/2018/png/13615/1539327563392-b01ee384-c79f-48b9-9027-4bc10a4397f7.png">

Collector 收集器、Storage 存儲、API、UI 用戶界面等幾部分構(gòu)成了 Zipkin Server 部分,對應(yīng)于 GitHub 上 openzipkin/zipkin 這個項目。而收集應(yīng)用中調(diào)用的耗時信息并將其上報的組件與應(yīng)用共生,并擁有各個語言的實現(xiàn)版本,其中 Java 的實現(xiàn)是 GitHub 上 openzipkin/brave 。除了 Java 客戶端實現(xiàn)之外,openzipkin 還提供了許多其他語言的實現(xiàn),其中包括了 go、php、JavaScript、.net、ruby 等,具體列表可以參閱 Zipkin 的 Exiting instrumentations 。

Zipkin 的工作過程

當用戶發(fā)起一次調(diào)用時,Zipkin 的客戶端會在入口處為整條調(diào)用鏈路生成一個全局唯一的 trace id,并為這條鏈路中的每一次分布式調(diào)用生成一個 span id。span 與 span 之間可以有父子嵌套關(guān)系,代表分布式調(diào)用中的上下游關(guān)系。span 和 span 之間可以是兄弟關(guān)系,代表當前調(diào)用下的兩次子調(diào)用。一個 trace 由一組 span 組成,可以看成是由 trace 為根節(jié)點,span 為若干個子節(jié)點的一棵樹。

如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤

Span 由調(diào)用邊界來分隔,在 Zipkin 中,調(diào)用邊界由以下四個 annotation 來表示:

  • cs - Clent Sent 客戶端發(fā)送了請求

  • sr - Server Receive 服務(wù)端接受到請求

  • ss - Server Send 服務(wù)端處理完畢,向客戶端發(fā)送回應(yīng)

  • cr - Client Receive 客戶端收到結(jié)果

顯然,通過這四個 annotation 上的時間戳,可以輕易的知道一次完整的調(diào)用在不同階段的耗時,比如:

  • sr - cs 代表了請求在網(wǎng)絡(luò)上的耗時

  • ss - sr 代表了服務(wù)端處理請求的耗時

  • cr - ss 代表了回應(yīng)在網(wǎng)絡(luò)上的耗時

  • cr - cs 代表了一次調(diào)用的整體耗時

Zipkin 會將 trace 相關(guān)的信息在調(diào)用鏈路上傳遞,并在每個調(diào)用邊界結(jié)束時異步的把當前調(diào)用的耗時信息上報給 Zipkin Server。Zipkin Server 在收到 trace 信息后,將其存儲起來,Zipkin 支持的存儲類型有 inMemory、MySQL、Cassandra、以及 ElasticsSearch 幾種方式。隨后 Zipkin 的 Web UI 會通過 API 訪問的方式從存儲中將 trace 信息提取出來分析并展示,如下圖所示:

如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤

在 Dubbo 中使用

由于 Brave 對 Dubbo 已經(jīng)主動做了支持,在 Dubbo 中集成基于 Zipkin 的鏈路追蹤變的十分簡單。下面會按照 Brave 中關(guān)于 Dubbo RPC 支持的指引 來說明如何在 Dubbo 中使用 Zipkin。

安裝 Zipkin Server

按照 Zipkin 官方文檔中的快速開始 來安裝 Zipkin,如下所示:

$ curl -sSL https://zipkin.io/quickstart.sh | bash -s$ java -jar zipkin.jar

按照這種方式安裝的 Zipkin Server 使用的存儲類型是 inMemory 的。當服務(wù)器停機之后,所有收集到的 trace 信息會丟失,不適用于生產(chǎn)系統(tǒng)。如果在生產(chǎn)系統(tǒng)中使用,需要配置另外的存儲類型。Zipkin 支持 MySql、Cassandra、和 ElasticSearch。推薦使用 Cassandra 和 ElasticSearch,相關(guān)的配置請自行查閱 官方文檔 。

本文為了演示方便,使用的存儲是 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.605  INFO 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 相關(guān)的依賴如下:

    <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>

其中:

  1. 引入 brave-instrumentation-dubbo-rpc,brave 對 dubbo 的支持: https://github.com/openzipkin/brave/blob/master/instrumentation/dubbo-rpc/README.md

  2. 引入 brave-spring-beans,brave 對 spring bean 的支持: https://github.com/openzipkin/brave/blob/master/spring-beans/README.md

  3. 引入 brave-context-slf4j,brave 對 SLF4J 的支持,可以在 MDC 中使用 traceId 和 spanId: https://github.com/openzipkin/brave/blob/master/context/slf4j/README.md

  4. 引入 zipkin-sender-okhttp3,使用 okhttp3 上報數(shù)據(jù): https://github.com/openzipkin/zipkin-reporter-java

引入 Dubbo 相關(guān)依賴

Dubbo 相關(guān)的依賴是 Dubbo 本身以及 Zookeeper 客戶端,在下面的例子中,我們將會使用獨立的 Zookeeper Server 作為服務(wù)發(fā)現(xiàn)。

    <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>

其中:

  1. Dubbo 這里依賴獨立的 Zookeeper Server 做服務(wù)發(fā)現(xiàn),這里使用的客戶端是 Curator

  2. 引入 Dubbo 框架的依賴,原則上 2.6 的任何版本都是工作的,這里使用的是 2.6.2 版本

實現(xiàn)

我們這里構(gòu)造的場景是一個有兩個節(jié)點的服務(wù)依賴鏈,也就是,當一個 Dubbo 客戶端調(diào)用服務(wù) A 時,服務(wù) A 將會繼續(xù)調(diào)用服務(wù) B。在這個例子中,服務(wù) A 是 greeting service,它所依賴的下游服務(wù)服務(wù) B 是 hello service。

定義服務(wù)接口

為此需要事先定義兩個服務(wù)接口 GreetingService 以及 HelloService

  1. com.alibaba.dubbo.samples.api.GreetingService

    package com.alibaba.dubbo.samples.api;public interface GreetingService {    String greeting(String message);
    }
  2. com.alibaba.dubbo.samples.api.HelloService

    package com.alibaba.dubbo.samples.api;public interface HelloService {    String hello(String message);
    }
實現(xiàn)服務(wù)接口

為了區(qū)分對待,所有和 HelloService 相關(guān)的實現(xiàn)代碼都放在 hello 子包下,同理 GreetingService 相關(guān)的放在 greeting 子包下。

  1. 實現(xiàn) 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 模擬業(yè)務(wù)邏輯處理時間
                Thread.sleep(new Random(System.currentTimeMillis()).nextInt(1000));
            } catch (InterruptedException e) {            // no op
            }        return "hello, " + message;
        }
    }
  2. 實現(xiàn) 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 {    // 下游依賴服務(wù),運行時靠 spring 容器注入 HelloService 的服務(wù)代理
        private HelloService helloService;    public void setHelloService(HelloService helloService) {        this.helloService = helloService;
        }    @Override
        public String greeting(String message) {        try {            // 通過 sleep 模擬業(yè)務(wù)邏輯處理時間
                Thread.sleep(new Random(System.currentTimeMillis()).nextInt(1000));
            } catch (InterruptedException e) {            // no op
            }        return "greeting, " + helloService.hello(message);
        }
    }

    這里需要注意的是,GreetingServiceImpl 的實現(xiàn)中聲明了一個類型是 HelloService 的成員變量,并在 greeting 方法中,執(zhí)行完自己邏輯之后又調(diào)用了 HelloService 上的 hello 方法。這里的 helloService 的實現(xiàn)將會在運行態(tài)由外部注入,注入的不是 HelloServiceImpl 的實現(xiàn),而是 HelloService 的遠程調(diào)用代理。通過這樣的方式,完成了在一個 Dubbo 服務(wù)中繼續(xù)調(diào)用另一個遠程 Dubbo 服務(wù)的目的。從鏈路追蹤的角度來說,客戶端調(diào)用 GreetingService 是一個 span,GreetingService 調(diào)用 HelloService 是另一個 span,并且兩者有父子關(guān)系,同屬于一個 trace,也就是屬于同一條調(diào)用鏈路。

    另外,在 GreetingServiceImpl 和 HelloServiceImpl 的實現(xiàn)中,通過 Thread.sleep 來模擬了處理業(yè)務(wù)邏輯的耗時,以便在 Zipkin UI 上更好的展示。

配置

為了專注在展示如何使用 Zipkin 這一點上,本文在配置和編程模型上沒有采用更多的高級技術(shù),而是使用了最傳統(tǒng)的 Spring XML 的配置方式,幫助讀者理解。更高級的通過 annotation 甚至 spring boot 的方式,讀者可以自行查閱 Dubbo 和 Zipkin 相關(guān)的文檔。

  1. 暴露 HelloService 服務(wù)

    在 resouces/spring/hello-service.xml 中增加以下的配置來將 HelloServiceImpl 暴露成一個 Dubbo 服務(wù):

        <!-- 定義 HelloService 的應(yīng)用名 -->
        <dubbo:application name="hello-service-provider"/>
        <!-- 指定注冊中心地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        <!-- 使用 Dubbo 原生協(xié)議在 20880 端口上暴露服務(wù) -->
        <dubbo:protocol name="dubbo" port="20880"/>
        <!-- 將 HelloServiceImpl 的實現(xiàn)聲明成一個 spring bean -->
        <bean id="helloService" class="com.alibaba.dubbo.samples.service.hello.HelloServiceImpl"/>
        <!-- 將 HelloServiceImpl 聲明成一個 Dubbo 服務(wù) -->
        <dubbo:service interface="com.alibaba.dubbo.samples.api.HelloService" ref="helloService"/>
    • 使用了本地啟動的 Zookeeper Server 作為注冊中心,地址為默認值 zookeeper://127.0.0.1:2181

    • 用 Dubbo 原生服務(wù)在端口 20880 上暴露服務(wù)

    • 將 HelloServiceImpl 注冊成 id 是 helloService 的 Spring Bean,這樣就可以在后續(xù)的 <dubbo:service> 中引用到這個實現(xiàn)類

    • 通過 <dubbo:service> 將 HelloServiceImpl 暴露成 Dubbo 服務(wù)

  2. 增加 Zipkin 相關(guān)的配置

    在 resources/spring/hello-service.xml 中增加 Zipkin 相關(guān)的配置:

        <!-- 1. 修改 dubbo 服務(wù)暴露配置,在 filter chain 中增加 zipkin 的 tracing 過濾器 -->
        <dubbo:service interface="com.alibaba.dubbo.samples.api.HelloService" ref="helloService" filter="tracing"/>
        <!-- 2. zipkin 相關(guān)的配置 -->
        <!-- 使用 OKHttp 來發(fā)送 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>
    • 修改 dubbo 服務(wù)暴露的配置,添加 Zipkin 的 tracing filter 到 Dubbo 的 filter chain 中

    • 按照 https://github.com/openzipkin/brave/blob/master/spring-beans/README.md 來配置 Zipkin 的 sender 和 tracing 的 spring bean

  3. 增加 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();
        }
    }
  4. 暴露 GreetingService 服務(wù),并使用 Zipkin

    在 resources/spring/greeting-service.xml 中配置 GreetingService。相關(guān)步驟與 HelloService 類似,不再贅述,重點關(guān)注如何在 GreetingService 中配置下游服務(wù)的依賴。完整的 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 的應(yīng)用名 -->
        <dubbo:application name="greeting-service-provider"/>
        <!-- 2. 指定注冊中心地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
         <!-- 3. 使用 Dubbo 原生協(xié)議在 20881 端口上暴露服務(wù) -->
        <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 的實現(xiàn)聲明成一個 spring bean,并將 HelloService 的遠程代理裝配進去 -->
        <bean id="greetingService" class="com.alibaba.dubbo.samples.service.greeting.GreetingServiceImpl">
            <property name="helloService" ref="helloService"/>
        </bean>
        <!-- 6. 將 GreetingServiceImpl 聲明成一個 Dubbo 服務(wù),并在 Dubbo 的 filter chain 中增加 tracing filter -->
        <dubbo:service interface="com.alibaba.dubbo.samples.api.GreetingService" ref="greetingService" filter="tracing"/>
        <!-- 7. zipkin 相關(guān)的配置 -->
        <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 類似,需要重點關(guān)注的有兩點:

    增加 GreeeingService 的啟動類,與 HelloService 類似,通過 spring/greeting-service.xml 的配置來初始化一個新的 spring context 來完成。

    • 第 3 步中注意服務(wù)需要暴露在不同的端口上,否則會和 HelloService 沖突,本例中選擇的是 20881 這個端口

    • 通過第 4 步先聲明 HelloService 的遠程代理,然后在第 5 步中將其組裝給 GreetingService 來完成服務(wù)上下游依賴的聲明

   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();
       }
   }
  1. 實現(xiàn)客戶端

    通過 resources/spring/client.xml 初始化一個 spring context,從其中獲取 GreetingService 的遠程代理,發(fā)起遠程調(diào)用。

    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();        // 獲取遠程代理并發(fā)起調(diào)用
            GreetingService greetingService = (GreetingService) context.getBean("greetingService");
            System.out.println(greetingService.greeting("world"));
        }
    }

    resource/spring/client.xml 中的配置與 Dubbo 服務(wù)的配置類似,主要是配置遠程代理,以及配置 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 客戶端的應(yīng)用名 -->
        <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 相關(guān)的配置 -->
        <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>

完成之后的工程的目錄結(jié)構(gòu)如下:

如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤

運行

現(xiàn)在讓我們把整個鏈路運行起來,看看 Zipkin 鏈路追蹤的效果。

啟動 Zookeeper Server

執(zhí)行以下命令在本地啟動一個 Zookeeper Server,如果沒有安裝,請自行從 ZooKeeper 官網(wǎng) 下載:

$ zkServer start
啟動 Zipkin Server

執(zhí)行以下命令在本地啟動一個 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

啟動成功后應(yīng)該可以在終端上看到 “Hello service started” 的字樣。

啟動 GreetingService

使用下面的命令啟動 GreetingService,當然也可以直接在 IDE 中啟動:

$ mvn exec:java -Dexec.mainClass=com.alibaba.dubbo.samples.service.greeting.Application

啟動成功后應(yīng)該可以在終端上看到 “Greeting service started” 的字樣。

運行 Dubbo 客戶端

使用下面的命令運行 Dubbo 客戶端向 GreetingService 發(fā)起遠程調(diào)用,當然也可以直接在 IDE 中運行:

$ mvn exec:java -Dexec.mainClass=com.alibaba.dubbo.samples.client.Application

執(zhí)行成功后,客戶端會在終端上輸出 “greeting, hello, world”。

鏈路追蹤

打開瀏覽器訪問 " http://localhost:9411 " 并通過 "Find Traces" 按鈕來搜索,可以找到剛剛調(diào)用的鏈路追蹤,效果如下圖所示:

如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤

還可以進一步的選擇每一個 span 來查看本次調(diào)用邊界內(nèi)的詳情,比如,hello-service 這個 span 的詳情如下:

如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤

到此,關(guān)于“如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

文章標題:如何在Dubbo中使用Zipkin來實現(xiàn)分布式追蹤
分享網(wǎng)址:http://muchs.cn/article20/gpgjco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、小程序開發(fā)、搜索引擎優(yōu)化標簽優(yōu)化、App開發(fā)、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化