Spring Boot 简明教程

Spring Boot - Tracing Micro Service Logs

如果出现任何问题,大多数开发人员都面临着跟踪日志的困难。这个问题可以通过弹簧云 Sleuth 和 ZipKin 服务器解决,以用于弹簧启动应用程序。

Most developers face difficulty of tracing logs if any issue occurred. This can be solved by Spring Cloud Sleuth and ZipKin server for Spring Boot application.

Spring Cloud Sleuth

弹簧云 Sleuth 日志以以下格式打印 −

Spring cloud Sleuth logs are printed in the following format −

[application-name,traceid,spanid,zipkin-export]

其中,

Where,

  1. Application-name = Name of the application

  2. Traceid = each request and response traceid is same when calling same service or one service to another service.

  3. Spanid = Span Id is printed along with Trace Id. Span Id is different every request and response calling one service to another service.

  4. Zipkin-export = By default it is false. If it is true, logs will be exported to the Zipkin server.

现在,按照如下所示在您的构建配置文件中添加 Spring Cloud Starter Sleuth 依赖项 −

Now, add the Spring Cloud Starter Sleuth dependency in your build configuration file as follows −

Maven 用户可以在他们的 pom.xml 文件中添加以下依赖 −

Maven users can add the following dependency in your pom.xml file −

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

Gradle 用户可以在他们的 build.gradle 文件中添加以下依赖 −

Gradle users can add the following dependency in your build.gradle file −

compile('org.springframework.cloud:spring-cloud-starter-sleuth')

现在,按照此处所示,将日志添加到您的 Spring Boot 应用程序 Rest 控制器类文件中 −

Now, add the Logs into your Spring Boot application Rest Controller class file as shown here −

package com.tutorialspoint.sleuthapp;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SleuthappApplication {
   private static final Logger LOG = Logger.getLogger(SleuthappApplication.class.getName());
   public static void main(String[] args) {
      SpringApplication.run(SleuthappApplication.class, args);
   }
   @RequestMapping("/")
   public String index() {
      LOG.log(Level.INFO, "Index API is calling");
      return "Welcome Sleuth!";
   }
}

现在,按照所示在 application.properties 文件中添加应用程序名称 −

Now, add the application name in application.properties file as shown −

spring.application.name = tracinglogs

完整构建配置文件的代码如下所示 −

The complete code for build configuration file is given below −

Maven – pom.xml

Maven – pom.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>sleuthapp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>sleuthapp</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-sleuth</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

Gradle – build.gradle

Gradle – build.gradle

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
   compile('org.springframework.cloud:spring-cloud-starter-sleuth')
   compile('org.springframework.boot:spring-boot-starter-web')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

您可以创建一个可执行 JAR 文件,并通过使用以下 Maven 或 Gradle 命令来运行 Spring Boot 应用程序。

You can create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands.

对于 Maven,可以使用以下命令:

For Maven, you can use the following command −

mvn clean install

“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

对于 Gradle,可以使用以下命令:

For Gradle, you can use the following command −

gradle clean build

“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

现在,通过使用此处所示命令运行 JAR 文件:

Now, run the JAR file by using the command shown here −

java –jar <JARFILE>

现在,应用程序已在 Tomcat 端口 8080 上启动。

Now, the application has started on the Tomcat port 8080.

started application on tomcat port 8080

现在,在 Web 浏览器中按 URL 并查看控制台日志中的输出。

Now, hit the URL in your web browser and see the output in console log.

output welcome sleuth

您可以在控制台窗口中看到以下日志。注意,日志采用以下格式打印 [应用程序名称、traceid、spanid、zipkin-export]

You can see the following logs in the console window. Observe that log is printed in the following format [application-name, traceid, spanid, zipkin-export]

log is printed

Zipkin Server

Zipkin 是一个监视和管理 Spring Boot 应用程序的 Spring Cloud Sleuth 日志的应用程序。要构建一个 Zipkin 服务器,我们需要在构建配置文件中添加 Zipkin UI 和 Zipkin Server 依赖。

Zipkin is an application that monitors and manages the Spring Cloud Sleuth logs of your Spring Boot application. To build a Zipkin server, we need to add the Zipkin UI and Zipkin Server dependencies in our build configuration file.

Maven 用户可以在他们的 pom.xml 文件中添加以下依赖 −

Maven users can add the following dependency in your pom.xml file −

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

Gradle 用户可以在 build.gradle 文件中添加如下依赖:

