<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Spring Cloud Zipkin 和 Sleuth 示例 > 原文: [https://howtodoinjava.com/spring-cloud/spring-cloud-zipkin-sleuth-tutorial/](https://howtodoinjava.com/spring-cloud/spring-cloud-zipkin-sleuth-tutorial/) [Zipkin](http://zipkin.io/) 是用于[微服務](https://howtodoinjava.com/microservices/microservices-definition-principles-benefits/)生態系統中**分布式跟蹤**的非常有效的工具。 通常,分布式跟蹤是對分布式事務中每個組件的延遲度量,其中調用多個微服務來為單個業務用例提供服務。 假設從我們的應用程序中,我們必須為事務調用 4 個不同的服務/組件。 在啟用了分布式跟蹤的情況下,我們可以測量哪個組件花費了多少時間。 這在調試過程中非常有用,當涉及大量基礎系統并且應用程序在任何特定情況下變慢時。 在這種情況下,我們首先需要確定哪個底層服務實際上是緩慢的。 一旦發現服務緩慢,我們便可以解決該問題。 分布式跟蹤有助于識別生態系統中的緩慢組件。 ## Zipkin Zipkin 最初是在 Twitter 上開發的,基于 Google 論文的概念,該論文描述了 Google 內部構建的分布式應用程序調試器 – [精簡程序](http://research.google.com/pubs/pub36356.html)。 它管理此數據的收集和查找。 要使用 Zipkin,將對應用程序進行檢測以向其報告計時數據。 如果要對生態系統中的延遲問題或錯誤進行故障排除,則可以根據應用程序,跟蹤的長度,注解或時間戳來對所有跟蹤進行過濾或排序。 通過分析這些跟蹤,可以確定哪些組件未按預期執行,并可以對其進行修復。 內部有 4 個模塊: 1. **收集器** – 一旦任何組件將跟蹤數據發送到 Zipkin 收集器守護程序,Zipkin 收集器就會對其進行驗證,存儲和索引以進行查找。 2. **存儲** – 此模塊在后端存儲和索引查找數據。 支持 [Cassandra](https://cassandra.apache.org/),[ElasticSearch](https://www.elastic.co/) 和 [MySQL](https://howtodoinjava.com/mysql/how-to-installuninstallexecute-mysql-as-windows-service/)。 3. **搜索** – 此模塊提供了一個簡單的 JSON API,用于查找和檢索存儲在后端的跟蹤。 該 API 的主要使用者是 Web UI。 4. **Web UI** – 一個非常好的 UI 界面,用于查看軌跡。 #### 如何安裝 Zipkin 可以在[快速入門頁](http://zipkin.io/pages/quickstart.html)上找到適用于不同操作系統的詳細安裝步驟,包括 [Docker](https://howtodoinjava.com/cloud/docker-hello-world-example/) 映像。 對于 Windows 安裝,只需從[ maven 存儲庫](https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec)下載最新的 Zipkin 服務器,然后使用以下命令運行[可執行 jar ](https://howtodoinjava.com/maven/maven-shade-plugin-create-uberfat-jar-example/)文件。 ```java java -jar zipkin-server-1.30.3-exec.jar ``` Zipkin 啟動后,我們可以在`http://localhost:9411/zipkin/`上看到 Web UI。 上面的命令將使用默認配置啟動 Zipkin 服務器。 對于高級配置,我們可以配置許多其他內容,例如存儲,收集器監聽器等。 要**在 Spring Boot 應用程序**中安裝 Zipkin,我們需要在 Spring Boot 項目中添加 Zipkin 啟動器依賴項。 ```java <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> ``` ## Sleuth [Sleuth](https://cloud.spring.io/spring-cloud-sleuth/) 是 Spring Cloud 系列的工具。 它用于生成跟蹤 ID,span id,并將這些信息添加到標頭和 MDC 中的服務調用中,以便諸如 Zipkin 和 [ELK](https://howtodoinjava.com/microservices/elk-stack-tutorial-example/) 等用于存儲,索引和處理日志文件之類的工具可以使用它。由于它來自 Spring Cloud 系列,因此一旦添加到`CLASSPATH`中,它就會自動集成到常見的通信渠道,例如: * 使用[`RestTemplate`](https://howtodoinjava.com/spring/spring-restful/spring-restful-client-resttemplate-example/)等發出的請求。 * 通過 [Netflix Zuul](https://howtodoinjava.com/spring/spring-cloud/spring-cloud-api-gateway-zuul/) 微代理的請求 * 在 [Spring MVC](https://howtodoinjava.com/spring-mvc-tutorial/) 控制器處收到的 HTTP 標頭 * 通過諸如 Apache Kafka 或 RabbitMQ 等消息傳遞技術的請求。 使用偵探非常容易。 我們只需要在 spring boot 項目中添加它的啟動 pom。 它將 Sleuth 添加到項目中,因此在其運行時也是如此。 ```java <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> ``` 到目前為止,我們已經將 Zipkin 和 Sleuth 集成到微服務中并運行 Zipkin 服務器。 讓我們看看如何利用此設置。 ## Zipkin 和 Sleuth 集成示例 對于此演示,讓我們創建 4 個基于 spring boot 的微服務。 它們都將同時具有 Zipkin 和 Sleuth 啟動器依賴項。 在每個微服務中,我們將公開一個端點,從第一個服務中我們將調用第二個服務,從第二個服務中我們將調用第三個服務,以此類推,以此類推。 正如我們已經提到的,Sleuth 會自動與 rest 模板一起使用,因此它將將此檢測到的服務調用信息發送到連接的 Zipkin 服務器。 然后 Zipkin 將開始進行延遲計算以及其他一些統計數據(如服務調用詳細信息)的記賬。 ![Microservices Interactions](https://img.kancloud.cn/7c/37/7c37407b1057062fe781b8534b9c34c7_677x425.jpg) 微服務交互 #### 創建微服務 所有這四個服務將具有相同的配置,唯一的不同是端點更改的服務調用詳細信息。 讓我們[創建具有 Web,Rest Repository,Zipkin 和 Sleuth 依賴項的 Spring Boot 應用程序](https://howtodoinjava.com/spring/spring-boot/spring-boot-tutorial-with-hello-world-example/)。 我將這些服務打包在一個父項目中,以便可以將這四個服務一起構建以節省時間。 如果愿意,可以繼續進行個人設置。 另外,我還添加了有用的 Windows 腳本,因此只需一個命令即可啟動/停止所有服務。 這是一個示例 rest 控制器,它公開一個端點并使用 rest 模板調用一個下游服務。 ```java package com.example.zipkinservice1; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.sleuth.sampler.AlwaysSampler; import org.springframework.context.annotation.Bean; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class ZipkinService1Application { public static void main(String[] args) { SpringApplication.run(ZipkinService1Application.class, args); } } @RestController class ZipkinController{ @Autowired RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } @Bean public AlwaysSampler alwaysSampler() { return new AlwaysSampler(); } private static final Logger LOG = Logger.getLogger(ZipkinController.class.getName()); @GetMapping(value="/zipkin") public String zipkinService1() { LOG.info("Inside zipkinService 1.."); String response = (String) restTemplate.exchange("http://localhost:8082/zipkin2", HttpMethod.GET, null, new ParameterizedTypeReference<String>() {}).getBody(); return "Hi..."; } } ``` #### 應用程序設置 由于所有服務都將在一臺計算機上運行,??因此我們需要在不同的端口上運行它們。 另外,要在 Zipkin 中進行標識,我們需要提供適當的名稱。 因此,請在資源文件夾下的`application.properties`文件中配置應用程序名稱和端口信息。 ```java server.port = 8081 spring.application.name = zipkin-server1 ``` 同樣,對于其他 3 個服務,我們將使用端口 **8082**,**8083**,**8084**,名稱也將類似于`zipkin-server2`,`zipkin-server3`和`zipkin-server4`。 另外,我們有意在上次服務中引入了延遲,以便我們可以在 Zipkin 中進行查看。 ## 示例 使用微服務中的命令`mvn clean install`進行最終的 Maven 構建,啟動所有 4 個應用程序以及 zipkin 服務器。 對于快速啟動和停止,使用批處理文件`Start-all.bat`和`Stop-all.bat`。 現在,從瀏覽器測試第一個服務端點幾次 – `http://localhost:8081/zipkin`。 請注意,上述 4 種服務之一有意延遲。 因此,延遲是預期的最終響應,只是不要放棄。 API 調用成功后,我們可以在 zipkin UI `http://localhost:9411/zipkin/`上看到延遲統計信息。 在第一個下拉菜單中選擇第一個服務,然后單擊**查找跟蹤**按鈕。 ![](https://img.kancloud.cn/49/f5/49f527d1da61d05ecd4830b5c35f082e_1360x432.jpg) Zipkin 主界面 您應該看到這種類型的 UI,可以在其中通過查看跟蹤數據來進行性能分析。 ![](https://img.kancloud.cn/24/60/24608b79a331cf293dc5b6e65174dd4c_1349x697.jpg) 查找跟蹤 UI ![](https://img.kancloud.cn/c6/ed/c6ed31093bdf0918bbf70aba91df6251_1356x510.jpg) 一個特定的事務概述 ![](https://img.kancloud.cn/e2/af/e2af620a2c060cba46b0f0286d2d51fb_1360x694.jpg) 特定服務調用統計的詳細信息 ## 總結 在本教程中,我們學習了使用 Zipkin 分析服務調用中的延遲。 我們還了解了 Sleuth 如何幫助我們創建元數據并將其傳遞給 Zipkin。 希望這些信息對您使用 Zipkin 和 Sleuth 進行**分布式跟蹤很有幫助**。 [下載源碼](https://howtodoinjava.com/wp-content/uploads/2017/08/zipkin.zip) 將我的問題放在評論部分。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看