+++ /dev/null
-*
-!target/*.jar
+++ /dev/null
-root = true
-
-[*]
-indent_style = space
-indent_size = tab
-tab_width = 2
-charset = utf-8
-end_of_line = lf
-trim_trailing_whitespace = true
-insert_final_newline = false
-
-[*.properties]
-charset = latin1
\ No newline at end of file
+++ /dev/null
-target/*.jar
+++ /dev/null
-FROM openjdk:11-jre
-VOLUME /tmp
-COPY target/*.jar /opt/app.jar
-ENTRYPOINT [ "java", "-jar", "/opt/app.jar" ]
-CMD []
#!/bin/bash
-IMAGE=juplo/spring-consumer-json:1.0-SNAPSHOT
-
if [ "$1" = "cleanup" ]
then
docker-compose down -v
- mvn clean
exit
fi
-docker-compose up -d kafka-0 kafka-1 kafka-2 kafka-3 cli
-
-if [[
- $(docker image ls -q $IMAGE) == "" ||
- "$1" = "build"
-]]
-then
- docker-compose rm -svf consumer-1 consumer-2
- mvn clean install || exit
-else
- echo "Using image existing images:"
- docker image ls $IMAGE
-fi
-
docker-compose up -d
docker-compose logs -f setup
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
-# tag::nachrichten[]
echo 6 | http -v :8080/peter
echo 77 | http -v :8080/klaus
-# end::nachrichten[]
echo "Writing poison pill..."
-# tag::poisonpill[]
echo 'BOOM!' | kafkacat -P -b :9092 -t test
-# end::poisonpill[]
docker-compose logs -f consumer-1 consumer-2
echo "Restarting consumer-1..."
-# tag::restart[]
docker-compose up consumer-1
-# end::restart[]
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.7.2</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
-
- <groupId>de.juplo.kafka</groupId>
- <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>
-
- <properties>
- <java.version>11</java.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-validation</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.kafka</groupId>
- <artifactId>spring-kafka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.kafka</groupId>
- <artifactId>spring-kafka-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>build-info</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>io.fabric8</groupId>
- <artifactId>docker-maven-plugin</artifactId>
- <version>0.33.0</version>
- <configuration>
- <images>
- <image>
- <name>juplo/%a:%v</name>
- </image>
- </images>
- </configuration>
- <executions>
- <execution>
- <id>build</id>
- <phase>package</phase>
- <goals>
- <goal>build</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <artifactId>maven-failsafe-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
-</project>
+++ /dev/null
-package de.juplo.kafka;
-
-import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.Consumer;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.ApplicationArguments;
-import org.springframework.boot.ApplicationRunner;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.ConfigurableApplicationContext;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
-import org.springframework.util.concurrent.ListenableFuture;
-
-import javax.annotation.PreDestroy;
-import java.util.concurrent.ExecutionException;
-
-
-@SpringBootApplication
-@Slf4j
-public class Application implements ApplicationRunner
-{
- @Autowired
- ThreadPoolTaskExecutor taskExecutor;
- @Autowired
- Consumer<?, ?> kafkaConsumer;
- @Autowired
- SimpleConsumer simpleConsumer;
- @Autowired
- ConfigurableApplicationContext context;
-
- ListenableFuture<Integer> consumerJob;
-
- @Override
- public void run(ApplicationArguments args) throws Exception
- {
- log.info("Starting SimpleConsumer");
- consumerJob = taskExecutor.submitListenable(simpleConsumer);
- consumerJob.addCallback(
- exitStatus ->
- {
- log.info("SimpleConsumer exited normally, exit-status: {}", exitStatus);
- SpringApplication.exit(context, () -> exitStatus);
- },
- t ->
- {
- log.error("SimpleConsumer exited abnormally!", t);
- SpringApplication.exit(context, () -> 2);
- });
- }
-
- @PreDestroy
- public void shutdown() throws ExecutionException, InterruptedException
- {
- log.info("Signaling SimpleConsumer to quit its work");
- kafkaConsumer.wakeup();
- log.info("Waiting for SimpleConsumer to finish its work");
- consumerJob.get();
- log.info("SimpleConsumer finished its work");
- }
-
-
- public static void main(String[] args)
- {
- SpringApplication.run(Application.class, args);
- }
-}
+++ /dev/null
-package de.juplo.kafka;
-
-import org.apache.kafka.clients.consumer.Consumer;
-import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import org.springframework.kafka.core.ConsumerFactory;
-
-
-@Configuration
-@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
-public class ApplicationConfiguration
-{
- @Bean
- public SimpleConsumer simpleConsumer(
- Consumer<String, Message> kafkaConsumer,
- KafkaProperties kafkaProperties,
- ApplicationProperties applicationProperties)
- {
- return
- new SimpleConsumer(
- kafkaProperties.getClientId(),
- applicationProperties.getTopic(),
- kafkaConsumer);
- }
-
- @Bean
- public Consumer<?, ?> kafkaConsumer(ConsumerFactory<?, ?> factory)
- {
- return factory.createConsumer();
- }
-}
+++ /dev/null
-package de.juplo.kafka;
-
-import lombok.Getter;
-import lombok.Setter;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.validation.annotation.Validated;
-
-import javax.validation.constraints.NotEmpty;
-import javax.validation.constraints.NotNull;
-
-
-@ConfigurationProperties(prefix = "simple.consumer")
-@Validated
-@Getter
-@Setter
-public class ApplicationProperties
-{
- @NotNull
- @NotEmpty
- private String topic;
-}
+++ /dev/null
-package de.juplo.kafka;
-
-
-public abstract class Message
-{
- public enum Type {ADD, CALC}
-
- public abstract Type getType();
-}
+++ /dev/null
-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;
- }
-}
+++ /dev/null
-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;
- }
-}
+++ /dev/null
-package de.juplo.kafka;
-
-import lombok.RequiredArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.kafka.clients.consumer.Consumer;
-import org.apache.kafka.clients.consumer.ConsumerRecord;
-import org.apache.kafka.clients.consumer.ConsumerRecords;
-import org.apache.kafka.common.errors.WakeupException;
-
-import java.time.Duration;
-import java.util.Arrays;
-import java.util.concurrent.Callable;
-
-
-@Slf4j
-@RequiredArgsConstructor
-public class SimpleConsumer implements Callable<Integer>
-{
- private final String id;
- private final String topic;
- private final Consumer<String, Message> consumer;
-
- private long consumed = 0;
-
-
- @Override
- public Integer call()
- {
- try
- {
- log.info("{} - Subscribing to topic {}", id, topic);
- consumer.subscribe(Arrays.asList(topic));
-
- while (true)
- {
- ConsumerRecords<String, Message> records =
- consumer.poll(Duration.ofSeconds(1));
-
- log.info("{} - Received {} messages", id, records.count());
- for (ConsumerRecord<String, Message> record : records)
- {
- handleRecord(
- record.topic(),
- record.partition(),
- record.offset(),
- record.key(),
- record.value());
- }
- }
- }
- catch(WakeupException e)
- {
- log.info("{} - Consumer was signaled to finish its work", id);
- return 0;
- }
- catch(Exception e)
- {
- log.error("{} - Unexpected error: {}, unsubscribing!", id, e.toString());
- consumer.unsubscribe();
- return 1;
- }
- finally
- {
- 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,
- Message value)
- {
- consumed++;
- log.info("{} - {}: {}/{} - {}={}", id, offset, topic, partition, key, value);
- }
-}
+++ /dev/null
-simple:
- consumer:
- topic: test
-management:
- endpoint:
- shutdown:
- enabled: true
- endpoints:
- web:
- exposure:
- include: "*"
- info:
- env:
- enabled: true
- java:
- enabled: true
-info:
- kafka:
- 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: my-group
- auto-offset-reset: earliest
- auto-commit-interval: 5s
- key-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
- de.juplo: DEBUG
-server:
- port: 8881
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8"?>
-<configuration>
-
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <Pattern>%highlight(%-5level) %m%n</Pattern>
- </encoder>
- </appender>
-
- <logger name="de.juplo" level="TRACE"/>
- <!-- logger name="org.apache.kafka.clients" level="DEBUG" / -->
-
- <root level="INFO">
- <appender-ref ref="STDOUT" />
- </root>
-
-</configuration>
+++ /dev/null
-package de.juplo.kafka;
-
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.boot.test.web.client.TestRestTemplate;
-import org.springframework.boot.test.web.server.LocalServerPort;
-import org.springframework.kafka.test.context.EmbeddedKafka;
-
-import static de.juplo.kafka.ApplicationIT.TOPIC;
-
-
-@SpringBootTest(
- webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
- properties = {
- "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
- "simple.consumer.topic=" + TOPIC })
-@EmbeddedKafka(topics = TOPIC)
-public class ApplicationIT
-{
- public static final String TOPIC = "FOO";
-
- @LocalServerPort
- private int port;
-
- @Autowired
- private TestRestTemplate restTemplate;
-
-
-
- @Test
- public void testApplicationStartup()
- {
- restTemplate.getForObject(
- "http://localhost:" + port + "/actuator/health",
- String.class
- )
- .contains("UP");
- }
-}
+++ /dev/null
-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));
- }
-}