【问题标题】:Google Cloud Run pubsub pull listener app fails to startGoogle Cloud Run pubsub pull listener 应用无法启动
【发布时间】:2020-08-11 20:00:36
【问题描述】:

我正在 Cloud Run 上测试 pubsub“pull”订阅者,仅使用此示例 java 代码的侦听器部分(SubscribeAsyncExample...稍微修改以适合我的 SpringBoot 应用程序): https://cloud.google.com/pubsub/docs/quickstart-client-libraries#java_1 它在部署期间无法启动......但在尝试启动时,它确实从 pubsub 队列中提取项目。最初,我在不同的 pubsub 主题上有一个 HTTP“推送”接收器(@RestController),效果很好。有什么建议么?我是 Cloud Run 的新手。谢谢。

Deploying...
  Creating Revision... Cloud Run error: Container failed to start. Failed to start and then listen on the port defined
  by the PORT environment variable. Logs for this revision might contain more information....failed
Deployment failed
In logs:
2020-08-11 18:43:22.688 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4606 ms
2020-08-11T18:43:25.287759Z Listening for messages on projects/ce-cxmo-dev/subscriptions/AndySubscriptionPull:
2020-08-11T18:43:25.351650801Z Container Sandbox: Unsupported syscall setsockopt(0x18,0x29,0x31,0x3eca02dfd974,0x4,0x28). It is very likely that you can safely ignore this message and that this is not the cause of any error you might be troubleshooting. Please, refer to https://gvisor.dev/c/linux/amd64/setsockopt for more information.
2020-08-11T18:43:25.351770555Z Container Sandbox: Unsupported syscall setsockopt(0x18,0x29,0x12,0x3eca02dfd97c,0x4,0x28). It is very likely that you can safely ignore this message and that this is not the cause of any error you might be troubleshooting. Please, refer to https://gvisor.dev/c/linux/amd64/setsockopt for more information.
2020-08-11 18:43:25.680 WARN 1 --- [ault-executor-0] i.g.n.s.i.n.u.internal.MacAddressUtil : Failed to find a usable hardware address from the network interfaces; using random bytes: ae:2c:fb:e7:92:9c:2b:24
2020-08-11T18:45:36.282714Z Id: 1421389098497572
2020-08-11T18:45:36.282763Z Data: We be pub-sub'n in pull mode2!!
Nothing else after this and the app stops running.

@Component
public class AndyTopicPullRecv {
  
  public AndyTopicPullRecv() 
  {
    subscribeAsyncExample("ce-cxmo-dev", "AndySubscriptionPull");
  }
  
  public static void subscribeAsyncExample(String projectId, String subscriptionId) {
    ProjectSubscriptionName subscriptionName =
        ProjectSubscriptionName.of(projectId, subscriptionId);

    // Instantiate an asynchronous message receiver.
    MessageReceiver receiver =
        (PubsubMessage message, AckReplyConsumer consumer) -> {
          // Handle incoming message, then ack the received message.
          System.out.println("Id: " + message.getMessageId());
          System.out.println("Data: " + message.getData().toStringUtf8());
          consumer.ack();
        };

    Subscriber subscriber = null;
    try {
      subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
      // Start the subscriber.
      subscriber.startAsync().awaitRunning();
      System.out.printf("Listening for messages on %s:\n", subscriptionName.toString());
      // Allow the subscriber to run for 30s unless an unrecoverable error occurs.
      // subscriber.awaitTerminated(30, TimeUnit.SECONDS);
      subscriber.awaitTerminated();
      System.out.printf("Async subscribe terminated on %s:\n", subscriptionName.toString());
    // } catch (TimeoutException timeoutException) {
    } catch (Exception e) {
      // Shut down the subscriber after 30s. Stop receiving messages.
      subscriber.stopAsync();
      System.out.printf("Async subscriber exception: " + e); 
    }
  }
}

【问题讨论】:

  • 您的订阅者代码在记录消息数据后会做什么?它是否立即尝试确认消息?如果您能确定您的应用程序崩溃的确切位置,将会很有帮助。
  • 您是否真的在监听并响应触发应用程序的 REST 请求?您可能需要发布代码链接。
  • 谢谢,我在上面的描述中添加了代码。

标签: google-cloud-pubsub google-cloud-run


【解决方案1】:

科尔班问题很重要!!对于共享代码,我想说“不”。 Cloud Run contract 很清楚:

  • 您的服务必须响应 HTTP 请求。没有请求,您无需支付任何费用,也没有 CPU 专用于您的实例(当没有请求正在处理时,该实例就像一个守护进程)
  • 您的服务必须是无状态的(这里不是您的情况,我不会花时间讨论这个)

如果您想提取您的 PubSub 订阅,请在您的 code with a Rest controller 中创建一个端点。在处理此请求时,运行拉取机制并处理消息。

Cloud Scheduler 可以定期调用此端点以保持进程正常运行。

小心,你有a max request processing timeout at 15 minutes(今天,可能在不久的将来发生变化)。因此,您的进程运行时间不能超过 15 分钟。让它有弹性以应对失败,并将您的调度程序设置为每 15 分钟调用一次您的服务

【讨论】:

  • 是的,我还有一个 Rest Controller。它们都在工作,所以我可以从 pubsub 接收推送或拉取式消息,但应用程序会在几分钟后退出,显然是由于没有响应健康检查。但是,我接受了您的建议,并导致 pull listener 由于 REST 调用而启动,而不是在应用程序启动时启动,这似乎解决了问题。非常感谢您的帮助。
猜你喜欢
  • 2021-12-26
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 2020-10-19
  • 2019-10-27
  • 2021-12-29
  • 2019-10-05
相关资源
最近更新 更多