From: Kai Moritz Date: Mon, 28 Oct 2024 08:27:04 +0000 (+0100) Subject: Producerspezifische Properties werden in eigener nested Class verwaltet X-Git-Tag: producer/spring-producer--2024-11-13--si~8 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=6f9df820e0d065d7220648ab73355ca1d70c4af6;p=demos%2Fkafka%2Ftraining Producerspezifische Properties werden in eigener nested Class verwaltet * Dadurch wird der Code übersichtlicher, wenn spätere Implementierungen _sowohl_ als Consumer, _als auch_ als Producer agieren! --- diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 5807af0..c417a7f 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -192,8 +192,8 @@ services: producer: image: juplo/spring-producer:1.0-SNAPSHOT environment: - juplo.producer.bootstrap-server: kafka:9092 - juplo.producer.client-id: producer + juplo.bootstrap-server: kafka:9092 + juplo.client-id: producer juplo.producer.topic: test consumer-1: diff --git a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java index 835e796..ff9170d 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -22,7 +22,7 @@ public class ApplicationConfiguration return new ExampleProducer( properties.getClientId(), - properties.getTopic(), + properties.getProducerProperties().getTopic(), kafkaProducer); } @@ -32,13 +32,13 @@ public class ApplicationConfiguration Properties props = new Properties(); props.put("bootstrap.servers", properties.getBootstrapServer()); props.put("client.id", properties.getClientId()); - props.put("acks", properties.getAcks()); - props.put("batch.size", properties.getBatchSize()); + props.put("acks", properties.getProducerProperties().getAcks()); + props.put("batch.size", properties.getProducerProperties().getBatchSize()); props.put("metadata.max.age.ms", 5000); // 5 Sekunden props.put("delivery.timeout.ms", 20000); // 20 Sekunden props.put("request.timeout.ms", 10000); // 10 Sekunden - props.put("linger.ms", properties.getLinger().toMillis()); - props.put("compression.type", properties.getCompressionType()); + props.put("linger.ms", properties.getProducerProperties().getLinger().toMillis()); + props.put("compression.type", properties.getProducerProperties().getCompressionType()); props.put("key.serializer", StringSerializer.class.getName()); props.put("value.serializer", StringSerializer.class.getName()); diff --git a/src/main/java/de/juplo/kafka/ApplicationProperties.java b/src/main/java/de/juplo/kafka/ApplicationProperties.java index 54114eb..16c2831 100644 --- a/src/main/java/de/juplo/kafka/ApplicationProperties.java +++ b/src/main/java/de/juplo/kafka/ApplicationProperties.java @@ -10,7 +10,7 @@ import org.springframework.validation.annotation.Validated; import java.time.Duration; -@ConfigurationProperties(prefix = "juplo.producer") +@ConfigurationProperties(prefix = "juplo") @Validated @Getter @Setter @@ -22,17 +22,34 @@ public class ApplicationProperties @NotNull @NotEmpty private String clientId; + @NotNull - @NotEmpty - private String topic; - @NotNull - @NotEmpty - private String acks; - @NotNull - private Integer batchSize; - @NotNull - private Duration linger; - @NotNull - @NotEmpty - private String compressionType; + private ProducerProperties producer; + + + public ProducerProperties getProducerProperties() + { + return producer; + } + + + @Validated + @Getter + @Setter + static class ProducerProperties + { + @NotNull + @NotEmpty + private String topic; + @NotNull + @NotEmpty + private String acks; + @NotNull + private Integer batchSize; + @NotNull + private Duration linger; + @NotNull + @NotEmpty + private String compressionType; + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f0217a6..0e28aba 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,7 @@ juplo: + bootstrap-server: :9092 + client-id: DEV producer: - bootstrap-server: :9092 - client-id: DEV topic: test acks: -1 batch-size: 16384 @@ -22,13 +22,14 @@ management: enabled: true info: kafka: - bootstrap-server: ${juplo.producer.bootstrap-server} - client-id: ${juplo.producer.client-id} - topic: ${juplo.producer.topic} - acks: ${juplo.producer.acks} - batch-size: ${juplo.producer.batch-size} - linger: ${juplo.producer.linger} - compression-type: ${juplo.producer.compression-type} + bootstrap-server: ${juplo.bootstrap-server} + client-id: ${juplo.client-id} + producer: + topic: ${juplo.producer.topic} + acks: ${juplo.producer.acks} + batch-size: ${juplo.producer.batch-size} + linger: ${juplo.producer.linger} + compression-type: ${juplo.producer.compression-type} logging: level: root: INFO diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java index 6d07e5a..fe8609e 100644 --- a/src/test/java/de/juplo/kafka/ApplicationTests.java +++ b/src/test/java/de/juplo/kafka/ApplicationTests.java @@ -29,7 +29,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. properties = { "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}", "spring.kafka.consumer.auto-offset-reset=earliest", - "juplo.producer.bootstrap-server=${spring.embedded.kafka.brokers}", + "juplo.bootstrap-server=${spring.embedded.kafka.brokers}", "juplo.producer.topic=" + TOPIC}) @AutoConfigureMockMvc @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)