简体   繁体   English

Java 应用中的并发 - Cloud Run

[英]Concurrency in Java applications - Cloud Run

I am using Cloud Run to run a java application and the concurrency for this cloud run service is set to 1. Whenever it receives a message through a http end point, applications get triggered runs for X amount of time and returns the result.我正在使用 Cloud Run 运行 java 应用程序,并且此云运行服务的并发设置为 1。每当它通过 http 端点接收到消息时,应用程序都会触发运行 X 时间并返回结果。

I would like to understand the below items:我想了解以下项目:

  1. What would happen if the cloud service gets one more http request and triggers the java application before the previous run is completed?如果云服务在上一次运行完成之前再次收到一个 http 请求并触发 java 应用程序,会发生什么情况?
    (For example, lets assume the java application takes 5 seconds to complete the execution and send the response back. When one is already in progress, it receives one more http request at 3rd second and triggers the java application) (例如,假设 java 应用程序需要 5 秒来完成执行并发回响应。当一个已经在进行时,它会在第 3 秒接收到另一个 http 请求并触发 java 应用程序)
  2. Does it cause any failure in the expected result?它会导致预期结果失败吗?

Thank you谢谢

I will start by answering the second question:我将从回答第二个问题开始:

Given you already have set concurrency ( Maximum requests per container in the console) to 1.鉴于您已经将concurrency (控制台中Maximum requests per container )设置为 1。

the concurrency for this cloud run service is set to 1.此云运行服务的并发设置为 1。

You will get an error code 429 for the new request, only if you have set max-instances ( Maximum number of instances in the console).仅当您设置了max-instances (控制台中的Maximum number of instances )时,您才会收到新请求的错误代码 429。

Ref: https://cloud.google.com/run/docs/troubleshooting#429参考: https://cloud.google.com/run/docs/troubleshooting#429

Otherwise and back to your first question: if max-instances > 1 a new Cloud Run instance will be created and the second request will suffer the container/application startup time.否则,回到您的第一个问题:如果max-instances > 1,将创建一个新的 Cloud Run 实例,并且第二个请求将受到容器/应用程序启动时间的影响。

This being said, if you are using Cloud Run, you are using a webserver like springboot and/or tomcat behind.话虽这么说,如果您使用的是 Cloud Run,那么您正在使用像 springboot 和/或 tomcat 这样的网络服务器。 Such server will be setup to handle concurrent requests with a thread-per-connection model and a default value of 200 for server.tomcat.max-threads .这样的服务器将设置为处理并发请求,每个连接的线程数为 model, server.tomcat.max-threads的默认值为 200。 So why not profit from this model, and increase concurrency , if your request are not dealing with shared resources.那么,如果您的请求不处理共享资源,为什么不从这个 model 中获利,并增加concurrency Of course try to use non-blocking reactive programming model to reduce the number of threads and their memory footprint.当然尝试使用非阻塞反应式编程 model 来减少线程数量及其 memory 占用空间。

But if, for your requirement, you need to keep concurrency at 1, in this case Cloud Function will be a better choice , for these reasons:但是,如果根据您的要求,您需要将并发保持在 1,在这种情况下,Cloud Function 将是更好的选择,原因如下:

  • You don't have to setup a webserver您不必设置网络服务器
  • Less memory, and less CPU requirement(no webserver, no multi threading)更少的 memory,更少的 CPU 需求(没有网络服务器,没有多线程)
  • Simpler code更简单的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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