Producing Using .Net Axual Client

Creating A Producer Application

When you have completed this step, you will have produced data in AVRO format to the stream you have configured in Creating topics.

Axual client doesn’t support SASL authentication. If you want to use SASL, please refer Kafka client producer using SASL

Adding Dependencies

Start by including the dependency on the Axual .NET client library as you would do with any dependency or
by adding PackageReference tag in your .csproj project.

For full executable Axual client Avro producer example, please refer examples repository.
<ItemGroup>
      <PackageReference Include="Axual.Kafka.Proxy" Version="1.4.0" />
      <PackageReference Include="Axual.SchemaRegistry.Serdes.Avro" Version="1.4.0" />
</ItemGroup>

Building The Application

In order to create a very basic producer application, this is all you need.
Next step is to create the configuration for the AxualProducerBuilder.

Producer Configuration

var config = new AxualProducerConfig
{
    // This is the app ID as it is configured in the self service
    ApplicationId ="logeventproducer",
    // The endoint of the Discovery API (used to retrieve information about bootstrap servers, schema registry, TTL etc...)
    EndPoint = new UriBuilder("https", "192.168.99.100", 443).Uri,
    // The tenant you are part of
    Tenant = "demo",
    // The environment corresponding to the shortname of the env in the self service
    Environment = "example",

    // The ssl configuration for your application. The certificate used should have a DN
    // matching the DN that was configured in self service

    // Server verifies the identity of the client
    // (i.e. that the public key certificate provided by the client has been signed by a
    // CA trusted by the server).
    SslKeystorePassword = "notsecret",
    SslKeystoreLocation = SSL_KEYSTORE_LOCATION, (1)
    SecurityProtocol = SecurityProtocol.Ssl,
    EnableSslCertificateVerification = true,
    // Client verifies the identity of the broker
    SslCaLocation = SSL_CA_PATH, (2)
    SslEndpointIdentificationAlgorithm = SslEndpointIdentificationAlgorithm.None,
};
1 For SSL_KEYSTORE_LOCATION use the absolute path to the keystore file, see also Data security (application certificate)
2 For SSL_CA_PATH use the absolute path to the CA file, see also Data security (Setting up trust)

Creating The Producer

With the above configurations, we can instantiate the AxualProducerBuilder to build a AxualProducer instantiate.

var producer = new AxualProducerBuilder<Application, ApplicationLogEvent>(config)
    .SetKeySerializer(new SpecificAvroSerializer<Application>())
    .SetValueSerializer(new SpecificAvroSerializer<ApplicationLogEvent>())
    .SetLogHandler((_, l) => Console.WriteLine($"> [{l.Level}]: {l.Message}"))
    .SetErrorHandler((_, e) => Console.WriteLine($"> [Error]: {e.Reason}"))
    .Build())

Producing Messages

Now, we are ready to start sending records using the AxualProducer.

var application = new Application
{
    name = "Axual Proxy .NET Specific Avro Producer",
    version = "1.9.9",
    owner = "Team Log"
};


// Produce 10 messages
for (var i = 0; i < 10; i++)
{
    try
    {
        var applicationLogEvent = new ApplicationLogEvent
        {
            timestamp = 1000 + i,
            source = application,
            message = "Message " + i,
            context = new Dictionary<string, string> {{$"some key {i}", $"some value {i}"}},
            level = ApplicationLogLevel.INFO
        };

        var message = new Message<Application, ApplicationLogEvent>
        {
            Key = application,
            Value = applicationLogEvent
        };

        producer.Produce("applicationlogevents", message,
            r => Console.WriteLine(!r.Error.IsError
                ? $"> Produced message to stream {r.Topic} partition {r.Partition} offset {r.Offset}"
                : $"> Delivery Error: {r.Error.Reason}"));
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}
producer.Flush();
Console.WriteLine("> Finish");

When all of the above steps have been done correctly, start your producer app. Your app will produce messages to the console that will look like this:

--------------------------------------------------------------------------------------
'axual_client_proxy_specific_avro_producer' producing to stream 'applicationlogevents'
--------------------------------------------------------------------------------------
> Produced message to stream applicationlogevents partition [1] offset 41667
> Produced message to stream applicationlogevents partition [1] offset 41668
> Produced message to stream applicationlogevents partition [1] offset 41669
> Produced message to stream applicationlogevents partition [1] offset 41670
> Produced message to stream applicationlogevents partition [1] offset 41671
> Produced message to stream applicationlogevents partition [1] offset 41672
> Produced message to stream applicationlogevents partition [1] offset 41673
> Produced message to stream applicationlogevents partition [1] offset 41674
> Produced message to stream applicationlogevents partition [1] offset 41675
> Produced message to stream applicationlogevents partition [1] offset 41676
> Finish

Next Step: Consuming Data Using A .NET Consumer Application

In the next step you will create a .NET consumer application to use the data you just produced.