BAEL-442 Adding spring-sleuth module and supporting code for application.
This commit is contained in:
		
							parent
							
								
									e27131f3a5
								
							
						
					
					
						commit
						f221faca4c
					
				
							
								
								
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								pom.xml
									
									
									
									
									
								
							| @ -160,6 +160,7 @@ | |||||||
|         <module>spring-security-rest</module> |         <module>spring-security-rest</module> | ||||||
|         <module>spring-security-x509</module> |         <module>spring-security-x509</module> | ||||||
|         <module>spring-session</module> |         <module>spring-session</module> | ||||||
|  |         <module>spring-sleuth</module> | ||||||
|         <module>spring-social-login</module> |         <module>spring-social-login</module> | ||||||
|         <module>spring-spel</module> |         <module>spring-spel</module> | ||||||
|         <module>spring-thymeleaf</module> |         <module>spring-thymeleaf</module> | ||||||
|  | |||||||
							
								
								
									
										64
									
								
								spring-sleuth/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								spring-sleuth/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,64 @@ | |||||||
|  | <?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.baeldung</groupId> | ||||||
|  |     <artifactId>spring-sleuth</artifactId> | ||||||
|  |     <version>1.0.0-SNAPSHOT</version> | ||||||
|  |     <packaging>jar</packaging> | ||||||
|  | 
 | ||||||
|  |     <parent> | ||||||
|  |         <groupId>org.springframework.boot</groupId> | ||||||
|  |         <artifactId>spring-boot-starter-parent</artifactId> | ||||||
|  |         <version>1.4.3.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> | ||||||
|  |     </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>Camden.SR3</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> | ||||||
| @ -0,0 +1,26 @@ | |||||||
|  | package com.baeldung.spring.session; | ||||||
|  | 
 | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.scheduling.annotation.Scheduled; | ||||||
|  | import org.springframework.stereotype.Service; | ||||||
|  | 
 | ||||||
