我有以下代码片段,使用了来自生产者的消息:
public class KafkaAvroExampleWithWait : MonoBehaviour
{
ConsumeResult<string, GenericRecord> consumeResult2;
string bootstrapServers = "localhost:9092";
string schemaRegistryUrl = "Production163:8081";
string topicName = "player";
string groupName = "avro-generic-example-group123456";
// Start is called before the first frame update
void Start()
{
Debug.Log("start called");
StartCoroutine(Main2());
}
IEnumerator Main2()
{
Debug.Log("Main Called");
using (CachedSchemaRegistryClient schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { SchemaRegistryUrl = schemaRegistryUrl }))
using (IConsumer<string, GenericRecord> consumer = new ConsumerBuilder<string, GenericRecord>(new ConsumerConfig { BootstrapServers = bootstrapServers, GroupId = groupName })
.SetKeyDeserializer(new AsyncAvroDeserializer<string>(schemaRegistry).AsSyncOverAsync())
.SetValueDeserializer(new AsyncAvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
.SetErrorHandler((_, e) => Debug.Log($"Error: {e.Reason}"))
.Build())
{
Debug.Log("subscribe" + this.name);
consumer.Subscribe(topicName);
while (true)
{
ConsumeResult<string, GenericRecord> consumeResult = consumer.Consume(new TimeSpan(0, 0, 1));
yield return consumeResult;
if (consumeResult != null)
{
Debug.Log($"Key: {consumeResult.Message.Key}\nValue: {consumeResult.Value}");
Debug.Log(consumeResult.Value.Schema);
Debug.Log(consumeResult.Value.Schema["favorite_number"].GetProperty("favorite_number"));
Debug.Log(consumeResult.Value.Schema["favorite_number"]);
Avro.Field f;
consumeResult.Value.Schema.TryGetField("favorite_number", out f);
Debug.Log(consumeResult.Value["favorite_number"]);
Debug.Log(consumeResult.Value["name"]);
}
else
{
Debug.Log("consumer Result is null");
}
yield return new WaitForSeconds(1);
}
}
}
}
当我不发送任何消息时,它工作正常。但是当我从另一个生产者那里发送消息时,消费者被绞死了,什么也没发生。我调试了代码,发现它挂在了这一行:
ConsumeResult<string, GenericRecord> consumeResult = consumer.Consume(new TimeSpan(0, 0, 1));
我是Kafka的新手,也许我有任何配置问题或其他问题。