我正在尝试使用Google Cloud Pub / Sub模拟器来测试我的scala应用 . Documentation陈述以下内容 -

运行env-init命令以确定和设置模拟器使用的环境变量 . 请注意,按照这些说明操作后,代码中的Google Cloud Pub / Sub客户端库会调用在本地实例中运行的API,而不是 生产环境 API .

但是当我发布消息或创建主题时,它会在 Cloud 中执行,而不是在我的本地模拟器上执行 . 我已经确认使用 println(s"ENV: ${sys.env("PUBSUB_EMULATOR_HOST")}") 正确设置了环境变量,它打印 ENV: localhost:8085 .

我尝试强制库使用我的本地设置,如下所示

val settings = TopicAdminSettings.defaultBuilder()
  .setChannelProvider(InstantiatingChannelProvider.newBuilder()
    .setEndpoint("localhost:8085")
      .setCredentialsProvider(TopicAdminSettings.defaultCredentialsProviderBuilder().build())
    .build())
  .build()

val topicAdminClient = TopicAdminClient.create(settings)
topicAdminClient.createTopic(topicName)

这样我成功向本地设置发送请求,但导致以下错误 -

[pubsub] 6 14, 2017 2:49:11 午後 io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFO: Adding handler(s) to newly registered Channel.
[pubsub] 6 14, 2017 2:49:11 午後io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFO: Detected non-HTTP/2 connection.
[pubsub] 6 14, 2017 2:49:11 午後 io.gapi.emulators.netty.NotFoundHandler handleRequest
[pubsub] INFO: Unknown request URI: /bad-request
[pubsub] 6 14, 2017 2:49:35 午後 io.gapi.emulators.grpc.GrpcServer$3 operationComplete
[pubsub] INFO: Adding handler(s) to newly registered Channel.
[pubsub] 6 14, 2017 2:49:35 午後 io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
[pubsub] INFO: Detected non-HTTP/2 connection.
[pubsub] 6 14, 2017 2:49:35 午後 io.gapi.emulators.netty.NotFoundHandler handleRequest
[pubsub] INFO: Unknown request URI: /bad-request

我按如下方式启动了模拟器 -

PC-12041:random kakaji$ sudo gcloud beta emulators pubsub start
Password:
Executing: /usr/local/google-cloud-sdk/platform/pubsub-emulator/bin/cloud-pubsub-emulator --host=localhost --port=8085
[pubsub] This is the Google Pub/Sub fake.
[pubsub] Implementation may be incomplete or differ from the real system.
[pubsub] 6 14, 2017 2:59:21 午後 com.google.cloud.pubsub.testing.v1.Main main
[pubsub] INFO: IAM integration is disabled. IAM policy methods and ACL checks are not supported
[pubsub] 6 14, 2017 2:59:21 午後 io.grpc.internal.ManagedChannelImpl <init>
[pubsub] INFO: [ManagedChannelImpl@77b52d12] Created with target localhost:8085
[pubsub] 6 14, 2017 2:59:21 午後 io.gapi.emulators.netty.NettyUtil applyJava7LongHostnameWorkaround
[pubsub] INFO: Unable to apply Java 7 long hostname workaround.
[pubsub] 6 14, 2017 2:59:21 午後 com.google.cloud.pubsub.testing.v1.Main main
[pubsub] INFO: Server started, listening on 8085