简体   繁体   English

当Kafka服务器与生产者之间的连接断开时,消息将如何处理?

[英]what happen to the messages when connection is down between kafka server and producer?

im new in kafka using spring boot and im working in projet that i want to integrate kafka using spring into it , so the problem is i want to send message from the producer to the consumer even if the kafka server don't running (offline mode) 即时通讯是使用spring boot在kafka中提供的新功能,而我要在projet中工作,因此我想使用spring将kafka集成到它中,所以问题是即使kafka服务器未运行,我也要从生产者向消费者发送消息(离线模式)

Can anyone give me example of how using kafka in offline mode , i can't find a tuto for this topic i want to stop my kafka server(for example) and in the same time producer want to send data to the topic,then the consumer can get these message? 谁能给我一个如何在脱机模式下使用kafka的示例,我找不到该主题的主题,我想停止我的kafka服务器(例如),同时生产者想将数据发送到该主题,然后消费者可以获得这些消息吗? what's the best solution?are they true ? 最好的解决方案是什么?是真的吗?

*sending data to a file, and when the server return to run (i test connexion for example),i export data from the file to the topic *将数据发送到文件,然后当服务器恢复运行时(例如,我测试连接),我将数据从文件导出到主题

*sending data to database and when the server return to run (test connexion),the same i send message(data) from database to my topic *将数据发送到数据库,当服务器返回运行状态(测试连接)时,同样,我从数据库向我的主题发送消息(数据)

*using a queue or a list to store message and when the server return to run (test connexion),i send data from the list to the topic but the problem that i have a lot of messages *使用队列或列表存储消息,并且当服务器返回运行时(测试连接),我将数据从列表发送到主题,但是问题是我有很多消息

-->if there are other solution with a simple example ,can anyone help me? ->如果还有其他简单示例的解决方案,有人可以帮助我吗?

this is example of brocker Redis that we test the connection between Redis brocker and producer , if connection fails, i will store data inside a Queue which can store many messages, and when connection return to work between Redis and producer ,the producer now get these messages from the Queue and send them to the Redis Brocker . 这是brocker Redis的示例,我们测试了Redis brocker与生产者之间的连接,如果连接失败,我会将数据存储在一个队列中 ,该队列可以存储许多消息,当连接恢复到Redis与生产者之间的工作时,生产者现在可以获得这些来自队列的消息并将它们发送到Redis Brocker。

But the problem in this brocker ,there are a few message lost so we decide to integrate kafka brocker inside my project instead of Redis brocker! 但是在这个brocker中的问题是,丢失了一些消息,因此我们决定将kafka brocker集成到我的项目中,而不是Redis brocker!

Can anyone give me example in java how to store a lot of message before sending them by producer to kafka cluster?or what's the best solution to this problem because we don't want to use the same Queue solution? 任何人都可以在Java中给我一个例子,在生产者将它们发送到kafka集群之前,如何存储大量消息?或者由于我们不想使用相同的Queue解决方案,此问题的最佳解决方案是什么?

this example in python is how store message inside a Queue if connexion failed to the server: 在python中的此示例是如何在服务器无法连接时将消息存储在队列中的方法:

    try:
    urllib.request.urlopen('http://serverAdress', timeout=0.1)
    r.publish(topicProduction,json_background_of_message1)
    print(json_background_of_message1)
    arretControle=Tru
    except Exception as e:
    qArret.put(json_background_of_message1)
    print("arret")
    arretControle=True

//json_background_of_message1 is the Queue that we can store a lot of messages in this Queue before sending these message if the connexion is failed /// json_background_of_message1是一个队列,如果连接失败,我们可以在发送这些消息之前在此队列中存储很多消息

Kafka is designed to be a highly-available messaging system. Kafka被设计为高度可用的消息传递系统。 Configured correctly, and depending on the replication factor, you can have multiple brokers go down, completely, for days at a time, and the cluster would still work (albeit under higher load probably). 正确配置,并根据复制因素,您可以一次将多个代理完全关闭几天,并且群集仍可以工作(尽管可能在较高的负载下)。 Every single Kafka production cluster I've worked with hasn't been completely down, ever, once it was successfully deployed. 一旦成功部署,我与之合作过的每个Kafka生产集群都不会完全崩溃。 We've had individual brokers go down, for days at a time sometimes, but that was never a problem. 我们有时让个别经纪人破产,有时有时要几天,但这绝不是问题。

What you're proposing is a fallback or backup method, in case Kafka is not available. 如果Kafka不可用,建议您使用备用或备用方法。 However, you still have the same problem. 但是,您仍然有同样的问题。 If you dump messages to a file, how long until you run out of disk space? 如果将消息转储到文件,磁盘空间用完要多长时间? If you store messages in a database, how long until the database runs out space? 如果您将消息存储在数据库中,那么数据库空间用完要多长时间? If you store messages in an in-memory queue, how long until you run out of memory, and crashes the application? 如果您将消息存储在内存中的队列中,那么直到内存用完并导致应用程序崩溃为止需要多长时间? And now you also have to build a mechanism to recover from a kafka outage, which adds complexity and overhead. 现在,您还必须建立一种机制来从kafka中断中恢复,这会增加复杂性和开销。

The best approach with Kafka is to configure it and handle it as a highly-available-system, configure alerts and metrics properly, so you'll be immediately alerted, and can react promptly, if something goes wrong. Kafka的最佳方法是对其进行配置并将其作为高可用性系统进行处理,正确配置警报和指标,这样,如果出现问题,您将立即收到警报并可以迅速做出反应。 Also, you should always size and test your applications so you have enough headroom to handle even your worst case scenario. 另外,您应该始终调整应用程序的大小并对其进行测试,以便有足够的空间来处理最坏的情况。 If you configure it to use replication factor 3, you would be able to lose any two brokers, and the cluster would still be able to function with no data loss. 如果将其配置为使用复制因子3,则将可能丢失任何两个代理,并且群集仍将能够正常运行而不会丢失数据。

Now, on the application side, your behavior in case Kafka is unavailable should depend on how important the messages are. 现在,在应用程序方面,万一Kafka不可用,您的行为应取决于消息的重要性。 If you can tolerate losing messages, then just drop them if producer returns an exception, and log it/send an alert. 如果您可以容忍丢失的消息,那么如果生产者返回异常,则将其丢弃,并记录/发送警报。 However, if they're extremely important records, then you shouldn't acknowledge/commit the messages on your upstream system (wherever the records originated from) until you have full confirmation that they're saved in Kafka. 但是,如果它们是非常重要的记录,那么在完全确认它们已保存在Kafka中之前,您不应该在上游系统(无论记录来自何处)上确认/提交消息。 I would recommend setting producer acks to -1 or all for this, multiple retries in case of failure, and setting up a proper Callback method on the producer.send() method. 我建议为此设置生产者acks到-1all ,在失败的情况下进行多次重试,并在producer.send()方法上设置适当的回调方法。 See here for much more detailed explanation: https://kafka.apache.org/21/javadoc/index.html?org/apache/kafka/clients/producer/Callback.html 请参阅此处以获取更多详细说明: https : //kafka.apache.org/21/javadoc/index.html? org/ apache/ kafka/ clients/ producer/ Callback.html

For more details, like others have said, please give the official docs a read: https://kafka.apache.org/documentation/ 如其他人所说,有关更多详细信息,请阅读官方文档: https : //kafka.apache.org/documentation/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM