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

导读:本篇文章讲解 SpringBoot配置jetty.threadPool.maxThreads能否生效,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

基于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线程池的位置

<init>: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容器的配置中

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/93688.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!