英文:
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/<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...
答案1
得分: 1
在我的初步调查中发现,这不是一个应用程序问题,而更多是一个Docker问题。
在进一步调查中,我发现容器可能没有使用相同的网络。
所以我创建了一个网络:
docker network create test-network
然后:
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>
现在应用程序能够与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 <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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论