主题在 PubSub 模拟器中未创建

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

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容器运行,并设置了以下环境变量...

PUBSUB_EMULATOR_HOST --- localhost:32805

PUBSUB_PROJECT_ID --- <DUMMY_PROJECT_ID>;

以下是代码...

        String hostport = System.getenv("PUBSUB_EMULATOR_HOST");

        ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();

        try {
            TransportChannelProvider channelProvider = FixedTransportChannelProvider
                    .create(GrpcTransportChannel.create(channel));

            CredentialsProvider credentialsProvider = NoCredentialsProvider.create();

            // 在创建`TopicAdminClient`时设置通道和凭据提供程序。
            // 同样适用于`SubscriptionAdminClient`
            TopicAdminClient topicClient = TopicAdminClient.create(TopicAdminSettings.newBuilder()
                    .setTransportChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build());

            TopicName topicName = TopicName.of(projectID, "niks-test-01");

            topicClient.createTopic(topicName);

            // 在创建`Publisher`时设置通道和凭据提供程序。
            // 同样适用于`Subscriber`
            Publisher publisher = Publisher.newBuilder(topicName).setChannelProvider(channelProvider)
                    .setCredentialsProvider(credentialsProvider).build();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            channel.shutdown();
        }

应用程序已启动,上述代码也成功执行,但当我使用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...

PUBSUB_EMULATOR_HOST --- localhost:32805

PUBSUB_PROJECT_ID --- &lt;DUMMY_PROJECT_ID&gt;

and the following code...

    String hostport = System.getenv(&quot;PUBSUB_EMULATOR_HOST&quot;);

    ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();

    try {
        TransportChannelProvider channelProvider = FixedTransportChannelProvider
                .create(GrpcTransportChannel.create(channel));

        CredentialsProvider credentialsProvider = NoCredentialsProvider.create();

        // Set the channel and credentials provider when creating a `TopicAdminClient`.
        // Similarly for SubscriptionAdminClient
        TopicAdminClient topicClient = TopicAdminClient.create(TopicAdminSettings.newBuilder()
                .setTransportChannelProvider(channelProvider).setCredentialsProvider(credentialsProvider).build());

        TopicName topicName = TopicName.of(projectID, &quot;niks-test-01&quot;);

        topicClient.createTopic(topicName);

        // Set the channel and credentials provider when creating a `Publisher`.
        // Similarly for Subscriber
        Publisher publisher = Publisher.newBuilder(topicName).setChannelProvider(channelProvider)
                .setCredentialsProvider(credentialsProvider).build();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        channel.shutdown();
    }

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:

确定