【发布时间】:2021-05-10 20:54:43
【问题描述】:
TL;DR:发送 GET 请求以从 Pub/Sub 服务器进行拉取订阅的正确方法是什么。使用的正确 URL 是什么?
我正在使用 gcloud beta emulators pubsub start 运行本地 Google Pub/Sub 伪造,我已经能够使用我编写的 ruby 脚本成功发布到它,但是我无法进行请求订阅。
我的目标是仅使用 GET 请求而不是脚本来完成此操作。我发现的问题之一是documentation 中的tons 与客户端或pull subscriptions 或gcloud 一起使用,但很少通过URL 访问服务器。也许我误解了什么是可能的 - 但我想使用 ruby 客户端将消息发布到 pub/sub,然后使用 Postman 向 pub/sub 服务器发出 GET 请求以检索消息。
我很确定问题在于我如何发出获取请求,但我已经复制了下面的所有其他内容
Ruby 发布代码
require "google/cloud/pubsub"
require 'json'
class Publisher
def publish(event)
puts "1==============>>>>>>>> publishing..."
pubsub = Google::Cloud::PubSub.new(
project_id: "grpc-demo-proj",
emulator_host: "localhost:8085"
)
topic_id = "event_topic"
topic = pubsub.topic topic_id
begin
topic.publish_async "receive_event#event",
event: JSON.generate(event) do |result|
raise "Failed to publish the message." unless result.succeeded?
puts "2==============>>>>>>>> Message published asynchronously."
end
# Stop the async_publisher to send all queued messages immediately.
topic.async_publisher.stop.wait!
rescue StandardError => e
puts "3==============>>>>>>>> Received error while publishing: #{e.message}"
end
end
end
这似乎有效,因为我得到了
1==============>>>>>>>> publishing...
DEBUG GRPC : calling localhost:8085:/google.pubsub.v1.Publisher/GetTopic
DEBUG GRPC : calling localhost:8085:/google.pubsub.v1.Publisher/Publish
2==============>>>>>>>> Message published asynchronously.
在我的终端中。
我还使用以下 shell 脚本运行 Pub/Sub 服务器。
#!/bin/bash
# Kill the existing process if it's already running
if [ "$(lsof -i:8085)" ]; then
kill $(lsof -t -i:8085)
fi
# Kick off the new process
gcloud beta emulators pubsub start --project=grpc-demo-proj
# Connect to environment variables
$(gcloud beta emulators pubsub env-init)
PubSub 设置脚本
#!/bin/bash
# Wait for the pubsub emulator to boot up
sleep 7
while [[ ! "$(lsof -i:8085)" ]]
do
echo '#===> PUBSUB EMULATOR SETUP: Waiting for PubSub Emulator to start...'
sleep 3
done
# Create topics
curl --header "Content-Type: application/json" \
--request PUT \
http://localhost:8085/v1/projects/grpc-demo-proj/topics/event_topic
# Create test subscriptions for each topic
curl --header "Content-Type: application/json" \
--request PUT \
--data '{"topic": "projects/grpc-demo-proj/topics/event_topic"}' \
http://localhost:8085/v1/projects/grpc-demo-proj/subscriptions/event_topic.test_sub1
再次。这些似乎运作良好。
我遇到的麻烦... 正在使用 GET 请求(来自 PostMan,或仅在浏览器的 URL 栏中)从 pub/sub 服务器进行拉取订阅
http://localhost:8085/v1/projects/grpc-demo-proj/subscriptions/event_topic.test_sub1:pull
返回
{
"error": {
"code": 400,
"message": "Invalid [subscriptions] name: (name=projects/grpc-demo-proj/subscriptions/event_topic.test_sub1:pull)",
"status": "INVALID_ARGUMENT"
}
}
但是订阅名是有效的,如
http://localhost:8085/v1/projects/grpc-demo-proj/subscriptions/event_topic.test_sub1
返回
{
"name": "projects/grpc-demo-proj/subscriptions/event_topic.test_sub1",
"topic": "projects/grpc-demo-proj/topics/event_topic",
"pushConfig": {},
"ackDeadlineSeconds": 10,
"messageRetentionDuration": "604800s"
}
这似乎可以确认服务器正在运行,并且主题和订阅已成功创建。
虽然-不是-我正在寻找的解决方案,但我尝试在命令行中使用 gcloud:
bgc@jadzia:~$ gcloud beta pubsub subscriptions pull test_sub1
ERROR: (gcloud.beta.pubsub.subscriptions.pull) NOT_FOUND: Resource not found (resource=test_sub1).
尽管其他消息来源似乎证实此订阅确实存在。
虽然可能是 Ruby 错误地说它成功发布了消息,或者服务器出了问题。我怀疑我只是没有正确执行 GET 请求。我已经尝试了上述 GET 请求的几种变体,但不会在这里全部列出。
那么,在不使用脚本的情况下,如何从发布/订阅服务器返回消息?理想情况下,我可以将 GET 请求的 URL 插入 PostMan,但基于命令行的解决方案也可以在这里工作。
【问题讨论】:
-
诚然(道歉)只是简要地扫描了您的问题。下面是创建订阅的 REST (PUT) 方法:cloud.google.com/pubsub/docs/reference/rest/v1/…
-
这里是 Goggle 的(推荐的)用于 Pub/Sub 的 Ruby SDK:cloud.google.com/pubsub/docs/…
-
我已经创建了一个订阅,我无法尝试从一个订阅中获取消息。我也不打算为此使用 Ruby SDK,我只是将它用于发布。
-
也就是说,您的第一个链接中的差异页面看起来很有希望:cloud.google.com/pubsub/docs/reference/rest/v1/… - 我尝试了它提到的 HTTP 请求。它与我在问题中尝试的 URL 相同,但作为 POST 请求,而不是 GET 请求。不幸的是,它仍然不起作用,给出 404 和“订阅不存在”,即使再次在设置脚本中运行 curl 命令以再次创建订阅也会给出“ALREADY_EXISTS:订阅已存在”。所以它不存在,但它也存在。薛定谔的 Pub/Sub 订阅?
标签: get postman gcloud publish-subscribe google-cloud-pubsub