unity3d 中的 Kafka 集成抛出 Win32Exception 错误

Kafka integration in unity3d throwing Win32Exception error

我正在尝试 运行 在统一环境中 Kafka 的代码示例,因此,我创建了一个 consumer 客户端(代码如下)。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Confluent.Kafka;
using Confluent.Kafka.Serialization;
using System.Text;

public class KafkaConsumer : MonoBehaviour 
{
    // Use this for initialization
    void Start () 
    {
        /*
         * The consumer application will then pick the messages from the same topic and write them to console output. 
         * The process to create the consumer application is also very simple.
         */
        var config = new Dictionary<string, object>
        {
            { "group.id","JavaInUseGroup" },
            { "bootstrap.servers", "localhost:9092" },
            { "enable.auto.commit", "false" }
        };

        using (var consumer = new Consumer<Null, string>(config, null, new StringDeserializer(Encoding.UTF8)))
        {
            consumer.Subscribe(new string[] { "javainuse-topic" });

            consumer.OnMessage += (_, msg) =>
            {
                //Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset :{msg.Offset} {msg.Value}");
                Debug.Log($"Topic: {msg.Topic} Partition: {msg.Partition} Offset :{msg.Offset} {msg.Value}");

                consumer.CommitAsync(msg);
            };

            while (true)
            {
                consumer.Poll(100);
            }
        }
    }
}

为了执行上面的代码示例,我还在我的项目资产文件夹中添加了 confluent.Kafka dll。但是每当我 运行 我的 unity 游戏它抛出一个 error:Win32Exception: The specified module could not be found.

Rethrow as InvalidOperationException: Error while loading librdkafka.dll or its dependencies from Assets/librdkafka.dll. Check the directory exists, if not check your deployment process. You can also load the library and its dependencies by yourself before any call to Confluent.Kafka Confluent.Kafka.Impl.LibRdKafka.Initialize (System.String userSpecifiedPath) (at <700d5bbe3b974ce5aed001c82b789f6a>:0) Confluent.Kafka.Consumer..ctor (System.Collections.Generic.IEnumerable1[T] config) (at <700d5bbe3b974ce5aed001c82b789f6a>:0) Confluent.Kafka.Consumer2[TKey,TValue]..ctor (System.Collections.Generic.IEnumerable1[T] config, Confluent.Kafka.Serialization.IDeserializer1[T] keyDeserializer, Confluent.Kafka.Serialization.IDeserializer`1[T] valueDeserializer) (at <700d5bbe3b974ce5aed001c82b789f6a>:0) KafkaConsumer.Start () (at Assets/KafkaConsumer.cs:26)

由于错误指出存在依赖性问题所以我也将这些 dll 复制到 assets/librdkafka/x64 文件夹

  1. librdkafka
  2. librdkafkacpp

  3. msvcr120

  4. zlib

现在的问题是,每当我尝试播放我的项目时都会卡住。

记住:我在vs 2017中通过nuget下载了所有这些dll。然后我将这些dll整合到unity中。

对于未来的用户,以下是在您的 Unity3d 项目中添加 Kafka 的过程: 实际上,在您的项目中添加 dll 有一个特定的顺序或文件夹层次结构。 (我没有找到任何权威参考,如果有人找到请分享)

  1. 首先将 Confluen.Kafka dll 粘贴到您的文件夹中

  1. 然后创建 librdkafka 文件夹(确保它是在 confluent.kafka dll 旁边创建的)并将与平台相关的 dll 粘贴到 x64 或 x86 文件夹中

现在,您可以运行我的代码示例(在问题中提到)。

IMP 注意:构建播放器后,您必须手动复制 Player/Managed/librdkafka 文件夹中的 dll 文件。您必须在托管文件夹中创建 librdkafka 文件夹,然后粘贴您的 dll。(同样,我不知道为什么需要它,但如果有人找到权威参考,然后分享)

如果您查看源代码:https://github.com/confluentinc/confluent-kafka-dotnet/blob/master/src/Confluent.Kafka/Impl/LibRdKafka.cs#L323-L374,confluent.kafka 将动态加载 librdkafka,这些 dll:

  1. libeay32.dll
  2. librdkafka.dll
  3. librdkafkacpp.dll
  4. libzstd.dll
  5. msvcp120.dll
  6. msvcr120.dll
  7. ssleay32.dll
  8. zlib.dll

要么需要复制到 Confluent.Kafka.dll 所在的同一文件夹,要么像这样在同一文件夹中创建一个 librdkafka 文件夹:

\---librdkafka
    +---x64
    |       libeay32.dll
    |       librdkafka.dll
    |       librdkafkacpp.dll
    |       libzstd.dll
    |       msvcp120.dll
    |       msvcr120.dll
    |       ssleay32.dll
    |       zlib.dll
    |
    \---x86
            libeay32.dll
            librdkafka.dll
            librdkafkacpp.dll
            libzstd.dll
            msvcp120.dll
            msvcr120.dll
            ssleay32.dll
            zlib.dll