Beispiel #1
0
        private static void StartProducing(ILogger logger, IConfiguration config, SchemaRegistryConfig schemaRegistryConfig)
        {
            var cachedSchemaRegistryClient = new CachedSchemaRegistryClient(schemaRegistryConfig);
            var producerFactory            = new ProducerFactory <string, Record>(
                logger,
                new ProtobufSerializer <Record>(cachedSchemaRegistryClient),
                KafkaOptions.ForProducer(config));

            var kafkaProducer = producerFactory.CreateProducer();

            for (int i = 0; i < 10; i++)
            {
                var person = new Person
                {
                    Id          = i,
                    Name        = $"{nameof(Person.Name)} {i}",
                    Email       = $"{nameof(Person.Email)} {i}",
                    Age         = 20 + i,
                    PhoneNumber = $"{nameof(Person.PhoneNumber)} {i}"
                };

                var address = new Address
                {
                    PersonId = i,
                    State    = $"{nameof(Address.State)} {i}",
                    Street   = $"{nameof(Address.Street)} {i}",
                    ZipCode  = $"{nameof(Address.ZipCode)} {i}",
                };

                var personRecord = new Record {
                    CreatedDate = Timestamp.FromDateTime(DateTime.UtcNow), Id = Guid.NewGuid().ToString(), Payload = Any.Pack(person)
                };
                var addressRecord = new Record {
                    CreatedDate = Timestamp.FromDateTime(DateTime.UtcNow), Id = Guid.NewGuid().ToString(), Payload = Any.Pack(address)
                };

                var personMessage = new Confluent.Kafka.Message <string, Record>
                {
                    Key   = Guid.NewGuid().ToString(),
                    Value = personRecord
                };

                var addressMessage = new Message <string, Record>
                {
                    Key   = Guid.NewGuid().ToString(),
                    Value = addressRecord
                };

                logger.Information(
                    "Sending message => Topic: {Topic} Key: {Key} Value: {Value}",
                    PERSON_TOPIC,
                    personMessage.Key,
                    personMessage.Value);

                kafkaProducer.ProduceAsync(PERSON_TOPIC, personMessage)
                .GetAwaiter()
                .GetResult();

                logger.Information(
                    "Sending message => Topic: {Topic} Key: {Key} Value: {Value}",
                    PERSON_TOPIC,
                    addressMessage.Key,
                    addressMessage.Value);

                kafkaProducer.ProduceAsync(PERSON_TOPIC, addressMessage)
                .GetAwaiter()
                .GetResult();
            }
        }
Beispiel #2
0
        private static void StartProducing(ILogger logger, IConfiguration config)
        {
            var producerFactory = new ProducerFactory <string, Record>(
                logger,
                new StringSerializer <Record>(e => Encode.FromString(JsonConvert.SerializeObject(e))),
                KafkaOptions.ForProducer(config));

            var kafkaProducer = producerFactory.CreateProducer();

            for (int i = 0; i < 10; i++)
            {
                var person = new Person
                {
                    Id          = i,
                    Name        = $"{nameof(Person.Name)} {i}",
                    Email       = $"{nameof(Person.Email)} {i}",
                    Age         = 20 + i,
                    PhoneNumber = $"{nameof(Person.PhoneNumber)} {i}"
                };

                var address = new Address
                {
                    PersonId = i,
                    State    = $"{nameof(Address.State)} {i}",
                    Street   = $"{nameof(Address.Street)} {i}",
                    ZipCode  = $"{nameof(Address.ZipCode)} {i}",
                };

                var personRecord  = Record.Create(person, typeof(Person));
                var addressRecord = Record.Create(address, typeof(Address));

                var personMessage = new Message <string, Record>
                {
                    Key   = person.Id.ToString(),
                    Value = personRecord
                };

                var addressMessage = new Message <string, Record>
                {
                    Key   = person.Id.ToString(),
                    Value = addressRecord
                };

                logger.Information(
                    "Sending message => Topic: {Topic} Key: {Key} Value: {Value}",
                    PERSON_TOPIC,
                    personMessage.Key,
                    personMessage.Value);

                kafkaProducer.ProduceAsync(PERSON_TOPIC, personMessage)
                .GetAwaiter()
                .GetResult();


                logger.Information(
                    "Sending message => Topic: {Topic} Key: {Key} Value: {Value}",
                    PERSON_TOPIC,
                    addressMessage.Key,
                    addressMessage.Value);

                kafkaProducer.ProduceAsync(PERSON_TOPIC, addressMessage)
                .GetAwaiter()
                .GetResult();
            }
        }