KafkaTemplate is Spring for Apache Kafka’s typed client for sending domain objects to Kafka topics. Adding the Spring Kafka dependency triggers Spring Boot autoconfiguration, which places a KafkaTemplate in the application context for injection. section 9.3.1 section 9.3.2
Use KafkaTemplate’s send methods to provide the message details that matter:
- Specify a topic and payload; optionally provide a key, partition, timestamp, ProducerRecord, or Message. section 9.3.2
- Focus on the topic and payload for straightforward publishing; partitions and keys add routing information without changing the basic usage. section 9.3.2
- Use sendDefault when a default topic is configured instead of passing the topic to every call. section 9.3.2
Publishing an order
Inject KafkaTemplate<String, TacoOrder> into a service, then call kafkaTemplate.send("tacocloud.orders.topic", order) from sendOrder. The generic type lets the template send the TacoOrder directly. section 9.3.2
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
Add the Spring Kafka dependency that enables the Kafka integration and related Boot autoconfiguration. section 9.3.1
spring:
kafka:
bootstrap-servers:
- localhost:9092
Configure localhost:9092 for a locally running Kafka broker; production deployments require different bootstrap-server settings. section 9.3.1

