如何将 Pubsub 模拟器与 google-cloud-pubsub 版本 0.28 的 PublisherClient 一起使用

How to use Pubsub Emulator with PublisherClient from google-cloud-pubsub version 0.28

我刚刚更新到 google-cloud-pubsub 版本 0.28,其中 Google 将 Python 客户端重组为 PublisherClient 和 SubscriberClient。我试图连接到本地主机上的 Pubsub 模拟器,但尽管设置了环境变量,它仍然连接到云 pubsub 服务,它可以列出来自云 pubsub 的所有主题,但不是来自模拟器的主题。

这是我用来连接模拟器的代码

from google.cloud import pubsub
import os
os.environ['PUBSUB_EMULATOR_HOST'] = 'localhost:8085'
os.environ['GOOGLE_CLOUD_PROJECT'] = 'my-project'
publisher = pubsub.PublisherClient()
topic = 'projects/my-project/topics/abc'
publisher.get_topic(topic)

这里是个例外:

Traceback (most recent call last):
  File "<input>", line 5, in <module>
  File "/Users/sx5640/Envs/uie/lib/python2.7/site-packages/google/cloud/pubsub_v1/_gapic.py", line 42, in <lambda>
    fx = lambda self, *a, **kw: wrapped_fx(self.api, *a, **kw)
  File "/Users/sx5640/Envs/uie/lib/python2.7/site-packages/google/cloud/gapic/pubsub/v1/publisher_client.py", line 341, in get_topic
    return self._get_topic(request, options)
  File "/Users/sx5640/Envs/uie/lib/python2.7/site-packages/google/gax/api_callable.py", line 452, in inner
    return api_caller(api_call, this_settings, request)
  File "/Users/sx5640/Envs/uie/lib/python2.7/site-packages/google/gax/api_callable.py", line 438, in base_caller
    return api_call(*args)
  File "/Users/sx5640/Envs/uie/lib/python2.7/site-packages/google/gax/api_callable.py", line 376, in inner
    return a_func(*args, **kwargs)
  File "/Users/sx5640/Envs/uie/lib/python2.7/site-packages/google/gax/retry.py", line 127, in inner
    ' classified as transient', exception)
RetryError: RetryError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.NOT_FOUND, Resource not found (resource=abc).)>)

该问题已在 0.29 版中修复