SpringBoot配置jetty.threadPool.maxThreads能否生效 您所在的位置:网站首页 jetty翻译 SpringBoot配置jetty.threadPool.maxThreads能否生效

SpringBoot配置jetty.threadPool.maxThreads能否生效

2023-10-28 18:38| 来源: 网络整理| 查看: 265

基于SpringBoot1.5.18

Jetty线程架构模型

Jetty的线程架构模型,分为acceptors,selectors和workers三个线程池。

acceptors负责接受新连接,然后交给selectors处理HTTP消息协议的解包,最后由workers处理请求。

前两个线程池采用非阻塞模型,一个线程可以处理很多socket的读写,所以线程池数量较小。

SpringBoot1.5.18自带Jetty配置

org/springframework/boot/spring-boot-autoconfigure/1.5.18.RELEASE/spring-boot-autoconfigure-1.5.18.RELEASE.jar!/META-INF/spring-configuration-metadata.json

*** { "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties$Jetty", "defaultValue": -1, "name": "server.jetty.acceptors", "description": "Number of acceptor threads to use. When the value is -1, the default, the number of acceptors is derived from the operating environment.", "type": "java.lang.Integer" }, { "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties$Jetty", "defaultValue": 200000, "name": "server.jetty.max-http-post-size", "description": "Maximum size in bytes of the HTTP post or put content.", "type": "java.lang.Integer" }, { "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties$Jetty", "defaultValue": -1, "name": "server.jetty.selectors", "description": "Number of selector threads to use. When the value is -1, the default, the number of selectors is derived from the operating environment.", "type": "java.lang.Integer" }, ***

对应配置类中

org.springframework.boot.autoconfigure.web.ServerProperties

public static class Jetty { /** * Maximum size in bytes of the HTTP post or put content. */ private int maxHttpPostSize = 200000; // bytes /** * Number of acceptor threads to use. When the value is -1, the default, the * number of acceptors is derived from the operating environment. */ private Integer acceptors = -1; /** * Number of selector threads to use. When the value is -1, the default, the * number of selectors is derived from the operating environment. */ private Integer selectors = -1; ***

可以看到,只有acceptors和selectors可在配置文件配置,且默认大小为-1,无限制

SpringBoot 2.x是有配置的

SpringBoot2.2.6

org.springframework.boot.autoconfigure.web.ServerProperties

public static class Jetty { /** * Access log configuration. */ private final Accesslog accesslog = new Accesslog(); /** * Maximum size of the form content in any HTTP post request. */ private DataSize maxHttpFormPostSize = DataSize.ofBytes(200000); /** * Number of acceptor threads to use. When the value is -1, the default, the * number of acceptors is derived from the operating environment. */ private Integer acceptors = -1; /** * Number of selector threads to use. When the value is -1, the default, the * number of selectors is derived from the operating environment. */ private Integer selectors = -1; /** * Maximum number of threads. */ private Integer maxThreads = 200; /** * Minimum number of threads. */ private Integer minThreads = 8; /** * Maximum thread idle time. */ private Duration threadIdleTimeout = Duration.ofMillis(60000); /** * Time that the connection can be idle before it is closed. */ private Duration connectionIdleTimeout; public Accesslog getAccesslog() { return this.accesslog; } SpringBoot初始化Jetty容器worker线程池的位置 :133, Server (org.eclipse.jetty.server) createServer:982, JettyEmbeddedServletContainerFactory$Jetty9ServerFactory (org.springframework.boot.context.embedded.jetty) createServer:197, JettyEmbeddedServletContainerFactory (org.springframework.boot.context.embedded.jetty) getEmbeddedServletContainer:174, JettyEmbeddedServletContainerFactory (org.springframework.boot.context.embedded.jetty) createEmbeddedServletContainer:166, EmbeddedWebApplicationContext (org.springframework.boot.context.embedded) onRefresh:136, EmbeddedWebApplicationContext (org.springframework.boot.context.embedded) refresh:537, AbstractApplicationContext (org.springframework.context.support) refresh:124, EmbeddedWebApplicationContext (org.springframework.boot.context.embedded) refresh:693, SpringApplication (org.springframework.boot) refreshContext:360, SpringApplication (org.springframework.boot) run:303, SpringApplication (org.springframework.boot) run:1118, SpringApplication (org.springframework.boot) run:1107, SpringApplication (org.springframework.boot) main:49, DossierApplication (com.thunisoft.dzjz.server)

此处的getThreadPool()返回为空,如果想自定义线程池,可以调用在Jetty启动前创建线程池并调用setThreadPool方法

org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory

private Server createServer(InetSocketAddress address) { Server server; if (ClassUtils.hasConstructor(Server.class, ThreadPool.class)) { server = new Jetty9ServerFactory().createServer(getThreadPool()); } else { server = new Jetty8ServerFactory().createServer(getThreadPool()); } server.setConnectors(new Connector[] { createConnector(address, server) }); return server; } /** * Returns a Jetty {@link ThreadPool} that should be used by the {@link Server}. * @return a Jetty {@link ThreadPool} or {@code null} */ public ThreadPool getThreadPool() { return this.threadPool; } /** * Set a Jetty {@link ThreadPool} that should be used by the {@link Server}. If set to * {@code null} (default), the {@link Server} creates a {@link ThreadPool} implicitly. * @param threadPool a Jetty ThreadPool to be used */ public void setThreadPool(ThreadPool threadPool) { this.threadPool = threadPool; }

当传入的pool为空,创建默认线程池QueuedThreadPool

org.eclipse.jetty.server.Server

public Server(@Name("threadpool") ThreadPool pool) { _threadPool=pool!=null?pool:new QueuedThreadPool(); addBean(_threadPool); setServer(this); }

默认构造方法,设置maxThreads为200

org.eclipse.jetty.util.thread.QueuedThreadPool

public QueuedThreadPool() { this(200); } public QueuedThreadPool(@Name("maxThreads") int maxThreads) { this(maxThreads, Math.min(8, maxThreads)); } jetty.threadPool.maxThreads是否能生效

综上,通过在配置文件增加jetty.threadPool.maxThreads,并不会再SpringBoot初始化时设置到内嵌Jetty容器的配置中



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有