TL.Kafka
0.6.0
dotnet add package TL.Kafka --version 0.6.0
NuGet\Install-Package TL.Kafka -Version 0.6.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TL.Kafka" Version="0.6.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TL.Kafka" Version="0.6.0" />
<PackageReference Include="TL.Kafka" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TL.Kafka --version 0.6.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TL.Kafka, 0.6.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TL.Kafka@0.6.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TL.Kafka&version=0.6.0
#tool nuget:?package=TL.Kafka&version=0.6.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
🦅 TL.Messaging.Kafka
Adaptador resiliente de mensageria para Apache Kafka implementando a abstração unificada
IEventProducereIEventHandler<T>, com suporte nativo a Partition Keys, Dead Letter Topic (.dlt), Idempotência, injeção de headers de telemetria e injeção de dependência simplificada para .NET 8 e .NET 9.
📦 Instalação
Adicione o pacote ao seu projeto via .NET CLI:
dotnet add package TL.Kafka --version 0.2.0
🚀 Como Usar
1. Configuração no Program.cs
using TL.Messaging.Kafka.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Registra a infraestrutura do Kafka e o IEventProducer agnóstico
builder.Services.AddKafkaMessaging(builder.Configuration);
// Registra o consumidor de eventos para um handler específico
builder.Services.AddKafkaConsumer<OrderCreatedEvent, OrderCreatedHandler>(topic: "events.orders.v1");
var app = builder.Build();
app.Run();
2. Publicando Eventos de Forma Agnóstica (IEventProducer)
Sua camada de aplicação ou domínio depende apenas da interface agnóstica IEventProducer:
using TL.BaseContracts.Messaging;
public class OrderService
{
private readonly IEventProducer _producer;
public OrderService(IEventProducer producer)
{
_producer = producer;
}
public async Task CreateOrderAsync(OrderDto order, CancellationToken ct)
{
var orderEvent = new OrderCreatedEvent(order.Id, order.Total);
// Opcional: Garante partição estrita no Kafka usando PartitionKey
var metadata = new EventMetadata().WithKafkaPartitionKey(order.CustomerId);
// Publica de forma transparente!
await _producer.PublishAsync(orderEvent, metadata, ct);
}
}
3. Consumindo Eventos (IEventHandler<T>)
using TL.BaseContracts;
using TL.BaseContracts.Messaging;
public class OrderCreatedHandler : IEventHandler<OrderCreatedEvent>
{
public async Task<Result> HandleAsync(EventMessage<OrderCreatedEvent> message, CancellationToken ct)
{
Console.WriteLine($"Processando pedido {message.Payload.OrderId}, CorrelationId: {message.CorrelationId}");
// Retornar Result.Success() executa o commit manual de offset no Kafka
// Retornar Result.Failure(...) encaminha a mensagem para o Dead Letter Topic (DLT)
return Result.Success();
}
}
4. Configuração no appsettings.json
{
"Kafka": {
"BootstrapServers": "localhost:9092",
"GroupId": "orders-consumer-group",
"DefaultTopic": "events.orders.v1",
"AutoOffsetReset": "Earliest",
"EnableIdempotence": true,
"RetryCount": 3
}
}
🏛️ Recursos de Resiliência
- Idempotência Nativa: Evita duplicação de mensagens no cluster Kafka.
- Headers de Tracing: Propaga
correlation-id,event-typeeevent-idautomaticamente. - Dead Letter Topic (DLT): Mensagens que falham após as retentativas configuradas são publicadas automaticamente no tópico
.dltantes de efetuar o commit do offset, evitando travamento de partição.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Confluent.Kafka (>= 2.5.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- Polly (>= 8.4.1)
- TL.BaseContracts (>= 0.3.1)
-
net9.0
- Confluent.Kafka (>= 2.5.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- Polly (>= 8.4.1)
- TL.BaseContracts (>= 0.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.