]> juplo.de Git - demos/kafka/training/commitdiff
Umbau auf Autoconfig von Spring Kafka spring/spring-producer--2026-03--vor-branchumbenennung--springframework spring/spring-producer--2026-03-lvm springkafka/spring-producer--2026-03-20 springkafka/spring-producer--2026-03-20--19-06 springkafka/spring-producer--2026-03-21--smartlifecycle-only
authorKai Moritz <kai@juplo.de>
Tue, 18 Mar 2025 15:48:45 +0000 (16:48 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Mar 2026 19:29:45 +0000 (20:29 +0100)
README.sh
build.gradle
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 a6dff1ede2faa81235af0e8b9868c949399b9352..1d208d534a05f579cb7a49e78aecf53c9699b79c 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-IMAGE=juplo/spring-producer:1.0-SNAPSHOT
+IMAGE=juplo/spring-producer:2.0-SNAPSHOT
 
 if [ "$1" = "cleanup" ]
 then
index 081a7eaf87e8e793a77f9ef08e798bcf342a0ab4..60118b631b1368e50753f94eee843fe1ef877590 100644 (file)
@@ -8,7 +8,7 @@ plugins {
 }
 
 group = 'de.juplo.kafka'
-version = '1.0-SNAPSHOT'
+version = '2.0-SNAPSHOT'
 
 java {
        toolchain {
@@ -27,7 +27,7 @@ repositories {
 }
 
 dependencies {
-       implementation 'org.apache.kafka:kafka-clients'
+       implementation 'org.springframework.boot:spring-boot-starter-kafka'
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
        implementation 'org.springframework.boot:spring-boot-starter-validation'
        implementation 'org.springframework.boot:spring-boot-starter-web'
@@ -37,7 +37,6 @@ dependencies {
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test'
-       testImplementation 'org.springframework.boot:spring-boot-starter-kafka'
        testImplementation 'org.springframework.kafka:spring-kafka-test'
        testCompileOnly 'org.projectlombok:lombok'
        testAnnotationProcessor 'org.projectlombok:lombok'
index a5a6b64f60a998f4e492a57f81ff8cfc9186a881..d151dab5dd111e95b65cfc4d4c92b380e45ea14b 100644 (file)
@@ -173,14 +173,11 @@ services:
       - kafka-3
 
   producer:
-    image: juplo/spring-producer:1.0-SNAPSHOT
+    image: juplo/spring-producer:2.0-SNAPSHOT
     environment:
-      juplo.bootstrap-server: kafka:9092
-      juplo.client-id: producer
+      spring.kafka.bootstrap-servers: kafka:9092
+      spring.kafka.client-id: producer
       juplo.producer.topic: test
-    cpu_period: 100000
-    cpu_quota:  50000
-    mem_limit:  100m
 
   consumer:
     image: juplo/simple-consumer:1.0-SNAPSHOT
diff --git a/pom.xml b/pom.xml
index 29a79b6aeb312b61bd3696ca428ffd760e6d086a..9e11d6248a2613e406198e93cad1ef638b02929d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -15,7 +15,7 @@
   <artifactId>spring-producer</artifactId>
   <name>Spring Producer</name>
   <description>A Simple Producer, based on Spring Boot, that sends messages via Kafka</description>
-  <version>1.0-SNAPSHOT</version>
+  <version>2.0-SNAPSHOT</version>
 
   <properties>
     <java.version>21</java.version>
@@ -40,8 +40,8 @@
       <artifactId>spring-boot-starter-validation</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.apache.kafka</groupId>
-      <artifactId>kafka-clients</artifactId>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-kafka</artifactId>
     </dependency>
     <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>spring-boot-starter-webmvc-test</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.springframework.boot</groupId>
-      <artifactId>spring-boot-starter-kafka</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.springframework.kafka</groupId>
       <artifactId>spring-kafka-test</artifactId>
index 0090ceeab45f9b9e4a262dada2ef52a37744ba7a..efdfafa179d6e0c27c16cdeb15ea8d0e7666f506 100644 (file)
@@ -1,15 +1,14 @@
 package de.juplo.kafka;
 
-import org.apache.kafka.clients.producer.KafkaProducer;
 import org.apache.kafka.clients.producer.Producer;
-import org.apache.kafka.common.serialization.StringSerializer;
+import org.springframework.beans.factory.annotation.Value;
 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 org.springframework.kafka.core.ProducerFactory;
 
 import java.time.Duration;
-import java.util.Properties;
 
 
 @Configuration
@@ -18,13 +17,14 @@ public class ApplicationConfiguration
 {
   @Bean
   public ExampleProducer exampleProducer(
+    @Value("${spring.kafka.client-id}") String clientId,
     ApplicationProperties properties,
     Producer<String, String> kafkaProducer,
     ConfigurableApplicationContext applicationContext)
   {
     return
       new ExampleProducer(
-        properties.getClientId(),
+        clientId,
         properties.getProducerProperties().getTopic(),
         properties.getProducerProperties().getThrottle() == null
           ? Duration.ofMillis(500)
@@ -34,23 +34,8 @@ public class ApplicationConfiguration
   }
 
   @Bean(destroyMethod = "")
-  public KafkaProducer<String, String> kafkaProducer(ApplicationProperties properties)
+  public Producer<?, ?> kafkaProducer(ProducerFactory<?, ?> producerFactory)
   {
-    Properties props = new Properties();
-    props.put("bootstrap.servers", properties.getBootstrapServer());
-    props.put("client.id", properties.getClientId());
-    props.put("acks", properties.getProducerProperties().getAcks());
-    props.put("delivery.timeout.ms", (int)properties.getProducerProperties().getDeliveryTimeout().toMillis());
-    props.put("max.block.ms", (int)properties.getProducerProperties().getMaxBlock().toMillis());
-    props.put("buffer.memory", properties.getProducerProperties().getBufferMemory());
-    props.put("batch.size", properties.getProducerProperties().getBatchSize());
-    props.put("metadata.max.age.ms",  5000); //  5 Sekunden
-    props.put("request.timeout.ms",   5000); //  5 Sekunden
-    props.put("linger.ms", properties.getProducerProperties().getLinger().toMillis());
-    props.put("compression.type", properties.getProducerProperties().getCompressionType());
-    props.put("key.serializer", StringSerializer.class.getName());
-    props.put("value.serializer", StringSerializer.class.getName());
-
-    return new KafkaProducer<>(props);
+    return producerFactory.createProducer();
   }
 }
index 43232628755882f32117ac1a72c5ade16dd13f1e..908072cbd12666b2eb019945dc24c91e787a8f4a 100644 (file)
@@ -16,13 +16,6 @@ import java.time.Duration;
 @Setter
 public class ApplicationProperties
 {
-  @NotNull
-  @NotEmpty
-  private String bootstrapServer;
-  @NotNull
-  @NotEmpty
-  private String clientId;
-
   @NotNull
   private ProducerProperties producer;
 
@@ -41,22 +34,6 @@ public class ApplicationProperties
     @NotNull
     @NotEmpty
     private String topic;
-    @NotNull
-    @NotEmpty
-    private String acks;
-    @NotNull
-    private Duration deliveryTimeout;
-    @NotNull
-    private Duration maxBlock;
-    @NotNull
-    private Long bufferMemory;
-    @NotNull
-    private Integer batchSize;
-    @NotNull
-    private Duration linger;
-    @NotNull
-    @NotEmpty
-    private String compressionType;
     private Duration throttle;
   }
 }
index 21fef284f03e091e3a9e7f87758e5fef9fb2fe42..3f6c233a1751059d20f011c9b365a87000206669 100644 (file)
@@ -1,16 +1,22 @@
 juplo:
-  bootstrap-server: :9092
-  client-id: DEV
   producer:
     topic: test
-    acks: -1
-    delivery-timeout: 10s
-    max-block: 5s
-    buffer-memory: 33554432
-    batch-size: 16384
-    linger: 0
-    compression-type: gzip
     throttle: 500ms
+spring:
+  kafka:
+    bootstrap-servers: :9092
+    client-id: DEV
+    producer:
+      acks: -1
+      buffer-memory: 33554432
+      batch-size: 16384
+      compression-type: gzip
+      properties:
+        metadata.max.age.ms: 5000
+        request.timeout.ms: 5000
+        delivery.timeout.ms: 10000
+        max.block.ms: 5000
+        linger.ms: 0
 management:
   endpoint:
     shutdown:
@@ -26,17 +32,8 @@ management:
       enabled: true
 info:
   kafka:
-    bootstrap-server: ${juplo.bootstrap-server}
-    client-id: ${juplo.client-id}
     producer:
       topic: ${juplo.producer.topic}
-      acks: ${juplo.producer.acks}
-      delivery-timeout: ${juplo.producer.delivery-timeout}
-      max-block: ${juplo.producer.max-block}
-      buffer-memory: ${juplo.producer.buffer-memory}
-      batch-size: ${juplo.producer.batch-size}
-      linger: ${juplo.producer.linger}
-      compression-type: ${juplo.producer.compression-type}
       throttle: ${juplo.producer.throttle}
 logging:
   level:
index 68d2a8add5d6213ed8162f78d29513789db93379..b313e9c9337f8ee5d1beab191e5fbaf2999c885c 100644 (file)
@@ -29,7 +29,6 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
   properties = {
     "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
     "spring.kafka.consumer.auto-offset-reset=earliest",
-    "juplo.bootstrap-server=${spring.embedded.kafka.brokers}",
     "juplo.producer.topic=" + TOPIC})
 @AutoConfigureMockMvc
 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)