Spring Boot 应用程序可以同时处理多个请求吗? [英] Can Spring Boot application handle multiple requests simultaneously?

查看:132
本文介绍了Spring Boot 应用程序可以同时处理多个请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

从我在 Spring-Boot:同时处理多个请求中读到的内容和 如何在 Spring Boot 中使用线程安全控制器,似乎 Spring Boot 可以并发处理请求,而控制器是线程安全的.

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

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

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?

推荐答案

是的,Spring boot 可以同时处理请求!如果您的 servlet 容器是底层的 tomcat,它可以同时处理 200 个请求.

Yes, Spring boot can handle simultaneously requests! If your servlet container is tomcat under the hood, it can handle 200 simultaneous requests.

下面的代码片段强调了这一点,但请参考 原始spring boot源码

Below snippet highlights the point, but refer original spring boot source

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
  public static class Tomcat {
     public static class Threads {
       private int max = 200; // Maximum amount of worker threads
     }
  }
}

但是,您可以通过将 server.tomcat.threads.max 添加到您的 application.propertiesapplication.yml 来覆盖此值.

However, you can override this value by adding server.tomcat.threads.max to your application.properties or application.yml.

Spring 将管理一个连接池并处理实体管理器的分配(根据您在属性中指定的最小和最大连接数).我相信您可以在这里阅读更多相关信息:连接什么时候用 Spring JPA (Hibernate) Entity Manager 返回到连接池?

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 应用程序可以同时处理多个请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