How Rest Controller handle multiple request at same time for a single instance application?

末鹿安然 提交于 2021-02-18 08:13:30

问题


  1. If multiple request are hit to a single RestController at the same time in a application, how it is handle for different scenarios (Multiple request to a single endpoints (only GET), or Multiple requests for multiple endpoints(GET, POST, PUT...))
  2. Is multi-threading concept utilized? If yes is it possible to handle the requests in FIFO pattern?
  3. What is the maximum request a RestController can take ?
  4. Does RestController scope affect handling of requests (behavior of request scope with default scope-singleton) ?
  5. Also how it is handle by Application context (example with flow will be helpful)

Considering building Micro-services with Spring Boot 2.


回答1:


From the point of view of Spring (Application Context) rest controller is a singleton if not specified otherwise.

So the code of controller must be ready to be invoked by multiple threads simultaneously.

When a new request reaches the server, in a tradition thread-per-request model the web server (like tomcat) is responsible to allocate a thread from the predefined pool of threads to the request. Then the request gets processed by controller in the context of this thread.

The actual thread pool implementation can in general vary from server to server, but in general, its something that can be configured (number of threads per loop, queue size to store requests for future processing if the pool is full, etc.)

Now regarding the Scope of RestController. If the controller is stateless (and it should be for many cases, just keep it singleton). If you need the new Instance of controller to be created per request, than change the scope. Obviously each thread will have to use the same (in case of singleton scope) instance of rest controller or spring mvc will create a new instance of controller if you specify another scope.

All the answer above applies to a "traditional" thread-per-request model.

Note that since spring 5 / spring boot 2 spring also supports "Reactive" model with a webflux. It works on top of netty and doesn't utilize a thread-per-request model. Please specify in the question if you're interested in this model rather than a tradition model that I've tried to briefly describe.



来源:https://stackoverflow.com/questions/56311512/how-rest-controller-handle-multiple-request-at-same-time-for-a-single-instance-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!