我正在尝试在Unity环境中运行一个Kafka代码示例,为此我创建了一个消费者客户端(以下是代码)。
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游戏时,它都会抛出一个错误:Win32Exception:找不到指定的模块。
正如错误所述,存在依赖关系问题,因此我还将这些dll复制到assets / librdkafka / x64文件夹中。重新抛出InvalidOperationException:从Assets / librdkafka.dll加载librdkafka.dll或其依赖项时出错。检查目录是否存在,如果不存在,请检查您的部署过程。在调用Confluent.Kafka Confluent.Kafka之前,您还可以自己加载库及其依赖项。Impl.LibRdKafka.Initialize(System.String userSpecifiedPath)(位于<700d5bbe3b974ce5aed001c82b789f6a>:0)Confluent.Kafka.Consumer..ctor(System.Collections.Generic.IEnumerable<code>1[T] config)(位于<700d5bbe3b974ce5aed001c82b789f6a>:0)Confluent.Kafka.Consumer 2 [TKey,TValue]..ctor(System.Collections.Generic.IEnumerable<code>1[T] config,Confluent.Kafka.Serialization.IDeserializer 1 [T] keyDeserializer,Confluent.Kafka.Serialization.IDeserializer`1 [T] valueDeserializer)(位于<700d5bbe3b974ce5aed001c82b789f6a>:0)KafkaConsumer.Start()(位于Assets / KafkaConsumer.cs:26)
- librdkafka
librdkafkacpp
msvcr120
- zlib
记住:我通过nuget在vs 2017中下载了所有这些dll。然后我将这些dll带到了Unity中。