大家知道,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.csdn.net/linxdcn/article/details/73382570
http://blog.csdn.net/linxdcn/article/details/73527465
Tomcat服務器默認使用Http11NioProtocol,不清楚是什麼的可以看下這篇博文https://blog.csdn.net/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;