Пример #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();
            }
        }
Пример #2
0
 public static Timestamp ToTimestamp(this DateTime dateTime)
 {
     return(Timestamp.FromDateTime(dateTime));
 }
Пример #3
0
 private static void AssertRoundtrip(Timestamp timestamp, DateTime dateTime)
 {
     Assert.AreEqual(timestamp, Timestamp.FromDateTime(dateTime));
     Assert.AreEqual(dateTime, timestamp.ToDateTime());
     Assert.AreEqual(DateTimeKind.Utc, timestamp.ToDateTime().Kind);
 }