简体   繁体   English

无法连接kafka集群时无异常

[英]No exception when unable to connect to kafka cluster

I am unable get an exception when the program fails to connect to the kafka cluster.当程序无法连接到 kafka 集群时,我无法获得异常。 The code outputs the exception in the console logs but I need it throw an exception.代码在控制台日志中输出异常,但我需要它抛出异常。 I am using this c# library: https://github.com/confluentinc/confluent-kafka-do.net我正在使用这个 c# 库: https://github.com/confluentinc/confluent-kafka-do.net

ProducerConfig _configKafka = new ProducerConfig { BootstrapServers ="localhost:9092/" };

ProducerBuilder<string, string> _kafkaProducer = new ProducerBuilder<string, string>(_configKafka);
using (var kafkaProducer = _kafkaProducer.Build())
{

    try
    {
        var dr = kafkaProducer.ProduceAsync("Kafka_Messages", new Message<string, string> { Key = null, Value = $"message {i++}" });
        dr.Wait(TimeSpan.FromSeconds(10));

        if(dr.Exception!=null)
        {
            Console.WriteLine($"Delivery failed:");
        }

        var status = dr.Status;

        //Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
    }

    catch (ProduceException<Null, string> e)
    {
        Console.WriteLine($"Delivery failed: {e.Error.Reason}");
    }
}

Below is the error printed by confluent-kafka in console:下面是 confluent-kafka 在控制台打印的错误:

%3|1565248275.024|FAIL|rdkafka#producer-1| [thrd:localhst:9092/bootstrap]: localhst:9092/bootstrap: Failed to resolve 'localhst:9092': No such host is known.  (after 2269ms in state CONNECT)
%3|1565248275.024|ERROR|rdkafka#producer-1| [thrd:localhst:9092/bootstrap]: localhst:9092/bootstrap: Failed to resolve 'localhst:9092': No such host is known.  (after 2269ms in state CONNECT)
%3|1565248275.025|ERROR|rdkafka#producer-1| [thrd:localhst:9092/bootstrap]: 1/1 brokers are down

To get the actual exception within your application you need to add .SetErrorHandler() : 为了在您的应用程序中获取实际的异常,您需要添加.SetErrorHandler()

    ProducerBuilder<string, string> _kafkaProducer = new ProducerBuilder<string, string>(_configKafka);

    using (var kafkaProducer = _kafkaProducer.SetErrorHandler((producer, error) =>
                        {
                           //You can handle error right here

                        }).Build())

error.Reason contains the error message error.Reason包含错误消息

You can use both .SetLogHandler and .SetErrorHandler in your consumer and producer code.您可以在消费者和生产者代码中同时使用.SetLogHandler.SetErrorHandler Otherwise it kind of silently fails without providing much details.否则它会在不提供太多细节的情况下默默地失败。 You can forward the messages to your logger there.您可以将消息转发到那里的记录器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 .net 连接到 Kafka 服务器 - Unable to connect to Kafka server using .net Kafka Producer 连接不上服务器,不会抛出任何异常 - Kafka Producer can't connect to servers and will not throw any exception 异常:“无法连接到任何指定的mysql主机” - exception: “unable to connect to any of the specified mysql hosts” Ftp:无法连接到远程服务器异常 - Ftp : Unable to connect to remote server exception Cassandra没有主机异常,无法连接到任何服务器 - Cassandra No host exception and Unable to connect to any servers 无法使用 stackexchange.redis 连接到 Redis 集群 - unable to connect to Redis cluster using stackexchange.redis 尝试连接到数据库时出现异常 - Exception when attempting to connect to database 当尝试使用Task.Run()在MySql中运行插入查询时,它引发以下异常:“无法连接到任何指定的MySQL主机。” - When trying to run Insert query in MySql using Task.Run(), it throws the follow exception: “Unable to connect to any of the specified MySQL hosts.” 尝试连接到.sdf数据库时出现异常 - Exception when trying to connect to .sdf database 尝试连接到SQL Azure数据库时发生异常 - Exception when trying to connect to SQL Azure Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM