coretech9 / kafkas

C# Consumer Implementation for Apache Kafka

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kafkas

C# Consumer Implementation for Apache Kafka.

NuGet

Sample Usage:

appsettings.json

  "Kafkas": {
    "Options": {
      "ConsumerGroupId": "SampleGroupId",
      "RetryCount": 5,
      "RetryWaitMilliseconds": 100,
      "RetryDelayStrategy": "Fixed",
      "CommitErrorMessages": true
    },
    "Consumer": {
      "SaslUsername": "test",
      "SaslPassword": "test",
      "SaslMechanism": "Plain",
      ... These configs ConsumerConfig class properties of Confluent.Kafka package
    },
    "Producer": {
      "SaslUsername": "test",
      "SaslPassword": "test",
      "Acks": "All",
      ... these configs ProducerConfig class properties of Confluent.Kafka package
    }
  }

IHostBuilder Implementation

IHost host = Host.CreateDefaultBuilder()
    .UseKafkas(builder => builder.AddConsumers(typeof(Program)))
    .Build();

Consumer

[Topic("Foo")]
[ErrorTopic("Foo_Error")]
[ConsumerGroupId("Group1")] //optional. if you want to override default options.
[Retry(5, 50, WaitStrategy.Multiplier)] //optional. if you want to override default options.
public class FooConsumer : ITopicConsumer<Foo>
{
    public async Task Consume(ConsumeContext<Foo> consumeContext)
    {
        // do something
    }
    
    //optional interface method implementation. if you want to do something after a failed try.
    public async Task RetryFallback(ConsumeContext<Foo> consumeContext, Exception exception)
    {
        // do something
    }
}

About

C# Consumer Implementation for Apache Kafka

License:Apache License 2.0


Languages

Language:C# 100.0%