If problems occur during the operation of the application, most developers are difficult to trace log. This can be solved by Spring Cloud Sleuth and ZipKin for Spring Boot server applications.
Spring Cloud Sleuth
Spring cloud Sleuth journal printed in the following format -
[application-name,traceid,spanid,zipkin-export]
In the above format,
Application-name = name of the application
Traceid = traceid each request and response is the same when a call to the same service or service to another.
Spanid = Span Id printed with Trace Id. The service call for a response to each request and another service, Span Id is different.
Zipkin-export = by default false
. If so, then the server logs exported to Zipkin.
Now add Spring Cloud Starter Sleuth dependencies in profiling, as follows -
<dependency>
<groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
Gradle users can build.gradle add the following file dependencies -
compile('org.springframework.cloud:spring-cloud-starter-sleuth')
Now, added to Logs Rest Controller Spring Boot application class files, as follows -
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!"; } }
Now, application.properties add the application name of the file, as shown below -
spring.application.name = tracinglogs
The complete code profiling is as follows -
Maven build file - 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>spring-cloud-dependencies