大家知道,SpringBoot内嵌Tomcat服务器,那么除了Tomcat服务器,SpringBoot还支持哪些Web服务器呢?我们来看看源码。
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.web.embedded;
import io.undertow.Undertow;
import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.UpgradeProtocol;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.webapp.WebAppContext;
import org.xnio.SslClientAuthMode;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* {@link EnableAutoConfiguration Auto-configuration} for embedded servlet and reactive
* web servers customizations.
*
* @author Phillip Webb
* @since 2.0.0
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
/**
* Nested configuration if Tomcat is being used.
*/
@Configuration
@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
public static class TomcatWebServerFactoryCustomizerConfiguration {
@Bean
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
}
}
/**
* Nested configuration if Jetty is being used.
*/
@Configuration
@ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })
public static class JettyWebServerFactoryCustomizerConfiguration {
@Bean
public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
/**
* Nested configuration if Undertow is being used.
*/
@Configuration
@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
public static class UndertowWebServerFactoryCustomizerConfiguration {
@Bean
public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(
Environment environment, ServerProperties serverProperties) {
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
}
}
}
这是SpringBoot自动配置内嵌Web服务器的代码,从注释中我们可以看出,SpringBoot支持Tomcat、Jetty、Undertow三个服务器,当然默认使用的是Tomcat服务器。如果我们要切换成其他Web服务器,则要先移除spring-boot-starter-tomcat这个包,导入其他两个jar包,分别是spring-boot-starter-jetty和spring-boot-starter-undertow
gradle移除jar包的做法是
dependencies {
compile("org.gradle.test.excludes:api:1.0") {
exclude module: 'shared'
}
}
maven的做法是
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Tomcat的核心组件是Connector,Connector的处理逻辑可以参考这篇文章
https://blog.youkuaiyun.com/linxdcn/article/details/73382570
http://blog.youkuaiyun.com/linxdcn/article/details/73527465
Tomcat服务器默认使用Http11NioProtocol,不清楚是什么的可以看下这篇博文https://blog.youkuaiyun.com/jeff_fangji/article/details/43909677
与Http11NioProtocol相对的协议是Http11Protocol,这个协议已经过时,将会在Tomcat9移除。
Http11NioProtocol类的唯一构造函数使用了NioEndpoint来构造成员变量
如果要换其他Protocol,可以参考这篇博文进行配置
NioEndPoint默认设置
PollerThread
默认名:http-nio-端口号-ClientPoller-序号,例如http-nio-8080-ClientPoller-0
默认数量:2
如果使用Http11NioProtocol来替代NioEndPoint,则可以修改默认数量配置(Http11NioProtocol是NioEndPoint的实现类)。
// -------------------- Pool setup --------------------
public void setPollerThreadCount(int count) {
((NioEndpoint)getEndpoint()).setPollerThreadCount(count);
}
先启动PollerThread,再启动AcceptorThread
AcceptorThread 默认数量是 1,可以设置
/**
* Acceptor thread count.
*/
protected int acceptorThreadCount = 1;