如何解决无法从本地虚假 PubSub 服务器执行拉取订阅的 GET 请求
TL;DR:从 Pub/Sub 服务器发送 GET 请求以执行拉取订阅的正确方法是什么。要使用的正确网址是什么?
我正在使用 gcloud beta emulators pubsub start
运行本地 Google Pub/Sub 伪造,我已经能够使用我编写的 ruby 脚本成功发布到它,但是我无法进行拉订阅。
我的目标是仅使用 GET 请求而不是脚本来完成此操作。我发现的问题之一是在对客户端或 tons 执行 documentation 时有 pull subscriptions 个 gcloud,但很少涉及如何通过 URL 访问服务器。也许我误解了什么是可能的 - 但我想使用 ruby 客户端将消息发布到发布/订阅,然后使用 Postman 向发布/订阅服务器发出 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)
发布订阅设置脚本
#!/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 栏中)从发布/订阅服务器执行拉取订阅
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,但基于命令行的解决方案也可以在这里工作。
解决方法
我确实使用您发布的所有脚本复制了您本地的假发布/订阅服务器。正如您评论的那样,我使用 POST 而不是 GET 并得到了响应。 Pub/Sub subscriptions pull reference
POST https://pubsub.googleapis.com/v1/{subscription}:pull
订阅请求的 POST 请求:
curl --header "Content-Type: application/json" \
--request POST \
--data "{
"maxMessages": "1"
}" \
http://localhost:8085/v1/projects/my-project/subscriptions/event_topic.test_sub1:pull
Pubsub Message 以 base 64 编码。请注意,我在 Google Cloud Shell 中运行了所有内容(创建发布订阅服务器、主题、订阅者、发布消息、提取消息)。
编辑 1:
正如 Brian 所说,这是对他有用的请求。这也适用于我的测试!
curl --header "Content-Type: application/json" \
--request POST \
localhost:8085/v1/projects/my-prject/subscriptions/event_topic.test_sub1:pull?maxMessages=5
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。