0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Leçon

Intégrer Spring Kafka Starter

Ajoutez les dépendances Spring Kafka nécessaires à votre projet Spring Boot et configurez les propriétés de connexion de base à un cluster Kafka.

Intégrer Spring Kafka Starter est une leçon Advanced Spring Boot 4: Event-Driven Architecture (Kafka) gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Advanced Spring Boot 4: Event-Driven Architecture (Kafka), et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Advanced Spring Boot 4: Event-Driven Architecture (Kafka) comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

Spring Kafka: The Easy Way

Welcome! In this lesson, we'll get your Spring Boot application ready to talk to Apache Kafka. We'll add the essential library and set up basic connection details.

Spring Kafka is a powerful module that simplifies integrating Kafka with Spring applications. It handles much of the boilerplate, letting you focus on your business logic.

Add the Dependency

First things first, we need to add the spring-kafka dependency to your project. If you're using Maven, add this to your pom.xml file:

  • Open your pom.xml.
  • Locate the <dependencies> section.
  • Add the snippet below.
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

Basic Spring Boot Project

Before we configure Kafka, ensure you have a standard Spring Boot project. This typically includes:

  • A main application class annotated with @SpringBootApplication.
  • An application.properties (or application.yml) file in src/main/resources.

This setup provides the foundation for Spring's auto-configuration magic.

Configure Kafka Connection

Spring Boot automatically picks up configuration from application.properties or application.yml. This is where we'll tell our application how to find the Kafka cluster.

All Kafka-related properties usually start with spring.kafka., making them easy to identify and manage.

Key Config: Bootstrap Servers

The most crucial configuration for connecting to Kafka is spring.kafka.bootstrap-servers. This property specifies a comma-separated list of Kafka broker addresses.

Your application uses these addresses to establish an initial connection to the Kafka cluster. Think of them as entry points.

  • Example: localhost:9092 for a local setup.
  • Example: kafka1.example.com:9092,kafka2.example.com:9092 for a cluster.

Producer Settings (Preview)

While we won't implement a producer yet (that's the next lesson!), it's good to know that producer-specific settings also go into your properties file.

These settings control how your application sends messages. Examples include:

  • spring.kafka.producer.key-serializer
  • spring.kafka.producer.value-serializer

We'll dive into customizing these later.

Consumer Settings (Preview)

Similarly, when your application needs to receive messages, you'll configure consumer-specific properties. These control how messages are consumed.

Common consumer settings include:

  • spring.kafka.consumer.group-id
  • spring.kafka.consumer.auto-offset-reset

We'll explore these in detail when building consumers.

Minimal Kafka Properties

For our initial setup, we only need the bootstrap-servers. Add this line to your src/main/resources/application.properties file:

spring.kafka.bootstrap-servers=localhost:9092

Starting a Kafka-Ready App

With the dependency added and bootstrap-servers configured, your Spring Boot app is now ready to interact with Kafka! Try running this simple application.

Spring Kafka will auto-configure the necessary components based on your properties, even if you're not sending or receiving messages yet.

package com.coddykit.kafka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class KafkaIntegrationApp {

  public static void main(String[] args) {
    SpringApplication.run(KafkaIntegrationApp.class, args);
    System.out.println("Spring Boot Kafka app started. Ready to connect!");
  }
}

Quick Check

You've just learned how to set up a basic Spring Boot application for Kafka. Let's test your understanding!

Recap: Ready for Kafka!

Great job! You've successfully integrated the Spring Kafka Starter into your Spring Boot project. Here's what you accomplished:

  • Added the spring-kafka dependency.
  • Understood the role of application.properties for Kafka configuration.
  • Configured the vital spring.kafka.bootstrap-servers property.

Your application is now prepared to send and receive messages. In the next lesson, we'll dive into building a Spring Boot Kafka Producer!

Questions Fréquemment Posées

La leçon « Intégrer Spring Kafka Starter » est-elle gratuite ?

Oui — le texte complet de « Intégrer Spring Kafka Starter » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Advanced Spring Boot 4: Event-Driven Architecture (Kafka), passe à CoddyKit PRO. Le cours Advanced Spring Boot 4: Event-Driven Architecture (Kafka) comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Intégrer Spring Kafka Starter » ?

Ajoutez les dépendances Spring Kafka nécessaires à votre projet Spring Boot et configurez les propriétés de connexion de base à un cluster Kafka. Tu pratiques Advanced Spring Boot 4: Event-Driven Architecture (Kafka) avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ?

Aucune expérience préalable n'est requise. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.

Combien de temps prend la leçon « Intégrer Spring Kafka Starter » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ?

Oui. Chaque leçon Advanced Spring Boot 4: Event-Driven Architecture (Kafka) inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Intégrer Spring Kafka Starter
  2. Envoyer des messages avec KafkaTemplate
  3. Personnaliser les configurations des producteurs
  4. Gérer les rappels d’envoi et les accusés de réception des producteurs
← Retour à Advanced Spring Boot 4: Event-Driven Architecture (Kafka)