欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

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

Zipkin 簡(jiǎn)介

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

Zipkin 架構(gòu)

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

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

Zipkin 的工作過(guò)程

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

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

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

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

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

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

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

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

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

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

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

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

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

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

在 Dubbo 中使用

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

安裝 Zipkin Server

按照 Zipkin 官方文檔中的快速開(kāi)始 來(lái)安裝 Zipkin,如下所示:

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

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

本文為了演示方便,使用的存儲(chǔ)是 inMemory 類(lèi)型。成功啟動(dòng)之后,可以在終端看到如下的提示:

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

然后在瀏覽器中訪問(wèn) http://localhost:9411 驗(yàn)證 WEB 界面。

配置 Maven 依賴(lài)

引入 Brave 依賴(lài)

新建一個(gè)新的 Java 工程,并在 pom.xml 中引入 Brave 相關(guān)的依賴(lài)如下:

    <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 對(duì) 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 對(duì) dubbo 的支持: https://github.com/openzipkin/brave/blob/master/instrumentation/dubbo-rpc/README.md

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

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

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

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

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

    <dependencies>
        <!-- 1. Zookeeper 客戶端依賴(lài) -->
        <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 依賴(lài) -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
    </dependencies>

其中:

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

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

實(shí)現(xiàn)

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

定義服務(wù)接口

為此需要事先定義兩個(gè)服務(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);
    }
實(shí)現(xiàn)服務(wù)接口

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

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

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

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

配置

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

  1. 暴露 HelloService 服務(wù)

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

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

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

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

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

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

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

        <!-- 1. 修改 dubbo 服務(wù)暴露配置,在 filter chain 中增加 zipkin 的 tracing 過(guò)濾器 -->
        <dubbo:service interface="com.alibaba.dubbo.samples.api.HelloService" ref="helloService" filter="tracing"/>
        <!-- 2. zipkin 相關(guān)的配置 -->
        <!-- 使用 OKHttp 來(lái)發(fā)送 trace 信息到 Zipkin Server。這里的 Zipkin Server 啟動(dòng)在本地 -->
        <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 來(lái)配置 Zipkin 的 sender 和 tracing 的 spring bean

  3. 增加 HelloService 的啟動(dòng)類(lèi)

    在 com.alibaba.dubbo.samples.service.hello.Application 中通過(guò) ClassPathXmlApplicationContext 讀取 剛才配置的 spring/hello-service.xml 來(lái)初始化一個(gè) spring context 并啟動(dòng)

    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 類(lèi)似,不再贅述,重點(diǎn)關(guān)注如何在 GreetingService 中配置下游服務(wù)的依賴(lài)。完整的 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. 指定注冊(cè)中心地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
         <!-- 3. 使用 Dubbo 原生協(xié)議在 20881 端口上暴露服務(wù) -->
        <dubbo:protocol name="dubbo" port="20881"/>
        
        <!-- 4. 聲明 HelloService 的遠(yuǎn)程代理,并在 Dubbo 的 filter chain 中增加 tracing filter -->
        <dubbo:reference id="helloService" check="false" interface="com.alibaba.dubbo.samples.api.HelloService" filter="tracing"/>
        
        <!-- 5. 將 GreetingServiceImpl 的實(shí)現(xiàn)聲明成一個(gè) spring bean,并將 HelloService 的遠(yuǎn)程代理裝配進(jìn)去 -->
        <bean id="greetingService" class="com.alibaba.dubbo.samples.service.greeting.GreetingServiceImpl">
            <property name="helloService" ref="helloService"/>
        </bean>
        <!-- 6. 將 GreetingServiceImpl 聲明成一個(gè) 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 類(lèi)似,需要重點(diǎn)關(guān)注的有兩點(diǎn):

    增加 GreeeingService 的啟動(dòng)類(lèi),與 HelloService 類(lèi)似,通過(guò) spring/greeting-service.xml 的配置來(lái)初始化一個(gè)新的 spring context 來(lái)完成。

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

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

   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. 實(shí)現(xiàn)客戶端

    通過(guò) resources/spring/client.xml 初始化一個(gè) spring context,從其中獲取 GreetingService 的遠(yuǎn)程代理,發(fā)起遠(yuǎn)程調(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();        // 獲取遠(yuǎn)程代理并發(fā)起調(diào)用
            GreetingService greetingService = (GreetingService) context.getBean("greetingService");
            System.out.println(greetingService.greeting("world"));
        }
    }

    resource/spring/client.xml 中的配置與 Dubbo 服務(wù)的配置類(lèi)似,主要是配置遠(yuǎn)程代理,以及配置 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. 指定注冊(cè)中心地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        <!-- 3. 聲明 GreetingService 的遠(yuǎn)程代理,并在 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來(lái)實(shí)現(xiàn)分布式追蹤

運(yùn)行

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

啟動(dòng) Zookeeper Server

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

$ zkServer start
啟動(dòng) Zipkin Server

執(zhí)行以下命令在本地啟動(dòng)一個(gè) Zipkin Server:

$ curl -sSL https://zipkin.io/quickstart.sh | bash -s$ java -jar zipkin.jar
啟動(dòng) HelloService

使用下面的命令啟動(dòng) HelloService,當(dāng)然也可以直接在 IDE 中啟動(dòng):

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

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

啟動(dòng) GreetingService

使用下面的命令啟動(dòng) GreetingService,當(dāng)然也可以直接在 IDE 中啟動(dòng):

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

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

運(yùn)行 Dubbo 客戶端

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

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

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

鏈路追蹤

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

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

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

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

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

文章標(biāo)題:如何在Dubbo中使用Zipkin來(lái)實(shí)現(xiàn)分布式追蹤
分享網(wǎng)址:http://aaarwkj.com/article20/gpgjco.html

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

廣告

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

成都seo排名網(wǎng)站優(yōu)化
精品亚洲午夜久久久久| 亚洲禁看av一区不卡| 亚洲av乱码毛片在线播放| 一区二区三区人妻系列| 久久亚洲一本综合久久| 日本不卡在线观看欧美精品| 久久精品国产亚洲av高清观看| 欧美视频在线观看香蕉| 99久久精品费精品国产风间由美| 亚洲最大成人综合福利网| 亚洲最大黄色免费在线观看 | 日韩在线视频精品一区| 欧美日韩国产福利在线观看| 中文字幕四虎在线观看| 亚洲精品一区二区毛豆| 久久这里精品中文字幕| 杨幂一区二区在线观看| 亚洲午夜一区二区三区精品| 国产亚洲一线二线三线| 热九九这里只有热九九| 啊啊…嗯嗯…用力免费观看视频| 国产一区二区av免费| 日本精品动漫一区二区三区| 日本精品人妻一区二区三区蜜桃| 日韩欧美亚洲国产一区久久精品| 持续侵犯人妻中文字幕| 亚洲欧美精品专区久久| 亚洲国产日韩欧美一级| 97资源视频在线播放| 无码精品人妻一区二区三区中| 国产一区二区精品小视频| 99久久精品国产国产毛片| 在线视频网友自拍偷拍| 成人久久精品一区二区| 欧美日韩电影一区二区三区| 国产午夜亚洲精品羞羞网站| 中文字幕一区侵犯人妻| 精品一区二区日本高清| 韩日av一区二区三区| 在线国产丝袜自拍观看| 亚洲国产欧美在线91|