Spring Boot 跟踪微服务日志
-
Spring Boot 跟踪微服务日志
如果发生任何问题,大多数开发人员都将面临跟踪日志的困难。可以通过Spring Cloud Sleuth和ZipKin服务器针对Spring Boot应用程序解决此问题。 -
Spring Cloud Sleuth
Spring Cloud Sleuth日志以以下格式打印-[application-name,traceid,spanid,zipkin-export]
说明:- application-name = 应用程序名称
- traceid = 在将同一服务或一个服务调用到另一个服务时,每个请求和响应的traceid都相同。
- spanid = Span Id与跟踪traceid一起打印。每个请求和响应将一个服务调用到另一个服务时,Span ID都不相同。
- zipkin-export = 默认情况下为false。如果为true,则日志将导出到Zipkin服务器。
现在,在您的构建配置文件中添加Spring Cloud Starter Sleuth依赖项,如下所示:Maven用户可以在pom.xml文件中添加以下依赖项-对于Maven,将Docker Maven插件添加到您的构建配置文件pom.xml中<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
Gradle用户可以在build.gradle文件中添加以下依赖项-implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
现在,将Logs添加到您的Spring Boot应用程序Rest Controller类文件中,如下所示-package com.jc2182.demo; 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
下面给出了构建配置文件的完整代码-<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.M4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.jc2182</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <docker.image.prefix>spring-boot-jc2182</docker.image.prefix> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> <version>3.0.0.M1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
Gradle – build.gradleplugins { id 'org.springframework.boot' version '2.3.0.M4' id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'java' } group = 'com.jc2182' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() maven { url 'https://repo.spring.io/milestone' } } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-sleuth:3.0.0.M1' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } } test { useJUnitPlatform() }
您可以创建一个可执行的JAR文件,并使用以下Maven或Gradle命令运行Spring Boot应用程序。对于Maven,您可以使用以下命令-mvn clean install
在“BUILD SUCCESS”之后,您可以在target目录下找到JAR文件。对于Gradle,您可以使用以下命令-gradle clean build
在“BUILD SUCCESSFUL”之后,您可以在build/libs目录下找到JAR文件。现在,使用如下所示的命令运行JAR文件:java –jar <JARFILE>
您会发现该应用程序已在Tomcat端口8080上启动。现在在您的Web浏览器中访问URL http://localhost:8080,您可以看到以下输出-您可以在控制台窗口中看到以下日志。观察日志以以下格式打印:[application-name,traceid,spaid,zipkin-export] -
Zipkin服务器
Zipkin是一个用于监视和管理Spring Boot应用程序的Spring Cloud Sleuth日志的应用程序。要构建Zipkin服务器,我们需要在构建配置文件中添加Zipkin UI和Zipkin Server依赖项。Maven用户可以在pom.xml文件中添加以下依赖项-对于Maven,将Docker 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文件中添加以下依赖项-implementation 'io.zipkin.java:zipkin-autoconfigure-ui' implementation 'io.zipkin.java:zipkin-server'
现在,在应用程序属性文件中配置server.port = 9411。对于属性文件用户,在application.properties文件中添加以下属性。server.port=9411 management.metrics.web.server.auto-time-requests=false
对于YAML用户,在application.yml文件中添加以下属性。server: port: 9411 management: metrics: web: server: auto-time-requests: false
在您的主要Spring Boot应用程序类文件中添加@EnableZipkinServer注解。@EnableZipkinServer注解用于使您的应用程序充当Zipkin服务器。package com.jc2182.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.13.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.jc2182</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Gradle – build.gradleplugins { id 'org.springframework.boot' version '2.1.13.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'java' } group = 'com.jc2182' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() maven { url 'https://repo.spring.io/milestone' } } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'io.zipkin.java:zipkin-server:2.12.9' implementation 'io.zipkin.java:zipkin-autoconfigure-ui:2.12.9' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } } test { useJUnitPlatform() }
您可以创建一个可执行的JAR文件,并使用以下Maven或Gradle命令运行Spring Boot应用程序。对于Maven,您可以使用以下命令-mvn clean install
在“BUILD SUCCESS”之后,您可以在target目录下找到JAR文件。对于Gradle,您可以使用以下命令-gradle clean build
在“BUILD SUCCESSFUL”之后,您可以在build/libs目录下找到JAR文件。现在,使用如下所示的命令运行JAR文件:java –jar <JARFILE>
您会发现该应用程序已在Tomcat端口9411上启动。现在在您的Web浏览器中访问URL http://localhost:9411/zipkin,您可以看到以下输出-然后,在客户端服务应用程序中添加以下依赖项,并指出Zipkin服务器URL,以通过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文件中添加以下依赖项-implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
现在,在Spring Boot应用程序中添加Always Sampler Bean,以将日志导出到Zipkin服务器。@Bean public AlwaysSampler defaultSampler() { return new AlwaysSampler(); }
如果添加AlwaysSampler Bean,则Spring Sleuth Zipkin Export选项将自动从false更改为true。接下来,在客户端服务application.properties文件中配置Zipkin Server基本URL。spring.zipkin.baseUrl = http://localhost:9411/zipkin/
然后,提供跟踪ID,并在Zipkin UI中找到跟踪。 -- http://localhost:9411/zipkin/traces/{traceid}/