英文链接源链接 [20791]
contenteditable="true"

Tracing Micro Service Logs 介绍

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

春云侦探

Spring Cloud Sleuth日志以以下格式打印-

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

哪里,

  • 应用程序名称=应用程序名称

  • Traceid =当调用同一服务或一个服务到另一个服务时,每个请求和响应的traceid都相同。

  • Spanid =跨度ID与跟踪ID一起打印。每个请求和响应将一个服务调用到另一个服务时,Span ID都不相同。

  • Zipkin-export =默认情况下为false。如果为true,则日志将导出到Zipkin服务器。

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

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

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

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

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

现在,将Logs添加到您的Spring Boot应用程序Rest Controller类文件中,如下所示-

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文件中添加应用程序名称,如下所示:

spring.application.name = tracinglogs

下面给出了构建配置文件的完整代码-

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

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应用程序。

对于Maven,您可以使用以下命令-

mvn clean install

在" BUILD SUCCESS"之后,您可以在目标目录下找到JAR文件。

对于Gradle,您可以使用以下命令-

gradle clean build

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

现在,使用此处显示的命令运行JAR文件-

java –jar <JARFILE> 

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

Started Application on Tomcat Port 8080

现在,在Web浏览器中单击URL,然后在控制台日志中查看输出。

http://localhost:8080/

Output Welcome Sleuth

您可以在控制台窗口中看到以下日志。观察日志以以下格式打印:[应用程序名称,traceid,spaid,zipkin-export]

Log is Printed

Zipkin服务器

Zipkin is an application that monitors and manages the 春云侦探 logs of your Spring Boot application. To build a Zipkin server, we need to add the Zipkin UI and Zipkin服务器 dependencies in our build configuration file.

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

<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文件中添加以下依赖项-

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

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

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

server.port = 9411

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

server:
   port: 9411

在您的主要Spring Boot应用程序类fie中添加@EnableZipkinServer批注。 @EnableZipkinServer批注用于使您的应用程序充当Zipkin服务器。

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);
   }
}

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

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

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应用程序-

对于Maven,请使用下面给出的命令-

mvn clean install

在" BUILD SUCCESS"之后,您可以在目标目录下找到JAR文件。

对于Gradle,请使用下面给出的命令-

gradle clean build

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

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

java –jar <JARFILE> 

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

Output Tomcat Port 9411

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

http://localhost:9411/zipkin/

Zipkin server UI

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

现在,在您的构建配置文件中添加Spring Cloud Starter Zipkin依赖关系,如下所示:

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

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

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

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

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

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

如果添加AlwaysSampler Bean,则Spring Sleuth Zipkin Exportoptions将自动从false更改为true。

Next, configure your Zipkin服务器 base URL in client service application.properties file.

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

然后,提供跟踪ID,并在Zipkin UI中找到跟踪。

http://localhost:9411/zipkin/traces/{traceid}/

点我分享笔记