I faced the problem with the following code:
public IActionResult Consume(string topic)
{
try
{
var config = new ConsumerConfig
{
BootstrapServers = "kafka-01:9092,kafka-02:9092,kafka-03:9092",
SecurityProtocol = SecurityProtocol.SaslPlaintext,
SaslMechanism = SaslMechanism.ScramSha256,
SaslUsername = "my-consumer",
SaslPassword = "pass",
GroupId = "my-test-group"
};
using IConsumer<Null, string> consumer = new ConsumerBuilder<Null, string>(config).Build();
consumer.Subscribe(topic);
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(60));
ConsumeResult<Null, string> result = consumer.Consume(cts.Token);
consumer.Close();
return Ok(result.Message.Value);
}
catch (OperationCanceledException cancelEx)
{
return NoContent();
}
catch (ConsumeException consumeEx)
{
return BadRequest(consumeEx.ToString());
}
}
But consumer doesn't work! As I see in Debug, it freezes until cancelationToken expires, throws an OperationCancelledException, and i receive no result.
The producer seems to work, it doesn't freeze or return error (using the similar config).
Why is doesn't work?