From: Kai Moritz Date: Tue, 29 Oct 2024 13:05:40 +0000 (+0100) Subject: Setup, um `grundlagen/simple-consumer` als Consumer Group zu skalieren X-Git-Tag: grundlagen/simple-consumer--consumergroup--vorlage--2024-11-13--si X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fgrundlagen%2Fsimple-consumer--consumergroup--vorlage;p=demos%2Fkafka%2Ftraining Setup, um `grundlagen/simple-consumer` als Consumer Group zu skalieren --- diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 49f82a9..0000000 --- a/.dockerignore +++ /dev/null @@ -1,3 +0,0 @@ -* -!target/*.jar -!target/libs/*.jar diff --git a/.maven-dockerexclude b/.maven-dockerexclude deleted file mode 100644 index 72e8ffc..0000000 --- a/.maven-dockerexclude +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/.maven-dockerinclude b/.maven-dockerinclude deleted file mode 100644 index a00c65f..0000000 --- a/.maven-dockerinclude +++ /dev/null @@ -1,2 +0,0 @@ -target/*.jar -target/libs/*.jar diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 22819af..0000000 --- a/Dockerfile +++ /dev/null @@ -1,6 +0,0 @@ -FROM eclipse-temurin:21-jre -VOLUME /tmp -COPY target/*.jar /opt/app.jar -COPY target/libs /opt/libs -ENTRYPOINT [ "java", "-jar", "/opt/app.jar" ] -CMD [ "kafka:9092", "test", "my-group", "DCKR" ] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index cba608b..3b5752c 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -193,9 +193,17 @@ services: image: juplo/simple-producer:1.0-SNAPSHOT command: kafka:9092 test producer - consumer: + consumer-1: image: juplo/simple-consumer:1.0-SNAPSHOT - command: kafka:9092 test my-group consumer + command: kafka:9092 test my-group consumer-1 + + consumer-2: + image: juplo/simple-consumer:1.0-SNAPSHOT + command: kafka:9092 test my-group consumer-2 + + consumer-3: + image: juplo/simple-consumer:1.0-SNAPSHOT + command: kafka:9092 test my-group consumer-3 volumes: zookeeper-data: diff --git a/pom.xml b/pom.xml deleted file mode 100644 index 2d81d24..0000000 --- a/pom.xml +++ /dev/null @@ -1,98 +0,0 @@ - - - - 4.0.0 - - - org.springframework.boot - spring-boot-starter-parent - 3.3.4 - - - - de.juplo.kafka - simple-consumer - Simple Consumer-Group - Super Simple Consumer-Group, that is implemented as a plain Java-program - 1.0-SNAPSHOT - - - 21 - - - - - org.apache.kafka - kafka-clients - - - org.projectlombok - lombok - - - ch.qos.logback - logback-classic - - - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - true - libs/ - de.juplo.kafka.ExampleConsumer - - - - - - pl.project13.maven - git-commit-id-plugin - - - io.fabric8 - docker-maven-plugin - 0.45.0 - - - - juplo/%a:%v - - - - - - build - package - - build - - - - - - - - diff --git a/src/main/java/de/juplo/kafka/ExampleConsumer.java b/src/main/java/de/juplo/kafka/ExampleConsumer.java deleted file mode 100644 index 3634875..0000000 --- a/src/main/java/de/juplo/kafka/ExampleConsumer.java +++ /dev/null @@ -1,132 +0,0 @@ -package de.juplo.kafka; - -import lombok.extern.slf4j.Slf4j; -import org.apache.kafka.clients.consumer.ConsumerRecord; -import org.apache.kafka.clients.consumer.ConsumerRecords; -import org.apache.kafka.clients.consumer.Consumer; -import org.apache.kafka.clients.consumer.KafkaConsumer; -import org.apache.kafka.common.errors.WakeupException; -import org.apache.kafka.common.serialization.StringDeserializer; - -import java.time.Duration; -import java.util.Arrays; -import java.util.Properties; - - -@Slf4j -public class ExampleConsumer -{ - private final String id; - private final String topic; - private final Consumer consumer; - - private volatile boolean running = false; - private long consumed = 0; - - public ExampleConsumer( - String broker, - String topic, - String groupId, - String clientId) - { - Properties props = new Properties(); - // Konfiguration für den Consumer zusammenstellen - - this.id = clientId; - this.topic = topic; - consumer = new KafkaConsumer<>(props); - } - - - public void run() - { - try - { - log.info("{} - Subscribing to topic {}", id, topic); - // TODO: subscribe! - running = true; - - while (true) - { - // TODO: - // Über consumer.poll() Nachrichten abrufen und für die - // empfangenen Nachrichten die Methode handleRecord - // aufrufen. - } - } - catch(WakeupException e) - { - log.info("{} - Consumer was signaled to finish its work", id); - } - catch(Exception e) - { - log.error("{} - Unexpected error, unsubscribing!", id, e); - } - finally - { - running = false; - log.info("{} - Closing the KafkaConsumer", id); - consumer.close(); - log.info("{}: Consumed {} messages in total, exiting!", id, consumed); - } - } - - private void handleRecord( - String topic, - Integer partition, - Long offset, - String key, - String value) - { - consumed++; - log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value); - } - - - public static void main(String[] args) throws Exception - { - String broker = ":9092"; - String topic = "test"; - String groupId = "my-group"; - String clientId = "DEV"; - - switch (args.length) - { - case 4: - clientId = args[3]; - case 3: - groupId = args[2]; - case 2: - topic = args[1]; - case 1: - broker = args[0]; - } - - - ExampleConsumer instance = new ExampleConsumer(broker, topic, groupId, clientId); - - Runtime.getRuntime().addShutdownHook(new Thread(() -> - { - instance.consumer.wakeup(); - - while (instance.running) - { - log.info("Waiting for main-thread..."); - try - { - Thread.sleep(1000); - } - catch (InterruptedException e) {} - } - log.info("Shutdown completed."); - })); - - log.info( - "Running ExampleConsumer: broker={}, topic={}, group-id={}, client-id={}", - broker, - topic, - groupId, - clientId); - instance.run(); - } -} diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml deleted file mode 100644 index 7a25e76..0000000 --- a/src/main/resources/logback.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - %d{HH:mm:ss.SSS} | %highlight(%-5level) %msg%n - - - - - - - - - -