我正在与 Google Docs 争论如何使用 PubSub 模拟器设置 Cloud PubSub with .NET。
https://cloud.google.com/dotnet/docs/getting-started/using-pub-sub
https://cloud.google.com/pubsub/docs/publisher
https://cloud.google.com/pubsub/docs/emulator
我有 Rails 背景,我的任务是为 .NET 产品实现 Cloud PubSub,在 .NET Core 上运行我们的谷歌云,使其能够发布。
Google::Cloud::Pubsub.new(project: project_id, emulator_host: emulator_host)
根据使用 .NET 的文档,我不断回到以下内容:
PublisherServiceApiClient publisherClient = PublisherServiceApiClient.Create();
PublisherClient publisher = PublisherClient.Create(...)
但是,文档中使用的库 Google.Cloud.PubSub.V1 -Pre
不包含定义。
“PublisherClient”不包含“Create”的定义
。
相反,我得到了包含 TopicName
、PublisherClient.ClientCreationSettings
和 PublisherClient.Settings
的 CreateAsync
。
我注意到 PublisherServiceApiClient
可以接受一个 Channel
,但我对如何实现这一点感到困惑。
最后提出一个实际问题,目前如何在云中使用 .NET 实现 Cloud PubSub,然后在本地使用模拟器实现?除此之外,我是否使用了错误的库或错误的文档?
如有任何建议、指示或建议,我们将不胜感激。
最佳答案
我管理了一个令我满意的解决方案。
我没有使用 PublisherClient
,而是单独使用了 PublisherServiceApiClient
。
emulatorAddr = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
if (emulatorAddr != null)
{
channel = new Channel(emulatorAddr, ChannelCredentials.Insecure);
pub = PublisherServiceApiClient.Create(channel);
}
else
{
pub = PublisherServiceApiClient.Create();
}
这意味着发布比向 PublisherClient
发送字符串稍微复杂一些,但总体来说还不错。
PubsubMessage msg = new PubsubMessage
{
Data = ByteString.CopyFromUtf8(JsonConvert.SerializeObject(payload))
};
pub.PublishAsync(topic, new[]{ msg });
如果项目在 Google Compute Engine 中运行,它将具有默认凭据。否则,无论您是在本地还是在 docker 中运行模拟器,您都可以定义 PUBSUB_EMULATOR_HOST
。
真正有用的是这个 https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/index.html
关于c# - 使用 GCloud 模拟器的 Google Cloud PubSub V1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52724976/