Get started with the Apache Kafka integration
このコンテンツはまだ日本語訳がありません。
Apache Kafka is a distributed streaming platform that enables you to build real-time data pipelines and streaming applications. The Aspire Apache Kafka integration enables you to connect to existing Kafka instances or create new instances from Aspire with the confluentinc/confluent-local container image.
In this introduction, you’ll see how to install and use the Aspire Apache Kafka integrations in a simple configuration. If you already have this knowledge, see Apache Kafka hosting integration for full reference details.
Set up hosting integration
Section titled “Set up hosting integration”To begin, install the Aspire Kafka hosting integration in your Aspire AppHost project:
aspire add kafkaAspire CLI は対話的です。求められたら適切な結果を選択してください:
Select an integration to add:
> kafka (Aspire.Hosting.Kafka)> Other results listed as selectable options...#:package Aspire.Hosting.Kafka@*<PackageReference Include="Aspire.Hosting.Kafka" Version="*" />Next, in the AppHost project, create instances of Kafka resources and pass them to the consuming client projects:
var builder = DistributedApplication.CreateBuilder(args);
var kafka = builder.AddKafka("kafka");
builder.AddProject<Projects.ExampleProject>() .WithReference(kafka);
builder.Build().Run();When Aspire adds a container image to the AppHost, it creates a new Kafka server instance on your local machine.
Set up client integration
Section titled “Set up client integration”To get started with the Aspire Apache Kafka client integration, install the package:
dotnet add package Aspire.Confluent.Kafka#:package Aspire.Confluent.Kafka@*<PackageReference Include="Aspire.Confluent.Kafka" Version="*" />In the Program.cs file of your client-consuming project, call the AddKafkaProducer extension method to register an IProducer<TKey, TValue> for use via the dependency injection container:
builder.AddKafkaProducer<string, string>("kafka");You can then retrieve the IProducer<TKey, TValue> instance using dependency injection:
internal sealed class Worker(IProducer<string, string> producer) : BackgroundService{ // Use producer...}