.NET Core微服務之基於Steeltoe使用Zipkin實現分布式追蹤


Tip: 此篇已加入.NET Core微服務基礎系列文章索引

=>  Steeltoe目錄快速導航

1. 基於Steeltoe使用Spring Cloud Eureka

2. 基於Steeltoe使用Spring Cloud Zuul

3. 基於Steeltoe使用Spring Cloud Hystrix

4. 基於Steeltoe使用Spring Cloud Config

5. 基於Steeltoe使用Zipkin

一、關於Spring Cloud Sleuth與Zipkin

  在 SpringCloud 之中提供的 Sleuth 技術可以實現微服務的調用跟蹤,也就是說它可以自動的形成一個調用連接線,通過這個連接線使得開發者可以輕松的找到所有微服務間關系,同時也可以獲取微服務所耗費的時間, 這樣就可以進行微服務調用狀態的監控以及相應的數據分析。

  

  Zipkin是一個分布式追蹤系統,它有助於收集解決微服務架構中延遲問題所需的時序數據。它管理這些數據的收集和查找。

  應用程序用於向Zipkin報告時間數據。Zipkin UI還提供了一個依賴關系圖,顯示每個應用程序有多少跟蹤請求。如果你正在解決延遲問題或錯誤問題,則可以根據應用程序,跟蹤長度,注釋或時間戳過濾或排序所有跟蹤。一旦選擇了一個跟蹤,你可以看到每個跨度所花費的總跟蹤時間的百分比,從而可以確定問題應用程序。

二、快速構建Zipkin Server

  示例版本:Spring Boot 1.5.15.RELEASE,Spring Cloud Edgware.SR3

  (1)pom.xml 添加相關依賴包

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 熱啟動,熱部署依賴包,為了調試方便,加入此包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- zipkin -->
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
        </dependency>
    </dependencies>

    <!-- spring cloud dependencies -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  (2)啟動類添加相關注解

@SpringBootApplication
@EnableZipkinServer
public class ZipkinServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZipkinServiceApplication.class, args);
    }
}

  (3)配置文件

server:
  port: 9411

spring:
  application:
    name: zipkin-service

  最終啟動后,訪問zipkin主界面:

三、ASP.NET Core集成Zipkin

3.1 示例環境准備

  這里仍然基於第一篇的示例進行修改,各個項目的角色如下表所示:

微服務項目名稱 項目微服務中的角色
eureka-service   服務發現&注冊(Spring Boot)
zuul-service   API網關 (Spring Boot)  
zipkin-service   分布式追蹤服務 (Spring Boot)  
agent-service   服務提供者 (ASP.NET Core)
client-service   服務提供者 (ASP.NET Core)
premium-service   服務提供者&服務消費者 (ASP.NET Core)

  所有相關服務(除zipkin-service外)注冊到Eureka之后的服務列表:

  

3.2 想要測試的服務調用鏈路

  瀏覽器通過API網關(Zuul)調用Premium-Service的API,在這個API中會調用Client-Service的API,當然,會通過服務發現(Eureka)來獲取Client-Service的URL。

3.3 以PremiumService為例添加相關配置

  這里以PremiumService為例,其他幾個Service參照下面的步驟依次添加配置即可。

  (1)添加相關NuGet包

PM> Install-Package Steeltoe.Extensions.Logging.DynamicLogger 

PM> Install-Package Steeltoe.Management.ExporterCore     

PM> Install-Package Steeltoe.Management.TracingCore

  (2)Program類添加動態日志Provider

    public class Program
    {
        ......

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://*:8030")
                .UseStartup<Startup>()
                .ConfigureLogging((builderContext, loggingBuilder) => { loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging")); // Add Steeltoe Dynamic Logging Provider  loggingBuilder.AddDynamicConsole(); });
    }

  Steeltoe的日志提供器是對ASP.NET Core自身日志器的進一步封裝,其在原始數據基礎上增加了如Spring Cloud Sleuth中一樣的額外信息。

  (3)Starup啟動類中添加相關配置

    public class Startup
    {
        ......

        public void ConfigureServices(IServiceCollection services)
        {
            ......
            // Add Steeltoe Distributed Tracing
            services.AddDistributedTracing(Configuration);
            // Export traces to Zipkin
            services.AddZipkinExporter(Configuration);

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // Add Hystrix Metrics to container
            services.AddHystrixMetricsStream(Configuration);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            ......

            app.UseMvc();
            
            // Start Hystrix metrics stream service
            app.UseHystrixMetricsStream();
            // Start up trace exporter
            app.UseTracingExporter();
        }
    }

  (4)appSettings添加相關配置 => 主要是zipkin server的相關信息

"management": {
    "tracing": {
      "alwaysSample": true,
      "egressIgnorePattern": "/api/v2/spans|/v2/apps/.*/permissions|/eureka/.*|/oauth/.*",
      "exporter": {
        "zipkin": {
          "endpoint": "http://localhost:9411/api/v2/spans",
          "validateCertificates": false
        }
      }
    }
  }

四、快速驗證測試

  (1)啟動Eureka, Zuul, Zipkin以及Premium-Service和Client-Service

  (2)通過Zuul調用API 

  

  (3)通過Zipkin UI查看Trace

  

  點擊具體的Trace查看Details

  

  (4)點擊“依賴分析”按鈕查看依賴圖

  

五、小結

  本文簡單地介紹了一下Spring Cloud Seluth與Zipkin,然后通過Java快速地構建了一個Zipkin Server,通過在ASP.NET Core中集成Zipkin並做了一個基本的微服務調用追蹤Demo。本示例的Zipkin Server的追蹤數據是基於內存,實際中應該集成ELK進行持久化。當然,我們也可以直接通過Zipkin的.NET客戶端來做。

示例代碼

  GitHub => https://github.com/EdisonChou/Microservice.PoC.Steeltoe/tree/master/src/Chapter4-ServiceTracing

參考資料

Steeltoe官方文檔:《Steeltoe Doc

Steeltoe官方示例:https://github.com/SteeltoeOSS/Samples

周立,《Spring Cloud與Docker 微服務架構實戰

小不點啊,《SpringCloud系列十二:SpringCloudSleuth(SpringCloudSleuth 簡介、SpringCloudSleuth 基本配置、數據采集)

Ken.W,《Steeltoe之Distributed Tracing篇

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM