Apache Pulsar C#客户端调用producer.Send时触发故障求助
Apache Pulsar C#客户端调用Send报错“Producer has faulted”的原因分析与解决
你遇到的问题是调用Apache Pulsar C#客户端的producer.Send方法时抛出“Producer has faulted”异常,结合你提供的代码,主要原因和解决方法如下:
核心问题:Producer初始化未使用异步方法
你的代码中创建Producer时使用了同步的Create()方法,但整个Main方法是异步的,这会导致Producer还未完成初始化就被用于发送消息,进而触发故障状态。Pulsar C#客户端的大部分IO操作都是异步设计的,必须使用异步方法等待初始化完成。
修正后的代码
public class PulsarProducerExample { private const string Topic = "persistent://public/default/my-topic"; public static async Task Main() { // Create a Pulsar client var client = PulsarClient.Builder().ServiceUrl(new Uri("pulsar://pulsar:6650")).Build(); Console.WriteLine("Created the new client"); // 创建Producer时使用异步方法并await var producer = await client.NewProducer() .Topic(Topic) .CreateAsync(); Console.WriteLine("Created the new producer"); try { // Send messages to the topic for (int i = 0; i < 10; i++) { var message = $"Message {i}"; await producer.Send(Encoding.UTF8.GetBytes(message)); Console.WriteLine($"Sent: {message}"); } } catch (Exception ex) { Console.WriteLine($"Exception: {ex.Message}"); // 打印完整异常栈,便于排查深层问题 Console.WriteLine($"Stack Trace: {ex.StackTrace}"); } finally { // Close the producer and the client await producer.DisposeAsync(); await client.DisposeAsync(); } } }
其他可能的故障原因
- 服务地址不可达:确认
pulsar://pulsar:6650中的主机名能被客户端正确解析,比如本地测试时改用pulsar://localhost:6650,Docker环境下检查容器网络是否连通。 - 版本不兼容:确保Pulsar服务端和C#客户端的大版本一致(如服务端是2.10.x,客户端也使用2.10.x系列),版本差异过大可能导致协议不匹配。
- 权限限制:如果服务端开启了严格的权限控制,需要提前创建目标主题,或者为生产者配置对应的权限。
内容的提问来源于stack exchange,提问作者Sameer Joshi
相关产品推荐
相关产品推荐

