如果应用程序在运行过程发生问题,大多数开发人员都难以跟踪日志。 这可以通过用于Spring Boot应用程序的Spring Cloud Sleuth和ZipKin服务器来解决。
Spring Cloud Sleuth
Spring cloud Sleuth日志以下列格式打印 -
[application-name,traceid,spanid,zipkin-export]
在上面格式中,
Application-name = 应用程序的名称
Traceid = 每个请求和响应traceid在调用相同服务或一个服务到另一个服务时是相同的。
Spanid = Span Id与Trace Id一起打印。对于将一个服务调用到另一个服务的每个请求和响应,Span Id都不同。
Zipkin-export = 默认情况下为false
。 如果是,则将日志导出到Zipkin服务器。
现在,在构建配置文件中添加Spring Cloud Starter Sleuth依赖项,如下所示 -
<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.yiibai.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.yiibai</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