Audit-Headers für den `ExampleProducer` implementiert producer/spring-producer--auditheaders producer/spring-producer--auditheaders--2024-11-13--si
authorKai Moritz <kai@juplo.de>
Fri, 1 Nov 2024 08:55:43 +0000 (09:55 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 8 Nov 2024 17:11:43 +0000 (18:11 +0100)
README.sh
docker/docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/ExampleProducer.java

index 19419b5..2ad543e 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-IMAGE=juplo/spring-producer:1.0-backpressure-SNAPSHOT
+IMAGE=juplo/spring-producer:1.0-auditheaders-SNAPSHOT
 
 if [ "$1" = "cleanup" ]
 then
@@ -10,7 +10,7 @@ then
 fi
 
 docker compose -f docker/docker-compose.yml up -d --remove-orphans kafka-1 kafka-2 kafka-3
-docker compose -f docker/docker-compose.yml rm -svf setup producer
+docker compose -f docker/docker-compose.yml rm -svf setup peter ute
 
 if [[
   $(docker image ls -q $IMAGE) == "" ||
@@ -26,18 +26,14 @@ fi
 docker compose -f docker/docker-compose.yml up --remove-orphans setup || exit 1
 
 
-docker compose -f docker/docker-compose.yml up -d producer
-sleep 5
+docker compose -f docker/docker-compose.yml up -d peter ute
+sleep 10
 
-docker compose -f docker/docker-compose.yml pause kafka-1 kafka-3
-sleep 20
-docker compose -f docker/docker-compose.yml unpause kafka-1 kafka-3
-sleep 3
-
-docker compose -f docker/docker-compose.yml stop producer
 
 echo
 echo "Empfangen:"
-docker compose -f docker/docker-compose.yml exec cli kafkacat -b kafka:9092 -t test -e -q -J
+docker compose -f docker/docker-compose.yml exec cli kafkacat -b kafka:9092 -t test -e -c 100
 
-docker compose -f docker/docker-compose.yml logs producer | grep ERROR
+echo
+echo "Empfangen (mit Headern!):"
+docker compose -f docker/docker-compose.yml exec cli kafkacat -b kafka:9092 -t test -e -c 100 -J
index 6b6ce22..085bc5f 100644 (file)
@@ -189,15 +189,27 @@ services:
       - kafka-2
       - kafka-3
 
-  producer:
-    image: juplo/spring-producer:1.0-backpressure-SNAPSHOT
+  peter:
+    image: juplo/spring-producer:1.0-auditheaders-SNAPSHOT
     environment:
       juplo.bootstrap-server: kafka:9092
-      juplo.client-id: producer
+      juplo.client-id: peter
       juplo.producer.topic: test
+      juplo.producer.linger: 300ms
+      juplo.producer.throttle: 66ms
       juplo.producer.delivery-timeout: 2147483647ms
-      juplo.producer.max-block: 2147483647ms
-      juplo.producer.max-queue-length: 10
+      juplo.producer.max-queue-length: 100
+
+  ute:
+    image: juplo/spring-producer:1.0-auditheaders-SNAPSHOT
+    environment:
+      juplo.bootstrap-server: kafka:9092
+      juplo.client-id: ute
+      juplo.producer.topic: test
+      juplo.producer.lingers: 500ms
+      juplo.producer.throttle: 30ms
+      juplo.producer.delivery-timeout: 2147483647ms
+      juplo.producer.max-queue-length: 50
 
   consumer:
     image: juplo/simple-consumer:1.0-SNAPSHOT
diff --git a/pom.xml b/pom.xml
index d2cb135..a1ef5d2 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -15,7 +15,7 @@
   <artifactId>spring-producer</artifactId>
   <name>Spring Producer</name>
   <description>A Simple Spring-Boot-Producer, that takes messages via POST and confirms successs</description>
-  <version>1.0-backpressure-SNAPSHOT</version>
+  <version>1.0-auditheaders-SNAPSHOT</version>
 
   <properties>
     <java.version>21</java.version>
index 4300eac..a3b83df 100644 (file)
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.producer.Producer;
 import org.apache.kafka.clients.producer.ProducerRecord;
 
+import java.math.BigInteger;
 import java.time.Duration;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -11,6 +12,9 @@ import java.util.concurrent.atomic.AtomicInteger;
 @Slf4j
 public class ExampleProducer implements Runnable
 {
+  public final static String HEADER_ID = "id";
+  public final static String HEADER_QUEUED = "#";
+
   private final String id;
   private final String topic;
   private final Duration throttle;
@@ -107,6 +111,11 @@ public class ExampleProducer implements Runnable
 
     int queuedAfterSend = queued.incrementAndGet();
 
+    record
+      .headers()
+      .add(HEADER_ID, id.getBytes())
+      .add(HEADER_QUEUED, BigInteger.valueOf(queuedAfterSend).toByteArray());
+
     producer.send(record, (metadata, e) ->
     {
       long now = System.currentTimeMillis();