Skip to content

Spring Cloud Microservices Log Tracing example using Sleuth and Zipkin

Notifications You must be signed in to change notification settings

itqpleyva/SpringBootMicroservicesLogTrace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#SpringMicroservicesLogTracing

Spring Cloud Microservices Log Tracing example using Sleuth and Zipkin

In this example we create Zipkin server to trace all logs from microservices

Dependencies for Zipkin server :

<dependencies>	
	<dependency>	
		<groupId>org.springframework.boot</groupId>	
		<artifactId>spring-boot-starter-web</artifactId>	
					<exclusions>
				<exclusion>
					<groupId>ch.qos.logback</groupId>
					<artifactId>logback-classic</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.apache.logging.log4j</groupId>
					<artifactId>log4j-to-slf4j</artifactId>
				</exclusion>
			</exclusions>	
		</dependency>	
	<dependency>	
		<groupId>org.springframework.boot</groupId>		
		<artifactId>spring-boot-devtools</artifactId>		
		<scope>runtime</scope>	
	</dependency>	
	<dependency>		
		<groupId>org.springframework.boot</groupId>		
		<artifactId>spring-boot-starter-test</artifactId>		
		<scope>test</scope>	
	</dependency>	
	<dependency>	
		<groupId>io.zipkin.java</groupId>	
		<artifactId>zipkin-server</artifactId>	
		<version>2.11.5</version>
	</dependency>	
	<dependency>	
		<groupId>io.zipkin.java</groupId>	
		<artifactId>zipkin-autoconfigure-ui</artifactId>	
		<version>2.11.5</version>	
	</dependency>
</dependencies>

Dependencies for Sleuth microservice:

  <dependencies>   
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-sleuth</artifactId>
		    <version>2.0.2.RELEASE</version>
		</dependency>
      <dependency>
	 <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-starter-web</artifactId>
      </dependency> 
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zipkin</artifactId>
			<version>2.0.2.RELEASE</version>
		</dependency>
   </dependencies>

Zipkin server main class:

	@SpringBootApplication
	@EnableZipkinServer
	public class Main {

	public static void main(String[] args) {
		
		SpringApplication.run(Main.class, args);

		}

	}

Sleuth microservice main class:

	@SpringBootApplication
	@RestController
	public class Main {

		private static final Logger LOG = Logger.getLogger(Main.class.getName());
		
		public static void main(String[] args) {
			
			SpringApplication.run(Main.class, args);
		}
		  @RequestMapping("/") 
		  public String home() { 
			
			LOG.log(Level.INFO, "Creating new log");
			
			return "Hello from microservice"; 

		  } 
	}

Zipkin server application.properties:

	spring.application.name=zipkinserver
	server.port:9411
	management.metrics.web.server.auto-time-requests=false

Sleuth microservice application.properties:

	spring.application.name=SleuthExample
	spring.zipkin.baseUrl=http://localhost:9411/
	spring.sleuth.sampler.probability = 1.0

Final result:

If we go to: http://localhost:8080/ we can see: "Hello from microservice" message

If we go to: http://localhost:8080/home we can see: "Error type=Not Found, status=404" message

Then we can go to zipkin server(http://localhost:9411/zipkin/) to see bouth logs:

If we select the red log related to the http://localhost:8080/home request, we can see the details of error log:

About

Spring Cloud Microservices Log Tracing example using Sleuth and Zipkin

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages