Auf die Autoconfiguration von Spring Boot umgestellt consumer/spring-consumer--generics consumer/spring-consumer--generics--2025-04-signal-spickzettel consumer/spring-consumer--generics--2025-05-lvm consumer/spring-consumer--generics--2025-05-lvm--spickzettel consumer/spring-consumer--generics--2025-05-signal-spickzettel consumer/spring-consumer--generics--2025-07-05
authorKai Moritz <kai@juplo.de>
Sun, 13 Apr 2025 10:17:15 +0000 (12:17 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 13 Apr 2025 10:17:15 +0000 (12:17 +0200)
docker/docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/ApplicationProperties.java
src/main/resources/application.yml
src/test/java/de/juplo/kafka/ApplicationTests.java

index 5d1fa46..e5b9a9f 100644 (file)
@@ -142,22 +142,28 @@ services:
   consumer:
     image: juplo/spring-consumer:1.1-generics-SNAPSHOT
     environment:
-      juplo.bootstrap-server: kafka:9092
-      juplo.client-id: consumer
+      spring.kafka.bootstrap-servers: kafka:9092
+      spring.kafka.client-id: consumer
+      spring.kafka.consumer.auto-offset-reset: earliest
+      logging.level.org.apache.kafka.clients.consumer: INFO
       juplo.consumer.topic: test
 
   peter:
     image: juplo/spring-consumer:1.1-generics-SNAPSHOT
     environment:
-      juplo.bootstrap-server: kafka:9092
-      juplo.client-id: peter
+      spring.kafka.bootstrap-servers: kafka:9092
+      spring.kafka.client-id: consumer
+      spring.kafka.consumer.auto-offset-reset: earliest
+      logging.level.org.apache.kafka.clients.consumer: INFO
       juplo.consumer.topic: test
 
   ute:
     image: juplo/spring-consumer:1.1-generics-SNAPSHOT
     environment:
-      juplo.bootstrap-server: kafka:9092
-      juplo.client-id: ute
+      spring.kafka.bootstrap-servers: kafka:9092
+      spring.kafka.client-id: consumer
+      spring.kafka.consumer.auto-offset-reset: earliest
+      logging.level.org.apache.kafka.clients.consumer: INFO
       juplo.consumer.topic: test
 
 volumes:
diff --git a/pom.xml b/pom.xml
index 88730f1..250f40d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -40,8 +40,8 @@
       <artifactId>spring-boot-starter-validation</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.apache.kafka</groupId>
-      <artifactId>kafka-clients</artifactId>
+      <groupId>org.springframework.kafka</groupId>
+      <artifactId>spring-kafka</artifactId>
     </dependency>
     <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.springframework.kafka</groupId>
-      <artifactId>spring-kafka</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.springframework.kafka</groupId>
       <artifactId>spring-kafka-test</artifactId>
index c525b11..e523a20 100644 (file)
@@ -1,14 +1,12 @@
 package de.juplo.kafka;
 
 import org.apache.kafka.clients.consumer.Consumer;
-import org.apache.kafka.clients.consumer.KafkaConsumer;
-import org.apache.kafka.common.serialization.StringDeserializer;
+import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-
-import java.util.Properties;
+import org.springframework.kafka.core.ConsumerFactory;
 
 
 @Configuration
@@ -19,35 +17,20 @@ public class ApplicationConfiguration
   public ExampleConsumer<String, String> exampleConsumer(
     Consumer<String, String> kafkaConsumer,
     ApplicationProperties properties,
+    KafkaProperties kafkaProperties,
     ConfigurableApplicationContext applicationContext)
   {
     return
       new ExampleConsumer<>(
-        properties.getClientId(),
+        kafkaProperties.getClientId(),
         properties.getConsumerProperties().getTopic(),
         kafkaConsumer,
         () -> applicationContext.close());
   }
 
   @Bean(destroyMethod = "")
-  public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+  public Consumer<?, ?> kafkaConsumer(ConsumerFactory<?, ?> consumerFactory)
   {
-    Properties props = new Properties();
-    props.put("bootstrap.servers", properties.getBootstrapServer());
-    props.put("client.id", properties.getClientId());
-    props.put("group.id", properties.getConsumerProperties().getGroupId());
-    if (properties.getConsumerProperties().getAutoOffsetReset() != null)
-    {
-      props.put("auto.offset.reset", properties.getConsumerProperties().getAutoOffsetReset().name());
-    }
-    if (properties.getConsumerProperties().getAutoCommitInterval() != null)
-    {
-      props.put("auto.commit.interval", properties.getConsumerProperties().getAutoCommitInterval());
-    }
-    props.put("metadata.max.age.ms", 5000); //  5 Sekunden
-    props.put("key.deserializer", StringDeserializer.class.getName());
-    props.put("value.deserializer", StringDeserializer.class.getName());
-
-    return new KafkaConsumer<>(props);
+    return consumerFactory.createConsumer();
   }
 }
index c8193c9..22c755e 100644 (file)
@@ -7,8 +7,6 @@ import lombok.Setter;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.validation.annotation.Validated;
 
-import java.time.Duration;
-
 
 @ConfigurationProperties(prefix = "juplo")
 @Validated
@@ -16,13 +14,6 @@ import java.time.Duration;
 @Setter
 public class ApplicationProperties
 {
-  @NotNull
-  @NotEmpty
-  private String bootstrapServer;
-  @NotNull
-  @NotEmpty
-  private String clientId;
-
   @NotNull
   private ConsumerProperties consumer;
 
@@ -38,15 +29,8 @@ public class ApplicationProperties
   @Setter
   static class ConsumerProperties
   {
-    @NotNull
-    @NotEmpty
-    private String groupId;
     @NotNull
     @NotEmpty
     private String topic;
-    private OffsetReset autoOffsetReset;
-    private Duration autoCommitInterval;
-
-    enum OffsetReset { latest, earliest, none }
   }
 }
index 7a06731..71dddda 100644 (file)
@@ -1,11 +1,6 @@
 juplo:
-  bootstrap-server: :9092
-  client-id: DEV
   consumer:
-    group-id: my-group
     topic: test
-    auto-offset-reset: earliest
-    auto-commit-interval: 5s
 management:
   endpoint:
     shutdown:
@@ -21,13 +16,17 @@ management:
       enabled: true
 info:
   kafka:
-    bootstrap-server: ${juplo.bootstrap-server}
-    client-id: ${juplo.client-id}
+    bootstrap-server: ${spring.kafka.bootstrap-servers}
+    client-id: ${spring.kafka.client-id}
+    group-id: ${spring.kafka.consumer.group-id}
+    topic: ${simple.consumer.topic}
+    auto-offset-reset: ${spring.kafka.consumer.auto-offset-reset}
+spring:
+  kafka:
+    bootstrap-servers: :9092
+    client-id: DEV
     consumer:
-      group-id: ${juplo.consumer.group-id}
-      topic: ${juplo.consumer.topic}
-      auto-offset-reset: ${juplo.consumer.auto-offset-reset}
-      auto-commit-interval: ${juplo.consumer.auto-commit-interval}
+      group-id: my-group
 logging:
   level:
     root: INFO
index ae119bf..fae9ae3 100644 (file)
@@ -17,10 +17,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 
-@SpringBootTest(
-  properties = {
-    "juplo.bootstrap-server=${spring.embedded.kafka.brokers}",
-    "juplo.consumer.topic=" + TOPIC })
+@SpringBootTest(properties = { "juplo.consumer.topic=" + TOPIC })
 @AutoConfigureMockMvc
 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
 public class ApplicationTests