Spring-Kafka Version des Simple-Consumer auf JSON-Nachrichten umgestellt
authorKai Moritz <kai@juplo.de>
Mon, 7 Nov 2022 20:44:40 +0000 (21:44 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 19 Nov 2022 17:52:13 +0000 (18:52 +0100)
README.sh
docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/Message.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/MessageAddNumber.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/MessageCalculateSum.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/SimpleConsumer.java
src/main/resources/application.yml
src/test/java/de/juplo/kafka/MessageTest.java [new file with mode: 0644]

index 53d1aa7..feaa66a 100755 (executable)
--- a/README.sh
+++ b/README.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-IMAGE=juplo/spring-consumer:1.0-SNAPSHOT
+IMAGE=juplo/spring-consumer-json:1.0-SNAPSHOT
 
 if [ "$1" = "cleanup" ]
 then
@@ -18,6 +18,7 @@ if [[
   "$1" = "build"
 ]]
 then
+  docker-compose rm -svf consumer-1 consumer-2
   mvn clean install || exit
 else
   echo "Using image existing images:"
@@ -25,12 +26,18 @@ else
 fi
 
 docker-compose up setup
-docker-compose up -d producer
-
-mvn spring-boot:run &
-sleep 10
-kill $(jobs -p)
-mvn spring-boot:run &
-sleep 10
-docker-compose stop producer
-kill $(jobs -p)
+docker-compose up -d producer consumer-1 consumer-2
+
+while ! [[ $(http 0:8080/actuator/health 2> /dev/null) =~ "UP" ]]; do echo "Waiting for producer..."; sleep 1; done
+while ! [[ $(http 0:8081/actuator/health 2> /dev/null) =~ "UP" ]]; do echo "Waiting for consumer-1..."; sleep 1; done
+while ! [[ $(http 0:8082/actuator/health 2> /dev/null) =~ "UP" ]]; do echo "Waiting for consumer-2..."; sleep 1; done
+
+echo 6  | http -v :8080/peter
+echo 77 | http -v :8080/klaus
+echo 66 | http -v :8080/peter
+echo 7  | http -v :8080/klaus
+
+docker-compose stop consumer-1 consumer-2
+
+docker-compose logs consumer-1
+docker-compose logs consumer-2
index bd15793..f6dbef4 100644 (file)
@@ -103,5 +103,33 @@ services:
     command: sleep infinity
 
   producer:
-    image: juplo/simple-producer:1.0-SNAPSHOT
-    command: producer
+    image: juplo/rest-producer-json:1.0-SNAPSHOT
+    ports:
+      - 8080:8080
+    environment:
+      server.port: 8080
+      producer.bootstrap-server: kafka:9092
+      producer.client-id: producer
+      producer.topic: test
+
+  consumer-1:
+    image: juplo/spring-consumer-json:1.0-SNAPSHOT
+    ports:
+      - 8081:8080
+    environment:
+      server.port: 8080
+      spring.kafka.bootstrap-servers: kafka:9092
+      spring.kafka.client-id: consumer-1
+      spring.kafka.consumer.group-id: my-group
+      simple.consumer.topic: test
+
+  consumer-2:
+    image: juplo/spring-consumer-json:1.0-SNAPSHOT
+    ports:
+      - 8082:8080
+    environment:
+      server.port: 8080
+      spring.kafka.bootstrap-servers: kafka:9092
+      spring.kafka.client-id: consumer-2
+      spring.kafka.consumer.group-id: my-group
+      simple.consumer.topic: test
diff --git a/pom.xml b/pom.xml
index cdfb199..ebf6eb1 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -12,7 +12,7 @@
   </parent>
 
   <groupId>de.juplo.kafka</groupId>
-  <artifactId>spring-consumer</artifactId>
+  <artifactId>spring-consumer-json</artifactId>
   <version>1.0-SNAPSHOT</version>
   <name>Spring Consumer</name>
   <description>Super Simple Consumer-Group, that is implemented as Spring-Boot application and configured by Spring Kafka</description>
index 62d61a2..a8b3e1d 100644 (file)
@@ -15,7 +15,7 @@ public class ApplicationConfiguration
 {
   @Bean
   public SimpleConsumer simpleConsumer(
-      Consumer<String, String> kafkaConsumer,
+      Consumer<String, Message> kafkaConsumer,
       KafkaProperties kafkaProperties,
       ApplicationProperties applicationProperties)
   {
diff --git a/src/main/java/de/juplo/kafka/Message.java b/src/main/java/de/juplo/kafka/Message.java
new file mode 100644 (file)
index 0000000..e4999b7
--- /dev/null
@@ -0,0 +1,9 @@
+package de.juplo.kafka;
+
+
+public abstract class Message
+{
+  public enum Type {ADD, CALC}
+
+  public abstract Type getType();
+}
diff --git a/src/main/java/de/juplo/kafka/MessageAddNumber.java b/src/main/java/de/juplo/kafka/MessageAddNumber.java
new file mode 100644 (file)
index 0000000..c024b65
--- /dev/null
@@ -0,0 +1,19 @@
+package de.juplo.kafka;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import lombok.Data;
+
+
+@Data
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class MessageAddNumber extends Message
+{
+  private Integer next;
+
+
+  @Override
+  public Type getType()
+  {
+    return Type.ADD;
+  }
+}
diff --git a/src/main/java/de/juplo/kafka/MessageCalculateSum.java b/src/main/java/de/juplo/kafka/MessageCalculateSum.java
new file mode 100644 (file)
index 0000000..afc5a39
--- /dev/null
@@ -0,0 +1,16 @@
+package de.juplo.kafka;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import lombok.Data;
+
+
+@Data
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class MessageCalculateSum extends Message
+{
+  @Override
+  public Type getType()
+  {
+    return Type.CALC;
+  }
+}
index 1cf9b22..3b0202f 100644 (file)
@@ -17,7 +17,7 @@ public class SimpleConsumer implements Runnable
 {
   private final String id;
   private final String topic;
-  private final Consumer<String, String> consumer;
+  private final Consumer<String, Message> consumer;
 
   private long consumed = 0;
 
@@ -32,11 +32,11 @@ public class SimpleConsumer implements Runnable
 
       while (true)
       {
-        ConsumerRecords<String, String> records =
+        ConsumerRecords<String, Message> records =
             consumer.poll(Duration.ofSeconds(1));
 
         log.info("{} - Received {} messages", id, records.count());
-        for (ConsumerRecord<String, String> record : records)
+        for (ConsumerRecord<String, Message> record : records)
         {
           handleRecord(
             record.topic(),
@@ -69,7 +69,7 @@ public class SimpleConsumer implements Runnable
     Integer partition,
     Long offset,
     String key,
-    String value)
+    Message value)
   {
     consumed++;
     log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value);
index d524e5f..07d0625 100644 (file)
@@ -30,10 +30,13 @@ spring:
       auto-offset-reset: earliest
       auto-commit-interval: 5s
       key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
-      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
+      value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
       properties:
         partition.assignment.strategy: org.apache.kafka.clients.consumer.StickyAssignor
         metadata.max.age.ms: 1000
+        spring.json.type.mapping: >
+          ADD:de.juplo.kafka.MessageAddNumber,
+          CALC:de.juplo.kafka.MessageCalculateSum
 logging:
   level:
     root: INFO
diff --git a/src/test/java/de/juplo/kafka/MessageTest.java b/src/test/java/de/juplo/kafka/MessageTest.java
new file mode 100644 (file)
index 0000000..82116f4
--- /dev/null
@@ -0,0 +1,29 @@
+package de.juplo.kafka;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+
+public class MessageTest
+{
+  ObjectMapper mapper = new ObjectMapper();
+
+  @Test
+  @DisplayName("Deserialize a MessageAddNumber message")
+  public void testDeserializeMessageAddNumber()
+  {
+    Assertions.assertDoesNotThrow(() -> mapper.readValue("{\"next\":42}", MessageAddNumber.class));
+    Assertions.assertDoesNotThrow(() -> mapper.readValue("{\"number\":666,\"next\":42}", MessageAddNumber.class));
+  }
+
+  @Test
+  @DisplayName("Deserialize a MessageCalculateSum message")
+  public void testDeserializeMessageCalculateSum() throws JsonProcessingException
+  {
+    Assertions.assertDoesNotThrow(() -> mapper.readValue("{}", MessageCalculateSum.class));
+    Assertions.assertDoesNotThrow(() -> mapper.readValue("{\"number\":666}", MessageCalculateSum.class));
+  }
+}