主题在 PubSub 模拟器中未创建

huangapple go评论107阅读模式
英文:

Topic not created in PubSub Emulator

问题

我正在尝试使用Google PubSub Java客户端库与Google PubSub仿真器。

我正在将PubSub仿真器作为Docker容器运行。

我可以使用类似以下URL从本地计算机访问仿真器:

http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/topics

http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/subscriptions

已经使用以下命令创建了一个主题:

http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/topics/<DUMMY_TOPIC_NAME>

使用以下命令创建了一个订阅:

http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/subscriptions/<DUMMY_SUBSCRIPTION_NAME>

可以使用以下命令推送消息:

http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/topics/<DUMMY_TOPIC_NAME>:publish

可以使用以下命令拉取消息:

http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/subscriptions/<DUMMY_SUBSCRIPTION_NAME>:pull

Java应用程序也作为单独的Docker容器运行,并设置了以下环境变量...

  1. PUBSUB_EMULATOR_HOST --- localhost:32805
  2. PUBSUB_PROJECT_ID --- <DUMMY_PROJECT_ID>;

以下是代码...

  1. String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
  2. ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
  3. try {
  4. TransportChannelProvider channelProvider = FixedTransportChannelProvider
  5. .create(GrpcTransportChannel.create(channel));
  6. CredentialsProvider credentialsProvider = NoCredentialsProvider.create();
  7. // 在创建`TopicAdminClient`时设置通道和凭据提供程序。
  8. // 同样适用于`SubscriptionAdminClient`
  9. TopicAdminClient topicClient = TopicAdminClient.create(TopicAdminSettings.newBuilder()
  10. .setTransportChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build());
  11. TopicName topicName = TopicName.of(projectID, "niks-test-01");
  12. topicClient.createTopic(topicName);
  13. // 在创建`Publisher`时设置通道和凭据提供程序。
  14. // 同样适用于`Subscriber`
  15. Publisher publisher = Publisher.newBuilder(topicName).setChannelProvider(channelProvider)
  16. .setCredentialsProvider(credentialsProvider).build();
  17. } catch (Exception e) {
  18. System.out.println(e.getMessage());
  19. } finally {
  20. channel.shutdown();
  21. }

应用程序已启动,上述代码也成功执行,但当我使用http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/topics查询主题时,什么都没有返回...

在打印了很多日志后,我发现应用程序卡在了topicClient.createTopic(topicName);,没有进一步的日志或异常...

知道这是什么问题吗... 谢谢...

编辑#1:发现这更像是一个Docker问题而不是仿真器问题... 容器无法使用localhost进行通信...

英文:

I an trying Google PubSub Java Client library with Google PubSub Emulator.

I am running the PubSub Emulator as a docker container.

I can access the Emulator from my local machine using url like

http://localhost:32805/v1/projects/&lt;DUMMY_PROJECT_ID&gt;/topics

http://localhost:32805/v1/projects/&lt;DUMMY_PROJECT_ID&gt;/subscriptions

Have created a topic using

http://localhost:32805/v1/projects/&lt;DUMMY_PROJECT_ID&gt;/topics/&lt;DUMMY_TOPIC_NAME&gt;

a subscription using

http://localhost:32805/v1/projects/&lt;DUMMY_PROJECT_ID&gt;/subscriptions/&lt;DUMMY_SUBSCRIPTION_NAME&gt;

can push msgs using

http://localhost:32805/v1/projects/&lt;DUMMY_PROJECT_ID&gt;/topics/&lt;DUMMY_TOPIC_NAME&gt;:publish

can pull msgs using

http://localhost:32805/v1/projects/&lt;DUMMY_PROJECT_ID&gt;/subscriptions/&lt;DUMMY_SUBSCRIPTION_NAME&gt;:pull

Java application is also running as a separate docker container and has following env vars set...

  1. PUBSUB_EMULATOR_HOST --- localhost:32805
  2. PUBSUB_PROJECT_ID --- &lt;DUMMY_PROJECT_ID&gt;

and the following code...

  1. String hostport = System.getenv(&quot;PUBSUB_EMULATOR_HOST&quot;);
  2. ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
  3. try {
  4. TransportChannelProvider channelProvider = FixedTransportChannelProvider
  5. .create(GrpcTransportChannel.create(channel));
  6. CredentialsProvider credentialsProvider = NoCredentialsProvider.create();
  7. // Set the channel and credentials provider when creating a `TopicAdminClient`.
  8. // Similarly for SubscriptionAdminClient
  9. TopicAdminClient topicClient = TopicAdminClient.create(TopicAdminSettings.newBuilder()
  10. .setTransportChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build());
  11. TopicName topicName = TopicName.of(projectID, &quot;niks-test-01&quot;);
  12. topicClient.createTopic(topicName);
  13. // Set the channel and credentials provider when creating a `Publisher`.
  14. // Similarly for Subscriber
  15. Publisher publisher = Publisher.newBuilder(topicName).setChannelProvider(channelProvider)
  16. .setCredentialsProvider(credentialsProvider).build();
  17. } catch (Exception e) {
  18. System.out.println(e.getMessage());
  19. } finally {
  20. channel.shutdown();
  21. }

Application is up and the above code also executes successfully but when I query the topic
using http://localhost:32805/v1/projects/&lt;DUMMY_PROJECT_ID&gt;/topics it returns nothing...

After printing lot of logs I see that app is stuck @ topicClient.createTopic(topicName); no further logs or exceptions...

Any idea whats wrong here... Thanks...

Edit#1: Found that it is less of an emulator issue but more of a docker issue... Containers r not able to talk using localhost...

答案1

得分: 1

在我的初步调查中发现,这不是一个应用程序问题,而更多是一个Docker问题。

在进一步调查中,我发现容器可能没有使用相同的网络。

所以我创建了一个网络:

docker network create test-network

然后:

docker run --network=test-network -itd --name=pubsub-emulator &lt;IMAGE_NAME&gt;

docker run --network=test-network -itd --name=pubsub-app &lt;IMAGE_NAME&gt; -e PUBSUB_EMULATOR_HOST=pubsub-emulator:&lt;INTERNAL_PORT&gt;

现在应用程序能够与pubsub-emulator通信...

英文:

As found during my initial investigation, it was not an application issue but more of a docker issue.

During further investigation I found that containers might not be using same network.

So I created a network with

docker network create test-network

and then

docker run --network=test-network -itd --name=pubsub-emulator &lt;IMAGE_NAME&gt;

&

docker run --network=test-network -itd --name=pubsub-app &lt;IMAGE_NAME&gt; -e PUBSUB_EMULATOR_HOST=pubsub-emulator:&lt;INTERNAL_PORT

Now the app is able to communicate with the pubsub-emulator...

huangapple
  • 本文由 发表于 2020年7月25日 01:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63079079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定