springboot内嵌了三个容器,tomcat(默认),jetty,undertow;如何替换容器可以见这篇文章 springboot将tomcat容器换为jetty容器 该文只从源码上面讨论容器的配置
相关包为spring-boot-qutoconfigure:2.4.3
第一个入口类为:
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
该类会加载配置类:ServerProperties
若是使用默认tomcat容器,则会创建TomcatWebServerFactoryCustomizer这个bean
ServerProperties类里面有三个非常重要的内部类Jetty,Tomcat,Undertow三个类;该类部分代码如下:
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
/**
* Server HTTP port.
*/
private Integer port;
/**
* Network address to which the server should bind.
*/
private InetAddress address;
@NestedConfigurationProperty
private final ErrorProperties error = new ErrorProperties();
/**
* Strategy for handling X-Forwarded-* headers.
*/
private ForwardHeadersStrategy forwardHeadersStrategy;
/**
* Value to use for the Server response header (if empty, no header is sent).
*/
private String serverHeader;
/**
* Maximum size of the HTTP message header.
*/
private DataSize maxHttpHeaderSize = DataSize.ofKilobytes(8);
/**
* Type of shutdown that the server will support.
*/
private Shutdown shutdown = Shutdown.IMMEDIATE;
@NestedConfigurationProperty
private Ssl ssl;
@NestedConfigurationProperty
private final Compression compression = new Compression();
@NestedConfigurationProperty
private final Http2 http2 = new Http2();
private final Servlet servlet = new Servlet();
private final Tomcat tomcat = new Tomcat();
private final Jetty jetty = new Jetty();
private final Netty netty = new Netty();
private final Undertow undertow = new Undertow();
}
配置以server.开头,然后接上属性名,如下所示:
server.port 配置启动端口
默认容器Tomcat内部类代码如下:
public static class Tomcat {
/**
* Access log configuration.
*/
private final Accesslog accesslog = new Accesslog();
/**
* Thread related configuration.
*/
private final Threads threads = new Threads();
/**
* Tomcat base directory. If not specified, a temporary directory is used.
*/
private File basedir;
/**
* Delay between the invocation of backgroundProcess methods. If a duration suffix
* is not specified, seconds will be used.
*/
@DurationUnit(ChronoUnit.SECONDS)
private Duration backgroundProcessorDelay = Duration.ofSeconds(10);
/**
* Maximum size of the form content in any HTTP post request.
*/
private DataSize maxHttpFormPostSize = DataSize.ofMegabytes(2);
/**
* Maximum amount of request body to swallow.
*/
private DataSize maxSwallowSize = DataSize.ofMegabytes(2);
/**
* Whether requests to the context root should be redirected by appending a / to
* the path. When using SSL terminated at a proxy, this property should be set to
* false.
*/
private Boolean redirectContextRoot = true;
/**
* Whether HTTP 1.1 and later location headers generated by a call to sendRedirect
* will use relative or absolute redirects.
*/
private boolean useRelativeRedirects;
/**
* Character encoding to use to decode the URI.
*/
private Charset uriEncoding = StandardCharsets.UTF_8;
/**
* Maximum number of connections that the server accepts and processes at any
* given time. Once the limit has been reached, the operating system may still
* accept connections based on the "acceptCount" property.
*/
private int maxConnections = 8192;
/**
* Maximum queue length for incoming connection requests when all possible request
* processing threads are in use.
*/
private int acceptCount = 100;
/**
* Maximum number of idle processors that will be retained in the cache and reused
* with a subsequent request. When set to -1 the cache will be unlimited with a
* theoretical maximum size equal to the maximum number of connections.
*/
private int processorCache = 200;
/**
* Comma-separated list of additional patterns that match jars to ignore for TLD
* scanning. The special '?' and '*' characters can be used in the pattern to
* match one and only one character and zero or more characters respectively.
*/
private List<String> additionalTldSkipPatterns = new ArrayList<>();
/**
* Comma-separated list of additional unencoded characters that should be allowed
* in URI paths. Only "< > [ \ ] ^ ` { | }" are allowed.
*/
private List<Character> relaxedPathChars = new ArrayList<>();
/**
* Comma-separated list of additional unencoded characters that should be allowed
* in URI query strings. Only "< > [ \ ] ^ ` { | }" are allowed.
*/
private List<Character> relaxedQueryChars = new ArrayList<>();
/**
* Amount of time the connector will wait, after accepting a connection, for the
* request URI line to be presented.
*/
private Duration connectionTimeout;
/**
* Static resource configuration.
*/
private final Resource resource = new Resource();
/**
* Modeler MBean Registry configuration.
*/
private final Mbeanregistry mbeanregistry = new Mbeanregistry();
/**
* Remote Ip Valve configuration.
*/
private final Remoteip remoteip = new Remoteip();
}
默认参数基本都够用了,其中处理请求线程的配置Threads代码如下:
/**
* Tomcat thread properties.
*/
public static class Threads {
/**
* Maximum amount of worker threads.
*/
private int max = 200;
/**
* Minimum amount of worker threads.
*/
private int minSpare = 10;
}
若是网站并发较高,可以调max大小,
server.tomcat.threads.max = 300
总结:
1,配置都在ServerProperties这个类中
2,配置以server.开头,加类的属性名即可