consumer:
image: juplo/spring-consumer:1.1-generics-SNAPSHOT
environment:
- juplo.bootstrap-server: kafka:9092
- juplo.client-id: consumer
+ spring.kafka.bootstrap-servers: kafka:9092
+ spring.kafka.client-id: consumer
+ spring.kafka.consumer.auto-offset-reset: earliest
+ logging.level.org.apache.kafka.clients.consumer: INFO
juplo.consumer.topic: test
peter:
image: juplo/spring-consumer:1.1-generics-SNAPSHOT
environment:
- juplo.bootstrap-server: kafka:9092
- juplo.client-id: peter
+ spring.kafka.bootstrap-servers: kafka:9092
+ spring.kafka.client-id: consumer
+ spring.kafka.consumer.auto-offset-reset: earliest
+ logging.level.org.apache.kafka.clients.consumer: INFO
juplo.consumer.topic: test
ute:
image: juplo/spring-consumer:1.1-generics-SNAPSHOT
environment:
- juplo.bootstrap-server: kafka:9092
- juplo.client-id: ute
+ spring.kafka.bootstrap-servers: kafka:9092
+ spring.kafka.client-id: consumer
+ spring.kafka.consumer.auto-offset-reset: earliest
+ logging.level.org.apache.kafka.clients.consumer: INFO
juplo.consumer.topic: test
volumes:
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
- <groupId>org.apache.kafka</groupId>
- <artifactId>kafka-clients</artifactId>
+ <groupId>org.springframework.kafka</groupId>
+ <artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>org.springframework.kafka</groupId>
- <artifactId>spring-kafka</artifactId>
- <scope>test</scope>
- </dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
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.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-
-import java.util.Properties;
+import org.springframework.kafka.core.ConsumerFactory;
@Configuration
public ExampleConsumer<String, String> exampleConsumer(
Consumer<String, String> kafkaConsumer,
ApplicationProperties properties,
+ KafkaProperties kafkaProperties,
ConfigurableApplicationContext applicationContext)
{
return
new ExampleConsumer<>(
- properties.getClientId(),
+ kafkaProperties.getClientId(),
properties.getConsumerProperties().getTopic(),
kafkaConsumer,
() -> applicationContext.close());
}
@Bean(destroyMethod = "")
- public KafkaConsumer<String, String> kafkaConsumer(ApplicationProperties properties)
+ public Consumer<?, ?> kafkaConsumer(ConsumerFactory<?, ?> consumerFactory)
{
- Properties props = new Properties();
- props.put("bootstrap.servers", properties.getBootstrapServer());
- props.put("client.id", properties.getClientId());
- props.put("group.id", properties.getConsumerProperties().getGroupId());
- if (properties.getConsumerProperties().getAutoOffsetReset() != null)
- {
- props.put("auto.offset.reset", properties.getConsumerProperties().getAutoOffsetReset().name());
- }
- if (properties.getConsumerProperties().getAutoCommitInterval() != null)
- {
- props.put("auto.commit.interval", properties.getConsumerProperties().getAutoCommitInterval());
- }
- props.put("metadata.max.age.ms", 5000); // 5 Sekunden
- props.put("key.deserializer", StringDeserializer.class.getName());
- props.put("value.deserializer", StringDeserializer.class.getName());
-
- return new KafkaConsumer<>(props);
+ return consumerFactory.createConsumer();
}
}
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
-import java.time.Duration;
-
@ConfigurationProperties(prefix = "juplo")
@Validated
@Setter
public class ApplicationProperties
{
- @NotNull
- @NotEmpty
- private String bootstrapServer;
- @NotNull
- @NotEmpty
- private String clientId;
-
@NotNull
private ConsumerProperties consumer;
@Setter
static class ConsumerProperties
{
- @NotNull
- @NotEmpty
- private String groupId;
@NotNull
@NotEmpty
private String topic;
- private OffsetReset autoOffsetReset;
- private Duration autoCommitInterval;
-
- enum OffsetReset { latest, earliest, none }
}
}
juplo:
- bootstrap-server: :9092
- client-id: DEV
consumer:
- group-id: my-group
topic: test
- auto-offset-reset: earliest
- auto-commit-interval: 5s
management:
endpoint:
shutdown:
enabled: true
info:
kafka:
- bootstrap-server: ${juplo.bootstrap-server}
- client-id: ${juplo.client-id}
+ 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: ${juplo.consumer.group-id}
- topic: ${juplo.consumer.topic}
- auto-offset-reset: ${juplo.consumer.auto-offset-reset}
- auto-commit-interval: ${juplo.consumer.auto-commit-interval}
+ group-id: my-group
logging:
level:
root: INFO
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-@SpringBootTest(
- properties = {
- "juplo.bootstrap-server=${spring.embedded.kafka.brokers}",
- "juplo.consumer.topic=" + TOPIC })
+@SpringBootTest(properties = { "juplo.consumer.topic=" + TOPIC })
@AutoConfigureMockMvc
@EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
public class ApplicationTests