Gradle users can add the below dependency in your build.gradle file −

compile('io.zipkin.java:zipkin-autoconfigure-ui')
compile('io.zipkin.java:zipkin-server')

现在,在应用程序属性文件中配置 server.port = 9411。

Now, configure the server.port = 9411 in application properties file.

对于属性文件用户,在 application.properties 文件中添加以下属性。

For properties file users, add the below property in application.properties file.

server.port = 9411

对于 YAML 用户,在 application.yml 文件中添加以下属性。

For YAML users, add the below property in application.yml file.

server:
   port: 9411

在您的主 Spring Boot 应用程序类文件中添加 @EnableZipkinServer 注解。@EnableZipkinServer 注解用于启用您的应用程序充当 Zipkin 服务器。

Add the @EnableZipkinServer annotation in your main Spring Boot application class fie. The @EnableZipkinServer annotation is used to enable your application act as a Zipkin server.

package com.tutorialspoint.zipkinapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer;

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

完整构建配置文件的代码如下所示。

The code for complete build configuration file is given below.

Maven – pom.xml

Maven – pom.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>zipkinapp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>zipkinapp</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath /> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>io.zipkin.java</groupId>
         <artifactId>zipkin-server</artifactId>
      </dependency>
      <dependency>
         <groupId>io.zipkin.java</groupId>
         <artifactId>zipkin-autoconfigure-ui</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

Gradle – build.gradle

Gradle – build.gradle

buildscript {
   ext {
      springBootVersion = '1.5.9.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
   compile('io.zipkin.java:zipkin-autoconfigure-ui')
   compile('io.zipkin.java:zipkin-server')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

您可以创建一个可执行 JAR 文件,并通过使用以下 Maven 或 Gradle 命令运行 Spring Boot 应用程序:

You can create an executable JAR file, and run the Spring Boot application by using the below Maven or Gradle commands −

对于 Maven,使用以下给出的命令 −

For Maven, use the command given below −

mvn clean install

“BUILD SUCCESS”之后,您可以在目标目录中找到 JAR 文件。

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

对于 Gradle,使用以下给出的命令 −

For Gradle, use the command given below −

gradle clean build

“BUILD SUCCESSFUL”之后,您可以在 build/libs 目录中找到 JAR 文件。

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

使用显示的命令运行 JAR 文件 −

Run the JAR file by using the command shown −

java –jar <JARFILE>

现在,应用程序已在 Tomcat 端口 9411 上启动,如下所示 −

Now, the application has started on the Tomcat port 9411 as shown below −

output tomcat port 9411

现在,点击下面的 URL 并查看 Zipkin 服务器 UI。

Now, hit the below URL and see the Zipkin server UI.

zipkin server ui

然后,在客户端服务应用程序中添加以下依赖,并指出 Zipkin 服务器 URL 以通过 Zipkin UI 追踪微服务日志。

Then, add the following dependency in your client service application and point out the Zipkin Server URL to trace the microservice logs via Zipkin UI.

现在,在构建配置文件中添加 Spring Cloud Starter Zipkin 依赖,如下所示 −

Now, add the Spring Cloud Starter Zipkin dependency in your build configuration file as shown −

Maven 用户可以在 pom.xml 文件中添加以下依赖 −

Maven users can add the following dependency in pom.xml file −

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

Gradle 用户可以在 build.gradle 文件中添加以下依赖 −

Gradle users can add the below dependency in build.gradle file −

compile('org.springframework.cloud:spring-cloud-sleuth-zipkin')

现在,在 Spring Boot 应用程序中添加 Always Sampler Bean 以将日志导出至 Zipkin 服务器。

Now, add the Always Sampler Bean in your Spring Boot application to export the logs into Zipkin server.

@Bean
public AlwaysSampler defaultSampler() {
   return new AlwaysSampler();
}

如果你添加了 AlwaysSampler Bean,Spring Sleuth Zipkin Export 选项会自动从 false 变为 true。

If you add the AlwaysSampler Bean, then automatically Spring Sleuth Zipkin Export option will change from false to true.

接下来,在客户端服务 application.properties 文件中配置你的 Zipkin 服务器基本 URL。

Next, configure your Zipkin Server base URL in client service application.properties file.

spring.zipkin.baseUrl = http://localhost:9411/zipkin/

然后,提供跟踪 ID 并找出 Zipkin UI 中的跟踪。

Then, provide the trace id and find the traces in Zipkin UI.