Aller au contenu

Apache Kafka hosting integration

Ce contenu n’est pas encore disponible dans votre langue.

Apache Kafka logo

The Apache Kafka hosting integration models a Kafka resource as the KafkaServerResource type. To access this type and APIs, add the 📦 Aspire.Hosting.Kafka NuGet package in your AppHost project:

Aspire CLI — Ajouter le package Aspire.Hosting.Kafka
aspire add kafka

La CLI Aspire est interactive ; choisissez le résultat approprié lorsque demandé :

Aspire CLI — Exemple de sortie
Select an integration to add:
> kafka (Aspire.Hosting.Kafka)
> Other results listed as selectable options...

In your AppHost project, call AddKafka on the builder instance to add a Kafka server resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka");
builder.AddProject<Projects.ExampleProject>()
.WithReference(kafka);
// After adding all resources, run the app...

When Aspire adds a container image to the AppHost, it creates a new Kafka server instance on your local machine.

To add the Kafka UI to the Kafka server resource, call the WithKafkaUI method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka")
.WithKafkaUI();
builder.AddProject<Projects.ExampleProject>()
.WithReference(kafka);

The Kafka UI is a free, open-source web UI to monitor and manage Apache Kafka clusters.

To change the Kafka UI host port, chain a call to the WithHostPort method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka")
.WithKafkaUI(kafkaUI => kafkaUI.WithHostPort(9100));
builder.AddProject<Projects.ExampleProject>()
.WithReference(kafka);

Add Kafka server resource with data volume

Section titled “Add Kafka server resource with data volume”

To add a data volume to the Kafka server resource, call the WithDataVolume method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka")
.WithDataVolume(isReadOnly: false);
builder.AddProject<Projects.ExampleProject>()
.WithReference(kafka);

The data volume is used to persist the Kafka server data outside the lifecycle of its container.

The Aspire Kafka integration deploys a container from the confluentinc/confluent-local image to your local container host. This image provides a simple Apache Kafka cluster that runs in KRaft mode and requires no further configuration. It’s ideal for developing and testing producers and consumers. However, this image is for local experimentation only and isn’t supported by Confluent.

In the following AppHost code, a local container is used in run mode. At other times, a connection string provides URLs and port numbers for the Kafka brokers:

var kafka = builder.ExecutionContext.IsRunMode
? builder.AddKafka("kafka").WithKafkaUI()
: builder.AddConnectionString("kafka");

The Kafka hosting integration automatically adds a health check for the Kafka server resource. The health check verifies that a Kafka producer with the specified connection name is able to connect and persist a topic to the Kafka server.