How to start Spring Kafka Application with Spring Boot
Tags: springboot, activemq, springkafka
Published: 2021-04-29 10:46:53

https://grokonez.com/java-integration/distributed-system/how-to-start-spring-kafka-application-with-spring-boot

How to start Spring Kafka Application with Spring Boot

The <strong>Spring Apache Kafka</strong> (spring-kafka) provides a high-level abstraction for Kafka-based messaging solutions. And in the previous <a href="https://grokonez.com/spring-framework/spring-boot/start-spring-apache-kafka-application-springboot">post</a>, We had developed a <strong>Spring Kafka Application</strong> with the <strong>auto-configuration</strong> supported by SpringBoot (from version <strong>1.5</strong>). But when we need explicitly configure <strong>Kafka factories</strong> (<strong>Kafka Producer</strong> and <strong>Kafka Consumer</strong>) for development, how to do it? So in the tutorial, <strong>JavaSampleApproach</strong> will introduce an alternative solution by manually configure <strong>Kafka factories</strong> to build a <strong>Spring Kafka Application</strong>.

Related Articles:
- <a href="https://grokonez.com/java-integration/start-apache-kafka">How to start Apache Kafka</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/start-spring-apache-kafka-application-springboot">How to start Spring Apache Kafka Application with SpringBoot Auto-Configuration</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/how-to-use-spring-kafka-jsonserializer-jsondeserializer-to-produceconsume-java-objects">How to use Spring Kafka JsonSerializer(JsonDeserializer) to produce/consume Java Object messages</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/create-spring-rabbitmq-producerconsumer-application-springboot">How to create Spring RabbitMQ Producer/Consumer application with SpringBoot</a>

<!--more-->
<iframe width="853" height="480" src="https://www.youtube.com/embed/A2FXupo7FLs?rel=0" frameborder="0" allowfullscreen></iframe>
<h2>I. Technologies</h2>
– Java 8
– Maven build
– Spring Boot
– Spring Kafka
– Apache Kafka
– Spring Tool Suite editor
<h2>II. Overview</h2>
<img src="https://grokonez.com/wp-content/uploads/2017/06/Spring-Kafka-Application-architecture-1.png" alt="Spring Kafka Application - architecture" width="359" height="397" class="alignnone size-full wp-image-5962" />

We will explicitly implement a <strong>ProducerFactory</strong> and <strong>ConsumerFactory</strong> with customized properties:

<pre><code class="language-java">
@Bean

public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    ...
    return new DefaultKafkaProducerFactory<>(configProps);
}

@Bean
public ConsumerFactory<String, String> consumerFactory() {
    Map<String, Object> props = new HashMap<>();
    ...
    return new DefaultKafkaConsumerFactory<>(props);
}
</code></pre>

Then use <strong>ProducerFactory</strong> to build <strong>KafkaTemplate</strong> and use <strong>ConsumerFactory</strong> to build 
<strong>ConcurrentKafkaListenerContainerFactory</strong> which will handle <code>@KafkaListener</code> later:

More at:

https://grokonez.com/java-integration/distributed-system/how-to-start-spring-kafka-application-with-spring-boot

How to start Spring Kafka Application with Spring Boot