我正在尝试在统一环境中运行 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
。但是每当我运行我的统一游戏时,它都会引发错误:Win32Exception :找不到指定的模块。
作为InvalidOperationException重新抛出:加载时出错 librdkafka.dll或其从Assets / librdkafka.dll的依赖项。校验 该目录存在,如果不存在,请检查您的部署过程。您可以 还可以在调用之前自行加载库及其依赖项 到Confluent.Kafka Confluent.Kafka.Impl.LibRdKafka.Initialize (System.String userSpecifiedPath)(在 <700d5bbe3b974ce5aed001c82b789f6a>:0)Confluent.Kafka.Consumer..ctor (System.Collections.Generic.IEnumerable
1[T] config) (at <700d5bbe3b974ce5aed001c82b789f6a>:0) Confluent.Kafka.Consumer
2 [TKey,TValue] .. ctor (System.Collections.Generic.IEnumerable1[T] config, Confluent.Kafka.Serialization.IDeserializer
1 [T] keyDeserializer, Confluent.Kafka.Serialization.IDeserializer`1 [T] valueDeserializer) (在<700d5bbe3b974ce5aed001c82b789f6a>:0处)KafkaConsumer.Start()(在 Assets / KafkaConsumer.cs:26)
由于错误指出存在依赖关系问题,因此我也将这些dll复制到了asset / librdkafka / x64文件夹中
librdkafkacpp
msvcr120
现在的问题是,每当我尝试播放它时,我的项目都会卡住。
记住:我已经在vs 2017中通过nuget下载了所有这些dll。然后将这些dll统一了。
答案 0 :(得分:1)
对于将来的用户,以下是在Unity3d项目中添加Kafka的过程: 实际上,有一个特定的顺序或文件夹层次结构可在您的项目中添加dll。 (如果有人找到了,请与我分享,但我没有找到任何权威参考)
现在,您可以运行我的代码示例(有问题的提及)。
IMP注意::构建播放器后,您必须手动将dll文件复制到Player / Managed / librdkafka文件夹中。您必须在托管文件夹中创建librdkafka文件夹,然后粘贴您的dll。(再次,我不知道为什么要这样做,但是如果有人发现了权威性的版权,则可以共享)
答案 1 :(得分:0)
如果您查看源代码:https://github.com/confluentinc/confluent-kafka-dotnet/blob/master/src/Confluent.Kafka/Impl/LibRdKafka.cs#L323-L374,confluent.kafka将动态加载librdkafka,这些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