Java 单个 servlet 如何处理来自客户端的多个请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27765547/
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-08-11 04:58:29  来源:igfitidea点击:

How does a single servlet handle multiple requests from client side

javamultithreadingservlets

提问by shivam-shekhar

How does a single servlet handle multiple client requests coming in the form of user requests ? Based on the singleton design pattern I know we get a single instance of servlet created , but how does a single servlet handle millions of requests . Confused about the threading involved in it also.

单个 servlet 如何处理以用户请求形式出现的多个客户端请求?根据单例设计模式,我知道我们创建了一个 servlet 实例,但是单个 servlet 如何处理数百万个请求。也对其中涉及的线程感到困惑。

Also does any browser specifications or settings come handy out here for sending the requests across or generating the threads sent out for the requests.

还有任何浏览器规范或设置在这里很方便,用于发送请求或生成为请求发送的线程。

Is it the same for all frameworks or it differs say for example struts v/s springs ?

它对所有框架都相同还是不同,例如 struts v/s springs ?

采纳答案by shazin

Struts/Spring frameworks are actually written on top of Servlet specification so doesn't matter what you use underneath it use Servlets.

Struts/Spring 框架实际上是在 Servlet 规范之上编写的,因此在它下面使用 Servlet 并不重要。

You are right, Only single instance of Servlet is created, but that instance is shared across multiple threads. For this reason you should never have shared mutable states in your Servlets.

您是对的,只创建了 Servlet 的单个实例,但该实例在多个线程之间共享。出于这个原因,您永远不应该在 Servlet 中共享可变状态。

For example you have following servlet mapped to http://localhost/myservlet

例如,您有以下 servlet 映射到 http://localhost/myservlet

class MySerlvet extends HttpServlet {

     public void doGet(HttpServletRequest req, HttpServletResponse res) {
          // Get Logic
     }    
}

The Web Server will have something similar (Not necessarily same) in its code.

Web 服务器将在其代码中具有类似(不一定相同)的内容。

MyServlet m = new MyServlet(); // This will be created once

// for each request for http://localhost/myservlet
executorService.submit(new RequestProcessingThread(m));

回答by Ali Sepehri.Kh

Each request is processed in a separated thread. This doesn't mean Tomcat creates a new thread per request. There is a pool of threads to process requests. Also there is a single instance for each servlet and this is the default case.(Some more information). Your servlet should be Thread Safei.e. it should be stateless.

每个请求都在一个单独的线程中处理。这并不意味着 Tomcat 会为每个请求创建一个新线程。有一个线程池来处理请求。每个 servlet 也有一个实例,这是默认情况。(更多信息)。您的 servlet 应该是Thread Safe即它应该是无状态的。

enter image description here

在此处输入图片说明

If your servlet implements SingleThreadModelinterface, each thread uses separate instance of servlet. SingleThreadModelis deprecated, Don't use it.

如果您的 servlet 实现了SingleThreadModel接口,则每个线程使用单独的 servlet 实例。SingleThreadModel已弃用,请勿使用。

SingleThreadModel

单线程模型

I made this answer as community wiki.

我将这个答案作为社区维基。

回答by siddhartha

You don't create multiple instances of servlet. The servlet engine utilizes a separate thread from the thread pool for each request (up to some max number of Threads allocated).

您不会创建 servlet 的多个实例。servlet 引擎为每个请求使用来自线程池的单独线程(最多分配一些最大线程数)。

The performance is relative to the number of threads, not the number of instances of the servlet.

性能与线程数有关,与 servlet 的实例数无关。

For example, if there are 1000 requests, and the maximum threads that can be generated by servlet is 100, then there will be performance degradation.

比如有1000个请求,servlet最多可以产生100个线程,那么性能就会下降。

In order to avoid this problem, we can use load balancerby putting multiple servers behind a load balancer. The load balancer should be configured to "route" the requests to any one of the servers based upon different parameters/settings (round robin distribution, load distribution etc.). The more load you need, the more servers you should add. However, this does send all traffic through the load balancer so it is important that this be redundant and failover safe.

为了避免这个问题,我们可以通过将多个服务器放在一个负载均衡器后面来使用负载均衡器。负载平衡器应配置为根据不同的参数/设置(循环分配、负载分配等)将请求“路由”到任何一台服务器。您需要的负载越多,您应该添加的服务器越多。但是,这确实通过负载平衡器发送所有流量,因此重要的是这是冗余和故障转移安全。