Skip to content

Commit f221fac

Browse files
committed
BAEL-442 Adding spring-sleuth module and supporting code for application.
1 parent e27131f commit f221fac

8 files changed

Lines changed: 276 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
<module>spring-security-rest</module>
161161
<module>spring-security-x509</module>
162162
<module>spring-session</module>
163+
<module>spring-sleuth</module>
163164
<module>spring-social-login</module>
164165
<module>spring-spel</module>
165166
<module>spring-thymeleaf</module>

‎spring-sleuth/pom.xml‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>spring-sleuth</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.4.3.RELEASE</version>
16+
<relativePath/> <!-- lookup parent from repository -->
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.cloud</groupId>
28+
<artifactId>spring-cloud-starter-sleuth</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<dependencyManagement>
43+
<dependencies>
44+
<dependency>
45+
<groupId>org.springframework.cloud</groupId>
46+
<artifactId>spring-cloud-dependencies</artifactId>
47+
<version>Camden.SR3</version>
48+
<type>pom</type>
49+
<scope>import</scope>
50+
</dependency>
51+
</dependencies>
52+
</dependencyManagement>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
64+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.spring.session;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.scheduling.annotation.Scheduled;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class SchedulingService {
11+
12+
private Logger logger = LoggerFactory.getLogger(this.getClass());
13+
private final SleuthService sleuthService;
14+
15+
@Autowired
16+
public SchedulingService(SleuthService sleuthService) {
17+
this.sleuthService = sleuthService;
18+
}
19+
20+
@Scheduled(fixedDelay = 30000)
21+
public void scheduledWork() throws InterruptedException {
22+
logger.info("Start some work from the scheduled task");
23+
sleuthService.asyncMethod();
24+
logger.info("End work from scheduled task");
25+
}
26+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.spring.session;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.util.concurrent.Executor;
10+
11+
@RestController
12+
public class SleuthController {
13+
14+
private Logger logger = LoggerFactory.getLogger(this.getClass());
15+
private final SleuthService sleuthService;
16+
private final Executor executor;
17+
18+
@Autowired
19+
public SleuthController(SleuthService sleuthService, Executor executor) {
20+
this.sleuthService = sleuthService;
21+
this.executor = executor;
22+
}
23+
24+
@GetMapping("/")
25+
public String helloSleuth() {
26+
logger.info("Hello Sleuth");
27+
return "success";
28+
}
29+
30+
@GetMapping("/same-span")
31+
public String helloSleuthSameSpan() throws InterruptedException {
32+
logger.info("Same Span");
33+
sleuthService.doSomeWorkSameSpan();
34+
return "success";
35+
}
36+
37+
@GetMapping("/new-span")
38+
public String helloSleuthNewSpan() throws InterruptedException {
39+
logger.info("New Span");
40+
sleuthService.doSomeWorkNewSpan();
41+
return "success";
42+
}
43+
44+
@GetMapping("/new-thread")
45+
public String helloSleuthNewThread() {
46+
logger.info("New Thread");
47+
Runnable runnable = () -> {
48+
try {
49+
Thread.sleep(1000L);
50+
} catch (InterruptedException e) {
51+
e.printStackTrace();
52+
}
53+
logger.info("I'm inside the new thread - with a new span");
54+
};
55+
executor.execute(runnable);
56+
57+
logger.info("I'm done - with the original span");
58+
return "success";
59+
}
60+
61+
@GetMapping("/async")
62+
public String helloSleuthAsync() throws InterruptedException {
63+
logger.info("Before Async Method Call");
64+
sleuthService.asyncMethod();
65+
logger.info("After Async Method Call");
66+
return "success";
67+
}
68+
69+
70+
71+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.spring.session;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.cloud.sleuth.Span;
7+
import org.springframework.cloud.sleuth.Tracer;
8+
import org.springframework.scheduling.annotation.Async;
9+
import org.springframework.stereotype.Service;
10+
11+
@Service
12+
public class SleuthService {
13+
private Logger logger = LoggerFactory.getLogger(this.getClass());
14+
private final Tracer tracer;
15+
16+
@Autowired
17+
public SleuthService(Tracer tracer) {
18+
this.tracer = tracer;
19+
}
20+
21+
public void doSomeWorkSameSpan() throws InterruptedException {
22+
Thread.sleep(1000L);
23+
logger.info("Doing some work");
24+
}
25+
26+
public void doSomeWorkNewSpan() throws InterruptedException {
27+
logger.info("I'm in the original span");
28+
29+
Span newSpan = tracer.createSpan("newSpan");
30+
try {
31+
Thread.sleep(1000L);
32+
logger.info("I'm in the new span doing some cool work that needs its own span");
33+
} finally {
34+
tracer.close(newSpan);
35+
}
36+
37+
logger.info("I'm in the original span");
38+
}
39+
40+
@Async
41+
public void asyncMethod() throws InterruptedException {
42+
logger.info("Start Async Method");
43+
Thread.sleep(1000L);
44+
logger.info("End Async Method");
45+
}
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.spring.session;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SleuthWebApp {
8+
public static void main(String[] args) {
9+
SpringApplication.run(SleuthWebApp.class, args);
10+
}
11+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.spring.session;
2+
3+
import org.springframework.beans.factory.BeanFactory;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
9+
import org.springframework.scheduling.annotation.EnableAsync;
10+
import org.springframework.scheduling.annotation.EnableScheduling;
11+
import org.springframework.scheduling.annotation.SchedulingConfigurer;
12+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
13+
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
14+
15+
import java.util.concurrent.Executor;
16+
import java.util.concurrent.Executors;
17+
18+
@Configuration
19+
@EnableAsync
20+
@EnableScheduling
21+
public class ThreadConfig extends AsyncConfigurerSupport implements SchedulingConfigurer{
22+
23+
@Autowired
24+
private BeanFactory beanFactory;
25+
26+
@Bean
27+
public Executor executor() {
28+
return makeExecutor();
29+
}
30+
31+
@Override
32+
public Executor getAsyncExecutor() {
33+
return makeExecutor();
34+
}
35+
36+
@Override
37+
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
38+
scheduledTaskRegistrar.setScheduler(schedulingExecutor());
39+
}
40+
41+
@Bean(destroyMethod = "shutdown")
42+
public Executor schedulingExecutor() {
43+
return Executors.newScheduledThreadPool(100);
44+
}
45+
46+
private Executor makeExecutor() {
47+
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
48+
threadPoolTaskExecutor.setCorePoolSize(5);
49+
threadPoolTaskExecutor.setMaxPoolSize(10);
50+
threadPoolTaskExecutor.initialize();
51+
52+
return new LazyTraceExecutor(beanFactory, threadPoolTaskExecutor);
53+
}
54+
55+
56+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=Baeldung Sleuth Tutorial

0 commit comments

Comments
 (0)