在kafka .Net中发送字节数组

时间:2018-05-21 05:48:27

标签: apache-kafka kafka-consumer-api kafka-producer-api confluent-kafka

我目前正面临着我为Kafka撰写的代码问题。代码非常简单,Producer生成一个字节数组,而消费者想要使用该数组。它的问题是它既不显示任何错误,也不能获得所需的输出。我确信byteArray(大小:4.06MB)输入正在获取要传递给kafka管道的正确值。 Consumer方法都没有提供所需的输出。在方法1中,代码没有通过" Consumer.Consume()"的IF条件。在方法2中," msg"变量即将为空。请告知我做错了什么。

    Kafka Producer:
        public class BookingProducer : IBookingProducer
            {
                public void Produce(byte[] byteArray)
                {
                    var config = new Dictionary<string, object>
                    {
                        { "bootstrap.servers", "localhost:9092"},
                        { "message.max.bytes", "5242880"}
                    };

                    using (var producer = new Producer<Null, byte[]>(config, null, new ByteArraySerializer()))
                    {
                        var result = producer.ProduceAsync("timemanagement_booking", null, byteArray).GetAwaiter().GetResult();
                        //Console.WriteLine($"Value: {result.Value}");
                        producer.Flush(10000);
                    }
                }
            }

    Kafka Consumer Method 1:
    public class BookingConsumer
        {
            public void Listen()
            {
                var config = new Dictionary<string, object>
                {
                    { "group.id","booking_consumer" },
                    { "bootstrap.servers", "localhost:9092" },
                    { "enable.auto.commit", "false" },
                    { "fetch.message.max.bytes", "5242880" }
                };

                using (var consumer = new Consumer<Null, byte[]>(config, null, new ByteArrayDeserializer()))
                {
                    consumer.Subscribe("timemanagement_booking");

                    while (true)
                    {
                        Message<Null, byte[]> msg;
                        consumer.Poll(10000);
                        if (consumer.Consume(out msg, TimeSpan.FromSeconds(1)))
                        {
                            Console.WriteLine(msg.Value.ToArray());
                        }
                    }
                }
            }
        }
Kafka Consumer Method 2:
public class BookingConsumer
    {
        public void Listen()
        {
            var config = new Dictionary<string, object>
            {
                { "group.id","booking_consumer" },
                { "bootstrap.servers", "localhost:9092" },
                { "enable.auto.commit", "false" },
                { "fetch.message.max.bytes", "5242880" }
            };

            using (var consumer = new Consumer<Null, byte[]>(config, null, new ByteArrayDeserializer()))
            {
                consumer.Subscribe("timemanagement_booking");
                consumer.OnMessage += (_, msg) =>
                {
                    if (msg != null)
                    {
                        Console.WriteLine(msg.Value.ToArray());
                    }
                };
                while (true)
                {
                    consumer.Poll(10000);
                }
            }
        }
    }

0 个答案:

没有答案