multithreading Spring Boot 应用程序可以同时处理多个请求吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46893237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:33:48  来源:igfitidea点击:

Can Spring Boot application handle multiple requests simultaneously?

multithreadingrestspring-bootspring-data-jpaamazon-elastic-beanstalk

提问by ddd

I am developing Rest APIs with Spring Boot which is deployed on AWS Beanstalk. Potentially, the service will be getting hits from thousands of clients every day. Therefore I would like to understand capability of Spring Boot of handling multiple requests.

我正在使用部署在 AWS Beanstalk 上的 Spring Boot 开发 Rest API。潜在地,该服务每天都会受到数以千计的客户的点击。因此,我想了解 Spring Boot 处理多个请求的能力。

From what I read in Spring-Boot: Handle multiple requests concurrentlyand How to have thread safe controller in spring boot, it seems Spring Boot can handle requests concurrently while controller being thread safe.

从我在Spring-Boot: Handle multiple requests concurrentlyHow to have thread safe controller in spring boot 中读到的内容来看,似乎 Spring Boot 可以同时处理请求,而控制器是线程安全的。

If two requests are made to the same endpoint for updates at the same time though, does the controller deal with the requests one after another or simultaneously with two threads in parallel? If latter, does each thread has its own entity manager? Is there a way to implement a thread pool to limit the number of threads based on the capacity of the EC2 instance? By the way, how do I decide how big of an instance should I start with based on the estimated volumn of requests?

如果同时向同一个端点发出两个更新请求,控制器是一个接一个地处理请求还是同时并行处理两个线程?如果是后者,是否每个线程都有自己的实体管理器?有没有办法实现一个线程池来根据EC2实例的容量限制线程数?顺便说一下,我如何根据估计的请求量来决定我应该从多大的实例开始?

回答by Felipe Mariano

Yes, Spring boot can handle simultaneously requests! You can limit the number of concurrent requests by adding server.tomcat.max-threadsto your application.propertiesor application.yml. Spring will manage a pool of connections and handle the distribution of entity managers (according to the minimum and maximum of connections you specify in your properties). I believe you can read more about it here: When are connections returned to the connection pool with Spring JPA (Hibernate) Entity Manager?

是的,Spring boot 可以同时处理请求!您可以通过添加server.tomcat.max-threads到您的application.properties或来限制并发请求的数量application.yml。Spring 将管理一个连接池并处理实体管理器的分布(根据您在属性中指定的最小和最大连接数)。我相信您可以在这里阅读更多相关信息:何时使用 Spring JPA (Hibernate) Entity Manager 将连接返回到连接池?

回答by LHCHIN

If you are developing web applications with Spring Boot (I mean that you have included the dependency of spring-boot-starter-webinto your pom file), Spring will automatically embed web container (Tomcat by default) and it can handle requests simultaneously just like common web containers.

如果您正在使用 Spring Boot 开发 Web 应用程序(我的意思是您已将 的依赖项包含spring-boot-starter-web到您的 pom 文件中),Spring 将自动嵌入 Web 容器(默认为 Tomcat)并且它可以像普通 Web 容器一样同时处理请求。

And you can also change the default web container from Tomcat to Undertow or Jetty by modifying the dependencies.

并且您还可以通过修改依赖项将默认 Web 容器从 Tomcat 更改为 Undertow 或 Jetty。

As @Felipe Mariano said, you can restrict the maximum amount of worker threads for different web container in your configuration file below.
(1) For Tomcat: server.tomcat.max-threads
(2) For Undertow: server.undertow.worker-threads
(3) For Jetty: server.jetty.acceptors

正如@Felipe Mariano 所说,您可以在下面的配置文件中限制不同 Web 容器的最大工作线程数。
(1) 对于 Tomcat:server.tomcat.max-threads
(2) 对于 Undertow:server.undertow.worker-threads
(3) 对于 Jetty:server.jetty.acceptors

回答by Maiden

There is a servlet container created for each request if I am right and is processed by each separate new spawned Thread so, technically each request is processed in paralleled. You need to configure the max threads in app properties file . Based upon your configurations thread pool will be taken care by Spring Framework . Thanks

如果我是对的,将为每个请求创建一个 servlet 容器,并由每个单独的新生成的线程处理,因此,从技术上讲,每个请求都是并行处理的。您需要在应用程序属性文件中配置最大线程数。Spring Framework 将根据您的配置处理线程池。谢谢