GCloud pubsub 模拟器不遵守 "PUBSUB_EMULATOR_HOST" 环境变量

GCloud pubsub emulator doesn't respect "PUBSUB_EMULATOR_HOST" environment variable

我在本地尝试 运行 pubsub 模拟器,并使用我在 pubsub 上 运行 现有的服务向它发送消息。没有收到消息,我得到的只是日志中的身份验证错误。

[pubsub] Jan 22, 2017 3:43:16 PM com.google.cloud.iam.testing.v1.shared.authorization.AuthInterceptor interceptCall
[pubsub] INFO: Authentication interceptor: Header value is null
[pubsub] Jan 22, 2017 3:43:16 PM com.google.cloud.iam.testing.v1.shared.authorization.AuthInterceptor interceptCall
[pubsub] INFO: Authentication interceptor: invalid or missing token

我正在请求从 dotnet 和 nodejs 发布和拉取。

C#

var creds = GoogleCredential.GetApplicationDefaultAsync().Result;
if (creds.IsCreateScopedRequired) {
    creds = creds.CreateScoped(new [] { PubsubService.Scope.Pubsub } );
}

return new PubsubService(
    new BaseClientService.Initializer() {
        HttpClientInitializer = creds,
        ApplicationName = "My Wonderful App"
    }
);

Node.js

var pubsub = require('@google-cloud/pubsub');

var pubsubClient = pubsub({
  projectId: config.get('GCLOUD_PROJECT')
});

我运行 解决了这个问题。

在研究它时,我发现了以下 post:"Nothing is going wrong. When using the emulator we pass no credentials and this is what the logs are telling you, that no authentication header was provided with any of the requests." - https://www.bountysource.com/issues/39093553-pubsub-emulator-authinterceptor-question

我的消息是在研究主题时开始发布的,所以这可能只是延迟。

header 值 null 是红色的。看起来 dotnet sdk 不像 nodejs sdk 那样尊重环境变量。我与 jskeet 通信,他创建了一个问题来添加文档以显示如何启用模拟器的使用:https://github.com/GoogleCloudPlatform/google-cloud-dotnet/issues/859

以下是我在 C# 中创建 PublisherClient 的方式

private static PublisherClient CreatePublisherClient() {
    var emulatorHostAndPort = Environment.GetEnvironmentVariable("PUBSUB_EMULATOR_HOST");
    if (String.IsNullOrWhiteSpace(emulatorHostAndPort)) {
        return PublisherClient.Create();
    } else {
        var channel = new Channel(emulatorHostAndPort, ChannelCredentials.Insecure);
        return PublisherClient.Create(channel);
    }
}