Spring Cloud – Tracing Services with Zipkin – Spring Cloud –Zipkin的追踪服务

最后修改: 2017年 3月 9日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we are going to add Zipkin to our spring cloud project. Zipkin is an open source project that provides mechanisms for sending, receiving, storing, and visualizing traces. This allows us to correlate activity between servers and get a much clearer picture of exactly what is happening in our services.

在本文中,我们将把Zipkin添加到我们的spring cloud项目Zipkin是一个开源项目,它提供了用于发送、接收、存储和可视化跟踪的机制。这使我们能够将服务器之间的活动联系起来,并更清楚地了解我们的服务中到底发生了什么。

This article is not an introductory article to distributed tracing or spring cloud. If you would like more information about distributed tracing, read our introduction to spring sleuth.

本文不是分布式跟踪或 spring cloud 的介绍性文章。如果您想了解有关分布式跟踪的更多信息,请阅读我们对spring sleuth的介绍。

2. Zipkin Service

2.Zipkin服务

Our Zipkin service will serve as the store for all our spans. Each span is sent to this service and collected into traces for future identification.

我们的Zipkin服务将作为我们所有跨度的存储。每个跨度都被发送到这个服务,并被收集成痕迹,以便将来识别。

2.1. Setup

2.1.设置

Create a new Spring Boot project and add these dependencies to pom.xml:

创建一个新的Spring Boot项目,并在pom.xml中添加这些依赖项:

<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-autoconfigure-ui</artifactId>
    <scope>runtime</scope>
</dependency>

For reference: you can find the latest version on Maven Central (zipkin-server, zipkin-autoconfigure-ui). Versions of the dependencies are inherited from spring-boot-starter-parent.

作为参考:您可以在Maven Centralzipkin-server, zipkin-autoconfigure-ui)找到最新版本。依赖项的版本从spring-boot-starter-parent继承。

2.2. Enabling Zipkin Server

2.2.启用Zipkin服务器

To enable the Zipkin server, we must add some annotations to the main application class:

为了启用Zipkin服务器,我们必须向主应用程序类添加一些注释。

@SpringBootApplication
@EnableZipkinServer
public class ZipkinApplication {...}

The new annotation @EnableZipkinServer will set up this server to listen for incoming spans and act as our UI for querying.

新的注解@EnableZipkinServer将设置这个服务器来监听传入的跨度,并作为我们的查询用户界面。

2.3. Configuration

2.3.配置

First, let’s create a file called bootstrap.properties in src/main/resources. Remember that this file is needed to fetch our configuration from out config server.

首先,让我们在src/main/resources中创建一个名为bootstrap.properties的文件。记住,需要这个文件来从配置服务器中获取我们的配置。

Let’s add these properties to it:

让我们把这些属性添加到它上面。

spring.cloud.config.name=zipkin
spring.cloud.config.discovery.service-id=config
spring.cloud.config.discovery.enabled=true
spring.cloud.config.username=configUser
spring.cloud.config.password=configPassword

eureka.client.serviceUrl.defaultZone=
  http://discUser:discPassword@localhost:8082/eureka/

Now let’s add a configuration file to our config repo, located at c:\Users\{username}\ on Windows or /home/{username}/ on *nix.

现在让我们添加一个配置文件到我们的配置源,在Windows上位于c:\Users\{username},在*nix上位于/home/{username}/

In this directory let’s add a file named zipkin.properties and add these contents:

在这个目录中,让我们添加一个名为zipkin.properties的文件并添加这些内容。

spring.application.name=zipkin
server.port=9411
eureka.client.region=default
eureka.client.registryFetchIntervalSeconds=5
logging.level.org.springframework.web=debug

Remember to commit the changes in this directory so that the config service will detect the changes and load the file.

记住要在这个目录中提交修改,这样配置服务就会检测到这些修改并加载文件。

2.4. Run

2.4.运行

Now let’s run our application and navigate to http://localhost:9411. We should be greeted with Zipkin’s homepage:

现在让我们运行我们的应用程序并导航到http://localhost:9411。我们应该看到Zipkin的主页。

zipkinhomepage 1

Great! Now we are ready to add some dependencies and configuration to our services that we want to trace.

很好!现在我们准备为我们要追踪的服务添加一些依赖和配置。

3. Service Configuration

3.服务配置

The setup for the resource servers is pretty much the same. In the following sections, we will detail how to set up the book-service. We will follow that up by explaining the modifications needed to apply these updates to the rating-service and gateway-service.

资源服务器的设置几乎是一样的。在接下来的章节中,我们将详细介绍如何设置book-service。随后我们将解释将这些更新应用到rating-servicegateway-service所需的修改。

3.1. Setup

3.1.设置

To begin sending spans to our Zipkin server, we will add this dependency to our pom.xml file:

为了开始向我们的Zipkin服务器发送跨度,我们将在pom.xml文件中添加这一依赖。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

For reference: you can find the latest version on Maven Central (spring-cloud-starter-zipkin).

作为参考:您可以在Maven Centralspring-cloud-starter-zipkin)找到最新版本。

3.2. Spring Config

3.2.Spring配置

We need to add some configuration so that book-service will use Eureka to find our Zipkin service. Open BookServiceApplication.java and add this code to the file:

我们需要添加一些配置,以便book-service将使用Eureka找到我们的Zipkinservice。打开BookServiceApplication.java,在文件中添加这段代码。

