Spring Boot的最大请求和线程数应设置为1(server.tomcat.max-threads=1)。

huangapple go评论61阅读模式
英文:

Spring Boot Maximum Requests and Threads should be 1 (server.tomcat.max-threads=1)

问题

背景:
我正在尝试使用Spring Webflux开发响应式应用程序,我想查看如果应用程序只有一个线程,它的行为会如何。

问题:
如何让Spring仅使用一个线程处理所有可能的请求?

到目前为止,我已经设置了 server.tomcat.max-threads=1,但我仍然看到每个请求创建了多个线程。您可以在下面看到运行中应用程序的一些日志:

Spring Boot的最大请求和线程数应设置为1(server.tomcat.max-threads=1)。

请告诉我是否有什么我需要在我的配置中添加以使其工作。

感谢您的帮助!

英文:

Background

I am experimenting with Spring Webflux, for reactive applications and I want to see the behavior of the application if I only have one thread in the application.

Question

How can I make Spring to only use a single thread for all the requests that could come?

So far I set server.tomcat.max-threads=1, however I still see that multiple threads are been created per request. You can see some of the logs of the running application below:
Spring Boot的最大请求和线程数应设置为1(server.tomcat.max-threads=1)。

Please let me know if I am missing something I should add in my configurations to make this work.

Thanks for your help!

答案1

得分: 4

WebFlux默认情况下与Tomcat无关(尽管您可以配置WebFlux以使用Tomcat/Undertow/Jetty而不是Netty)。WebFlux默认使用Netty,而Netty与Servlets几乎没有关系。

在WebFlux中,所有Web请求都通过事件循环进行处理,内部使用响应式Netty来处理Web请求。

用于处理Web请求的线程的名称如下:

reactor-http-nio-4

如果要配置事件循环使用的线程数,您可以在应用程序启动之前传递一个VM参数:

-Dreactor.netty.ioWorkerCount=1

例如,它会像这样:

java -jar application.jar -Dreactor.netty.ioWorkerCount=1

此外,您在日志中看到的“parallel-n”线程是与Flux.interval()方法一起使用的并行调度程序的线程。这些线程与用于处理Web请求的事件循环无关。

英文:

WebFlux has nothing to do with Tomcat by default (you can configure WebFlux to use Tomcat/Undertow/Jetty instead of Netty, though). WebFlux uses Netty by default which is not even close to servlets.

In WebFlux all web requests are processed by event-loop, internally it uses reactive Netty to handle the web requests.

The threads for serving the web requests have the name like this:

> reactor-http-nio-4

If you want to configure the number of threads used by event loop, you can do it passing a VM-argument before application startup:

-Dreactor.netty.ioWorkerCount=1

For example, it would look like this:

java -jar application.jar -Dreactor.netty.ioWorkerCount=1

Also: parallel-n threads you can see in your logs are the threads from parallel scheduler, which is used with Flux.interval() method. Those are not related to the event loop for serving web requests

答案2

得分: 2

以下是翻译好的部分:

"The threads you see in the logs are just scheduling threads, so they schedule work on the event loop. If you only want one event loop, do as the other answer suggested."
在日志中看到的线程只是调度线程,它们用于调度事件循环上的工作。如果你只想要一个事件循环,就像其他回答建议的那样做。

"If you on the other hand only want one scheduler you can add .onSubscribe(Schedulers.single()) on any Flux/Mono in the chain."
另一方面,如果你只想要一个调度程序,你可以在链中的任何Flux/Mono上添加.onSubscribe(Schedulers.single())

"This will during subscription to your application apply a single threaded scheduler."
这将在订阅应用程序时应用一个单线程调度程序。

"Worth noting, this is not what it is meant for, and performance might be extremely crippled."
值得注意的是,这不是其预定用途,性能可能会受到极大的限制。

"This single scheduler is mainly used for:"
这个单一调度程序主要用于:

"Optimized for low-latency Runnable one-off executions"
优化用于低延迟的一次性执行。

"according to the docs and is meant for one-shot operations."
根据文档,它用于一次性操作。

英文:

Just to add to the other answer given which is completely correct.

The threads you see in the logs are just scheduling threads, so they schedule work on the event loop. If you only want one event loop, do as the other answer suggested.

If you on the other hand only want one scheduler you can add .onSubscribe(Schedulers.single()) on any Flux/Mono in the chain.

This will during subscription to your application apply a single threaded scheduler.

Worth noting, this is not what it is meant for, and performance might be extremely crippled.

this single scheduler is mainly used for:

> Optimized for low-latency Runnable one-off executions

according to the docs and is meant for one-shot operations.

huangapple
  • 本文由 发表于 2023年3月7日 20:04:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定