简体   繁体   中英

Topic not created in PubSub Emulator

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/<DUMMY_PROJECT_ID>/topics

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

Have created a topic using

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

a subscription using

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

can push msgs using

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

can pull msgs using

http://localhost:32805/v1/projects/<DUMMY_PROJECT_ID>/subscriptions/<DUMMY_SUBSCRIPTION_NAME>: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 --- <DUMMY_PROJECT_ID>

and the following code...

    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();

        // 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, "niks-test-01");

        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/<DUMMY_PROJECT_ID>/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...

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 <IMAGE_NAME>

&

docker run --network=test-network -itd --name=pubsub-app <IMAGE_NAME> -e PUBSUB_EMULATOR_HOST=pubsub-emulator:<INTERNAL_PORT

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM