Table of Contents

Kafka - Basic

This sample implements the simplest possible producer and consumer.

What This Sample Shows

  • Connecting Silverback to Kafka with AddKafka and moving the broker clients configuration to an IBrokerClientsConfigurator.
  • Configuring a producer that routes the SampleMessage to the samples-basic topic and publishing it through the IPublisher from a hosted service (one message every 100 ms).
  • Configuring a consumer with a group id, starting from the earliest offset when no offset is stored for the group.
  • Applying an error policy that retries each message twice and then skips it.
  • Subscribing to the IInboundEnvelope<SampleMessage> to access the message together with its envelope.

How to Run

Start the infrastructure as described in Samples, then run the two applications from the repository root.

dotnet run --project ./samples/Kafka/Basic.Producer/.
dotnet run --project ./samples/Kafka/Basic.Consumer/.

Observe the console output while the messages are produced and consumed.

Common

The message being exchanged is defined in a common project.

namespace Silverback.Samples.Kafka.Basic.Common;

public class SampleMessage
{
    public int Number { get; set; }
}

Full source code: https://github.com/BEagle1984/silverback/tree/main/samples/Kafka/Basic.Common

Producer

The producer uses a hosted service to publish some messages in the background.

using Microsoft.Extensions.DependencyInjection;
using Silverback.Configuration;
using Silverback.Messaging.Configuration;

namespace Silverback.Samples.Kafka.Basic.Producer;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Enable Silverback
        services.AddSilverback()

            // Use Apache Kafka as message broker
            .WithConnectionToMessageBroker(options => options.AddKafka())

            // Delegate the broker clients configuration to a separate class
            .AddBrokerClientsConfigurator<BrokerClientsConfigurator>();

        // Add the hosted service that produces the random sample messages
        services.AddHostedService<ProducerBackgroundService>();
    }

    public void Configure()
    {
    }
}

Full source code: https://github.com/BEagle1984/silverback/tree/main/samples/Kafka/Basic.Producer

Consumer

The consumer processes the messages and outputs their value to the standard output.

using Microsoft.Extensions.DependencyInjection;
using Silverback.Configuration;
using Silverback.Messaging.Configuration;

namespace Silverback.Samples.Kafka.Basic.Consumer;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Enable Silverback
        services.AddSilverback()

            // Use Apache Kafka as message broker
            .WithConnectionToMessageBroker(options => options.AddKafka())

            // Delegate the broker clients configuration to a separate class
            .AddBrokerClientsConfigurator<BrokerClientsConfigurator>()

            // Register the subscribers
            .AddSingletonSubscriber<SampleMessageSubscriber>();
    }

    public void Configure()
    {
    }
}

Full source code: https://github.com/BEagle1984/silverback/tree/main/samples/Kafka/Basic.Consumer

Additional Resources