|  | @Service | ||||||
|  | public class SchedulingService { | ||||||
|  | 
 | ||||||
|  |     private Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||||
|  |     private final SleuthService sleuthService; | ||||||
|  | 
 | ||||||
|  |     @Autowired | ||||||
|  |     public SchedulingService(SleuthService sleuthService) { | ||||||
|  |         this.sleuthService = sleuthService; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Scheduled(fixedDelay = 30000) | ||||||
|  |     public void scheduledWork() throws InterruptedException { | ||||||
|  |         logger.info("Start some work from the scheduled task"); | ||||||
|  |         sleuthService.asyncMethod(); | ||||||
|  |         logger.info("End work from scheduled task"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,71 @@ | |||||||
|  | package com.baeldung.spring.session; | ||||||
|  | 
 | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.web.bind.annotation.GetMapping; | ||||||
|  | import org.springframework.web.bind.annotation.RestController; | ||||||
|  | 
 | ||||||
|  | import java.util.concurrent.Executor; | ||||||
|  | 
 | ||||||
|  | @RestController | ||||||
|  | public class SleuthController { | ||||||
|  | 
 | ||||||
|  |     private Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||||
|  |     private final SleuthService sleuthService; | ||||||
|  |     private final Executor executor; | ||||||
|  | 
 | ||||||
|  |     @Autowired | ||||||
|  |     public SleuthController(SleuthService sleuthService, Executor executor) { | ||||||
|  |         this.sleuthService = sleuthService; | ||||||
|  |         this.executor = executor; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @GetMapping("/") | ||||||
|  |     public String helloSleuth() { | ||||||
|  |         logger.info("Hello Sleuth"); | ||||||
|  |         return "success"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @GetMapping("/same-span") | ||||||
|  |     public String helloSleuthSameSpan() throws InterruptedException { | ||||||
|  |         logger.info("Same Span"); | ||||||
|  |         sleuthService.doSomeWorkSameSpan(); | ||||||
|  |         return "success"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @GetMapping("/new-span") | ||||||
|  |     public String helloSleuthNewSpan() throws InterruptedException { | ||||||
|  |         logger.info("New Span"); | ||||||
|  |         sleuthService.doSomeWorkNewSpan(); | ||||||
|  |         return "success"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @GetMapping("/new-thread") | ||||||
|  |     public String helloSleuthNewThread() { | ||||||
|  |         logger.info("New Thread"); | ||||||
|  |         Runnable runnable = () -> { | ||||||
|  |             try { | ||||||
|  |                 Thread.sleep(1000L); | ||||||
|  |             } catch (InterruptedException e) { | ||||||
|  |                 e.printStackTrace(); | ||||||
|  |             } | ||||||
|  |             logger.info("I'm inside the new thread - with a new span"); | ||||||
|  |         }; | ||||||
|  |         executor.execute(runnable); | ||||||
|  | 
 | ||||||
|  |         logger.info("I'm done - with the original span"); | ||||||
|  |         return "success"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @GetMapping("/async") | ||||||
|  |     public String helloSleuthAsync() throws InterruptedException { | ||||||
|  |         logger.info("Before Async Method Call"); | ||||||
|  |         sleuthService.asyncMethod(); | ||||||
|  |         logger.info("After Async Method Call"); | ||||||
|  |         return "success"; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,46 @@ | |||||||
|  | package com.baeldung.spring.session; | ||||||
|  | 
 | ||||||
|  | import org.slf4j.Logger; | ||||||
|  | import org.slf4j.LoggerFactory; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.cloud.sleuth.Span; | ||||||
|  | import org.springframework.cloud.sleuth.Tracer; | ||||||
|  | import org.springframework.scheduling.annotation.Async; | ||||||
|  | import org.springframework.stereotype.Service; | ||||||
|  | 
 | ||||||
|  | @Service | ||||||
|  | public class SleuthService { | ||||||
|  |     private Logger logger = LoggerFactory.getLogger(this.getClass()); | ||||||
|  |     private final Tracer tracer; | ||||||
|  | 
 | ||||||
|  |     @Autowired | ||||||
|  |     public SleuthService(Tracer tracer) { | ||||||
|  |         this.tracer = tracer; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void doSomeWorkSameSpan() throws InterruptedException { | ||||||
|  |         Thread.sleep(1000L); | ||||||
|  |         logger.info("Doing some work"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void doSomeWorkNewSpan() throws InterruptedException { | ||||||
|  |         logger.info("I'm in the original span"); | ||||||
|  | 
 | ||||||
|  |         Span newSpan = tracer.createSpan("newSpan"); | ||||||
|  |         try { | ||||||
|  |             Thread.sleep(1000L); | ||||||
|  |             logger.info("I'm in the new span doing some cool work that needs its own span"); | ||||||
|  |         } finally { | ||||||
|  |             tracer.close(newSpan); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         logger.info("I'm in the original span"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Async | ||||||
|  |     public void asyncMethod() throws InterruptedException { | ||||||
|  |         logger.info("Start Async Method"); | ||||||
|  |         Thread.sleep(1000L); | ||||||
|  |         logger.info("End Async Method"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,11 @@ | |||||||
|  | package com.baeldung.spring.session; | ||||||
|  | 
 | ||||||
|  | import org.springframework.boot.SpringApplication; | ||||||
|  | import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||||
|  | 
 | ||||||
|  | @SpringBootApplication | ||||||
|  | public class SleuthWebApp { | ||||||
|  |     public static void main(String[] args) { | ||||||
|  |         SpringApplication.run(SleuthWebApp.class, args); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,56 @@ | |||||||
|  | package com.baeldung.spring.session; | ||||||
|  | 
 | ||||||
|  | import org.springframework.beans.factory.BeanFactory; | ||||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor; | ||||||
|  | import org.springframework.context.annotation.Bean; | ||||||
|  | import org.springframework.context.annotation.Configuration; | ||||||
|  | import org.springframework.scheduling.annotation.AsyncConfigurerSupport; | ||||||
|  | import org.springframework.scheduling.annotation.EnableAsync; | ||||||
|  | import org.springframework.scheduling.annotation.EnableScheduling; | ||||||
|  | import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||||||
|  | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||||||
|  | import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||||||
|  | 
 | ||||||
|  | import java.util.concurrent.Executor; | ||||||
|  | import java.util.concurrent.Executors; | ||||||
|  | 
 | ||||||
|  | @Configuration | ||||||
|  | @EnableAsync | ||||||
|  | @EnableScheduling | ||||||
|  | public class ThreadConfig extends AsyncConfigurerSupport implements SchedulingConfigurer{ | ||||||
|  | 
 | ||||||
|  |     @Autowired | ||||||
|  |     private BeanFactory beanFactory; | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public Executor executor() { | ||||||
|  |         return makeExecutor(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public Executor getAsyncExecutor() { | ||||||
|  |         return makeExecutor(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { | ||||||
|  |         scheduledTaskRegistrar.setScheduler(schedulingExecutor()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean(destroyMethod = "shutdown") | ||||||
|  |     public Executor schedulingExecutor() { | ||||||
|  |         return Executors.newScheduledThreadPool(100); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private Executor makeExecutor() { | ||||||
|  |         ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); | ||||||
|  |         threadPoolTaskExecutor.setCorePoolSize(5); | ||||||
|  |         threadPoolTaskExecutor.setMaxPoolSize(10); | ||||||
|  |         threadPoolTaskExecutor.initialize(); | ||||||
|  | 
 | ||||||
|  |         return new LazyTraceExecutor(beanFactory, threadPoolTaskExecutor); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | } | ||||||
							
								
								
									
										1
									
								
								spring-sleuth/src/main/resources/application.properties
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								spring-sleuth/src/main/resources/application.properties
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | spring.application.name=Baeldung Sleuth Tutorial | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user