@Autowired
private EurekaClient eurekaClient;
 
@Autowired
private SpanMetricReporter spanMetricReporter;
 
@Autowired
private ZipkinProperties zipkinProperties;
 
@Value("${spring.sleuth.web.skipPattern}")
private String skipPattern;

// ... the main method goes here

@Bean
public ZipkinSpanReporter makeZipkinSpanReporter() {
    return new ZipkinSpanReporter() {
        private HttpZipkinSpanReporter delegate;
        private String baseUrl;

        @Override
        public void report(Span span) {
 
            InstanceInfo instance = eurekaClient
              .getNextServerFromEureka("zipkin", false);
            if (!(baseUrl != null && 
              instance.getHomePageUrl().equals(baseUrl))) {
                baseUrl = instance.getHomePageUrl();
                delegate = new HttpZipkinSpanReporter(new RestTemplate(), 
                  baseUrl, zipkinProperties.getFlushInterval(), spanMetricReporter);
                if (!span.name.matches(skipPattern)) delegate.report(span);
            }
        }
    };
}

The above configuration registers a custom ZipkinSpanReporter that gets its URL from eureka. This code also keeps track of the existing URL and only updates the HttpZipkinSpanReporter if the URL changes. This way no matter where we deploy our Zipkin server to we will always be able to locate it without restarting the service.

上述配置注册了一个自定义的ZipkinSpanReporter,从eureka获取其URL。这段代码还跟踪了现有的URL,并且只在URL发生变化时更新HttpZipkinSpanReporter。这样,无论我们把Zipkin服务器部署到哪里,都能找到它,而不需要重新启动服务。

We also import the default Zipkin properties that are loaded by spring boot and use them to manage our custom reporter.

我们还导入了由spring boot加载的默认Zipkin属性,并使用它们来管理我们的自定义报告。

3.3. Configuration

3.3.配置

Now let’s add some configuration to our book-service.properties file in the config repository:

现在,让我们向配置库中的book-service.properties文件添加一些配置。

spring.sleuth.sampler.percentage=1.0
spring.sleuth.web.skipPattern=(^cleanup.*)

Zipkin works by sampling actions on a server. By setting the spring.sleuth.sampler.percentage to 1.0, we are setting the sampling rate to 100%. The skip pattern is simply a regex used for excluding spans whose name matches.

Zipkin通过对服务器上的行动进行采样来工作。通过将spring.sleuth.sampler.percentra设置为1.0,我们就将采样率设置为100%。跳过模式是一个简单的重词,用于排除名称匹配的跨度。

The skip pattern will block all spans from being reported that start with the word ‘cleanup’. This is to stop spans originating from the spring session code base.

跳过模式将阻止所有以’cleanup’开头的跨度被报告。这是为了阻止来自spring session代码库的跨度。

3.4. Rating Service

3.4.评级服务

Follow the same steps from the book-service section above, applying the changes to the equivalent files for rating-service.

按照上面book-service部分的相同步骤,将这些变化应用于rating-service.的相应文件。

3.5. Gateway Service

3.5.网关服务

Follow the same steps book-service. But when adding the configuration to the gateway.properties add these instead:

按照同样的步骤book-service。但在将配置添加到网关.properties时,要添加这些内容。

spring.sleuth.sampler.percentage=1.0
spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*)

This will configure the gateway service not to send spans about the favicon or spring session.

这将配置网关服务不发送关于favicon或spring session的跨度。

3.6. Run

3.6.运行

If you haven’t done so already, start the config, discovery, gateway, book, rating, and zipkin services.

如果你还没有这样做,启动configdiscoverygatewaybookratingzipkin服务。

Navigate to http://localhost:8080/book-service/books.

导航到http://localhost:8080/book-service/books。

Open a new tab and navigate to http://localhost:9411. Select book-service and press the ‘Find Traces’ button. You should see a trace appear in the search results. Click that trace of opening it:

打开一个新的标签,导航到http://localhost:9411。选择book-service并按 “查找痕迹 “按钮。你应该看到搜索结果中出现一个痕迹。点击该痕迹,打开它。

zipkintrace 1

On the trace page, we can see the request broken down by service. The first two spans are created by the gateway, and the last is created by the book-service. This shows us how much time the request spent processing on the book-service, 18.379 ms, and on the gateway, 87.961 ms.

在跟踪页面上,我们可以看到按服务细分的请求。前两个跨度是由gateway创建的,最后一个跨度是由book-service创建的。这显示了请求在book-service上花费了多少时间,18.379 ms,在gateway上花费了87.961 ms。

4. Conclusion

4.结论

We have seen how easy it is to integrate Zipkin into our cloud application.

我们已经看到,将Zipkin整合到我们的云应用程序中是多么容易。

This gives us some much-needed insight into how communication travels through our application. As our application grows in complexity, Zipkin can provide us with much-needed information on where requests are spending their time. This can help us determine where things are slowing down and indicate what areas of our application need improvement.

这给我们提供了一些急需的信息,让我们了解通信是如何通过我们的应用程序进行的。随着我们的应用程序越来越复杂,Zipkin可以为我们提供非常需要的信息,说明请求在哪里花费时间。这可以帮助我们确定哪里的速度变慢了,并指出我们的应用程序有哪些地方需要改进。

As always you can find the source code over on Github.

像往常一样,你可以在Github上找到源代码over