将分布式跟踪与 Azure Spring Cloud 配合使用Use distributed tracing with Azure Spring Cloud

使用 Azure Spring Cloud 中的分布式跟踪工具,可以轻松地调试和监视复杂问题。With the distributed tracing tools in Azure Spring Cloud, you can easily debug and monitor complex issues. Azure Spring Cloud 将 Spring Cloud Sleuth 与 Azure 的 Application Insights 集成。Azure Spring Cloud integrates Spring Cloud Sleuth with Azure's Application Insights. 这种集成可以通过 Azure 门户提供强大的分布式跟踪功能。This integration provides powerful distributed tracing capability from the Azure portal.

在本文中,学习如何:In this article, you learn how to:

  • 在 Azure 门户中启用分布式跟踪。Enable distributed tracing in the Azure portal.
  • 将 Spring Cloud Sleuth 添加到应用程序。Add Spring Cloud Sleuth to your application.
  • 查看微服务应用程序的依赖项映射。View dependency maps for your microservice applications.
  • 通过不同的筛选器搜索跟踪数据。Search tracing data with different filters.

先决条件Prerequisites

若要完成这些过程,需要一个已预配且正在运行的 Azure Spring Cloud 服务。To follow these procedures, you need an Azure Spring Cloud service that is already provisioned and running. 请完成部署第一个 Azure Spring Cloud 应用程序快速入门,以预配和运行 Azure Spring Cloud 服务。Complete the Deploy your first Azure Spring Cloud application quickstart to provision and run an Azure Spring Cloud service.

添加依赖项Add dependencies

  1. 将以下行添加到 application.properties 文件:Add the following line to the application.properties file:

    spring.zipkin.sender.type = web
    

    完成此更改后,Zipkin 发送方就可以将内容发送到 Web。After this change, the Zipkin sender can send to the web.

  2. 如果已按 Azure Spring Cloud 应用程序准备指南进行了操作,则可跳过此步骤。Skip this step if you followed our guide to preparing an Azure Spring Cloud application. 否则,请转到本地开发环境并编辑 pom.xml 文件,以添加以下 Spring Cloud Sleuth 依赖项:Otherwise, go to your local development environment and edit your pom.xml file to include the following Spring Cloud Sleuth dependency:

    • Spring Boot 版本低于 2.4.x。Spring boot version < 2.4.x.

      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-sleuth</artifactId>
                  <version>${spring-cloud-sleuth.version}</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-sleuth</artifactId>
          </dependency>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-zipkin</artifactId>
          </dependency>
      </dependencies>
      
    • Spring Boot 版本高于等于 2.4.x。Spring boot version >= 2.4.x.

      <dependencyManagement>
          <dependencies>
            <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-sleuth</artifactId>
                  <version>${spring-cloud-sleuth.version}</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-sleuth</artifactId>
          </dependency>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-sleuth-zipkin</artifactId>
           </dependency>
      </dependencies>
      
  3. 再次为 Azure Spring Cloud 服务执行生成和部署操作,使之反映这些更改。Build and deploy again for your Azure Spring Cloud service to reflect these changes.

修改采样率Modify the sample rate

可以通过修改采样率,更改遥测数据收集速率。You can change the rate at which your telemetry is collected by modifying the sample rate. 例如,如果要按平常一半的频率采样,请打开 application.properties 文件并更改以下行:For example, if you want to sample half as often, open your application.properties file, and change the following line:

spring.sleuth.sampler.probability=0.5

如果已生成并部署应用程序,则可修改采样率。If you have already built and deployed an application, you can modify the sample rate. 为此,可在 Azure CLI 或 Azure 门户中添加上一行作为环境变量。Do so by adding the previous line as an environment variable in the Azure CLI or the Azure portal.

启用 Application InsightsEnable Application Insights

  1. 转到 Azure 门户中的 Azure Spring Cloud 服务页面。Go to your Azure Spring Cloud service page in the Azure portal.
  2. 在“监视”页上,选择“分布式跟踪”。 On the Monitoring page, select Distributed Tracing.
  3. 选择“编辑设置”,以便编辑现有设置或添加新设置。Select Edit setting to edit or add a new setting.
  4. 新建一个 Application Insights 查询,或选择一个现有的。Create a new Application Insights query, or select an existing one.
  5. 选择要监视哪个日志记录类别,并指定保留时间(天)。Choose which logging category you want to monitor, and specify the retention time in days.
  6. 选择“应用”以应用新跟踪。Select Apply to apply the new tracing.

查看应用程序映射View the application map

返回到“分布式跟踪”页面,选择“查看应用程序映射”。 Return to the Distributed Tracing page and select View application map. 查看应用程序和监视设置的视觉表现形式。Review the visual representation of your application and monitoring settings. 若要了解如何使用应用程序,请参阅应用程序映射:会审分布式应用程序To learn how to use the application map, see Application Map: Triage distributed applications.

使用搜索函数查询其他特定的遥测项。Use the search function to query for other specific telemetry items. 在“分布式跟踪”页面上,选择“搜索”。 On the Distributed Tracing page, select Search. 有关如何使用搜索函数的详细信息,请参阅在 Application Insights 中使用搜索For more information on how to use the search function, see Using Search in Application Insights.

使用 Application InsightsUse Application Insights

除了应用程序映射和搜索功能,Application Insights 还提供监视功能。Application Insights provides monitoring capabilities in addition to the application map and search function. 在 Azure 门户中搜索应用程序的名称,然后打开 Application Insights 页面来查找监视信息。Search the Azure portal for your application's name, and then open an Application Insights page to find monitoring information. 有关如何使用这些工具的更多指南,请查看 Azure Monitor 日志查询For more guidance on how to use these tools, check out Azure Monitor log queries.

禁用 Application InsightsDisable Application Insights

  1. 转到 Azure 门户中的 Azure Spring Cloud 服务页面。Go to your Azure Spring Cloud service page in the Azure portal.
  2. 在“监视”部分,选择“分布式跟踪”。 On Monitoring, select Distributed Tracing.
  3. 选择“禁用”,禁用 Application Insights。Select Disable to disable Application Insights.

后续步骤Next steps

本文介绍了如何在 Azure Spring Cloud 中启用并了解分布式跟踪。In this article, you learned how to enable and understand distributed tracing in Azure Spring Cloud. 若要了解如何将服务绑定到应用程序,请参阅将 Azure Cosmos DB 数据库绑定到 Azure Spring Cloud 应用程序To learn about binding services to an application, see Bind an Azure Cosmos DB database to an Azure Spring Cloud application.