From: Kai Moritz Date: Fri, 22 Apr 2022 09:24:55 +0000 (+0200) Subject: Springify: Der Kafka-`Consumer` wird über die Spring-Factory erzeugt X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d401bf6750d33cfea6ab1bb01aefe9a5d6098e0c;p=demos%2Fkafka%2Ftraining Springify: Der Kafka-`Consumer` wird über die Spring-Factory erzeugt * Per `git cherry-pick` aus `springified-consumer--config' übernommen. * Conflicts: ** src/main/java/de/juplo/kafka/ApplicationConfiguration.java ** src/test/java/de/juplo/kafka/ApplicationTests.java * Damit Spring Kafka den Consumer instanziieren kann, musste insbesondere noch der Teil der Konfiguration, der fix ist, aus der Konfig-Klasse `ApplicationConfiguration` in die YAML-Datei `application.yml` verschoben werden: ** Die Deaktivierung der Automatischen Commits ** Die Konfiguration der Deserialisierer --- diff --git a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java index 92c7abe..05b5697 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -1,17 +1,15 @@ package de.juplo.kafka; import org.apache.kafka.clients.consumer.Consumer; -import org.apache.kafka.clients.consumer.KafkaConsumer; -import org.apache.kafka.common.serialization.StringDeserializer; 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.support.serializer.JsonDeserializer; import java.time.Clock; import java.util.Optional; -import java.util.Properties; +import org.springframework.kafka.core.ConsumerFactory; + import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -60,7 +58,7 @@ public class ApplicationConfiguration @Bean public EndlessConsumer endlessConsumer( - KafkaConsumer kafkaConsumer, + Consumer kafkaConsumer, ExecutorService executor, ApplicationRebalanceListener rebalanceListener, ApplicationRecordHandler recordHandler, @@ -84,24 +82,8 @@ public class ApplicationConfiguration } @Bean(destroyMethod = "close") - public KafkaConsumer kafkaConsumer(KafkaProperties kafkaProperties) + public Consumer kafkaConsumer(ConsumerFactory factory) { - Properties props = new Properties(); - - props.put("bootstrap.servers", kafkaProperties.getBootstrapServers()); - props.put("partition.assignment.strategy", "org.apache.kafka.clients.consumer.CooperativeStickyAssignor"); - props.put("group.id", kafkaProperties.getConsumer().getGroupId()); - props.put("client.id", kafkaProperties.getClientId()); - props.put("enable.auto.commit", false); - props.put("auto.offset.reset", kafkaProperties.getConsumer().getAutoOffsetReset()); - props.put("metadata.max.age.ms", kafkaProperties.getConsumer().getProperties().get("metadata.max.age.ms")); - props.put("key.deserializer", StringDeserializer.class.getName()); - props.put("value.deserializer", JsonDeserializer.class.getName()); - props.put(JsonDeserializer.TRUSTED_PACKAGES, "de.juplo.kafka"); - props.put(JsonDeserializer.TYPE_MAPPINGS, - Message.Type.ADD + ":" + MessageAddNumber.class.getName() + "," + - Message.Type.CALC + ":" + MessageCalculateSum.class.getName()); - - return new KafkaConsumer<>(props); + return factory.createConsumer(); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a899340..805e72b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -32,9 +32,15 @@ spring: consumer: group-id: my-group auto-offset-reset: earliest + enable-auto-commit: false auto-commit-interval: 5s + key-deserializer: org.apache.kafka.common.serialization.StringDeserializer + value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer properties: 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/GenericApplicationTests.java b/src/test/java/de/juplo/kafka/GenericApplicationTests.java index 0e05dbe..403e16f 100644 --- a/src/test/java/de/juplo/kafka/GenericApplicationTests.java +++ b/src/test/java/de/juplo/kafka/GenericApplicationTests.java @@ -13,6 +13,7 @@ import org.apache.kafka.common.utils.Bytes; import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration; import org.springframework.boot.autoconfigure.kafka.KafkaProperties; import org.springframework.boot.autoconfigure.mongo.MongoProperties; import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo; @@ -37,7 +38,11 @@ import static org.assertj.core.api.Assertions.*; import static org.awaitility.Awaitility.*; -@SpringJUnitConfig(initializers = ConfigDataApplicationContextInitializer.class) +@SpringJUnitConfig( + initializers = ConfigDataApplicationContextInitializer.class, + classes = { + KafkaAutoConfiguration.class, + ApplicationTests.Configuration.class }) @TestPropertySource( properties = { "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}", @@ -55,13 +60,13 @@ abstract class GenericApplicationTests @Autowired - KafkaConsumer kafkaConsumer; + org.apache.kafka.clients.consumer.Consumer kafkaConsumer; @Autowired Consumer> consumer; @Autowired ApplicationProperties applicationProperties; - @Autowired - KafkaProperties kafkaProperties; + @Autowired + KafkaProperties kafkaProperties; @Autowired ExecutorService executor; @Autowired