Skip to content

Commit f44c735

Browse files
adinauerclaude
andcommitted
feat(samples): Add Kafka producer and consumer to Spring Boot 3 sample app
Add spring-kafka dependency and a simple Kafka producer/consumer setup behind a 'kafka' Spring profile. Includes a REST endpoint to produce messages and a KafkaListener that consumes them. Kafka auto-configuration is excluded by default and only activated when the 'kafka' profile is enabled. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5f063c1 commit f44c735

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ springboot3-starter-security = { module = "org.springframework.boot:spring-boot-
183183
springboot3-starter-jdbc = { module = "org.springframework.boot:spring-boot-starter-jdbc", version.ref = "springboot3" }
184184
springboot3-starter-actuator = { module = "org.springframework.boot:spring-boot-starter-actuator", version.ref = "springboot3" }
185185
springboot3-starter-cache = { module = "org.springframework.boot:spring-boot-starter-cache", version.ref = "springboot3" }
186+
spring-kafka3 = { module = "org.springframework.kafka:spring-kafka", version = "3.3.5" }
186187
springboot4-otel = { module = "io.opentelemetry.instrumentation:opentelemetry-spring-boot-starter", version.ref = "otelInstrumentation" }
187188
springboot4-resttestclient = { module = "org.springframework.boot:spring-boot-resttestclient", version.ref = "springboot4" }
188189
springboot4-starter = { module = "org.springframework.boot:spring-boot-starter", version.ref = "springboot4" }

sentry-samples/sentry-samples-spring-boot-jakarta/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ dependencies {
5959
implementation(libs.springboot3.starter.cache)
6060
implementation(libs.caffeine)
6161

62+
// kafka
63+
implementation(libs.spring.kafka3)
64+
6265
// OpenFeature SDK
6366
implementation(libs.openfeature)
6467

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.sentry.samples.spring.boot.jakarta;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.context.annotation.Profile;
6+
import org.springframework.kafka.annotation.KafkaListener;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
@Profile("kafka")
11+
public class KafkaConsumer {
12+
13+
private static final Logger logger = LoggerFactory.getLogger(KafkaConsumer.class);
14+
15+
@KafkaListener(topics = "sentry-topic", groupId = "sentry-sample-group")
16+
public void listen(String message) {
17+
logger.info("Received message: {}", message);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.sentry.samples.spring.boot.jakarta;
2+
3+
import org.springframework.context.annotation.Profile;
4+
import org.springframework.kafka.core.KafkaTemplate;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@Profile("kafka")
12+
@RequestMapping("/kafka")
13+
public class KafkaController {
14+
15+
private final KafkaTemplate<String, String> kafkaTemplate;
16+
17+
public KafkaController(KafkaTemplate<String, String> kafkaTemplate) {
18+
this.kafkaTemplate = kafkaTemplate;
19+
}
20+
21+
@GetMapping("/produce")
22+
String produce(@RequestParam(defaultValue = "hello from sentry!") String message) {
23+
kafkaTemplate.send("sentry-topic", message);
24+
return "Message sent: " + message;
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Kafka — activate with: --spring.profiles.active=kafka
2+
spring.autoconfigure.exclude=
3+
spring.kafka.bootstrap-servers=localhost:9092
4+
spring.kafka.consumer.group-id=sentry-sample-group
5+
spring.kafka.consumer.auto-offset-reset=earliest
6+
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
7+
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
8+
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
9+
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

sentry-samples/sentry-samples-spring-boot-jakarta/src/main/resources/application.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ spring.quartz.job-store-type=memory
3737

3838
# Cache tracing
3939
sentry.enable-cache-tracing=true
40+
41+
# Kafka is only active with the 'kafka' profile (--spring.profiles.active=kafka)
42+
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
43+
4044
spring.cache.cache-names=todos
4145
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s
4246

0 commit comments

Comments
 (0)