- 前言
- 引入 web 起步依赖
- 查看 SptingBoot 提供的内置服务器
- 切换内置服务器
- 总结
前言
SpringBoot 的 web 环境中默认使用 tomcat 作为内置服务器,其实 SpringBoot 提供了4种内置服务器供我们选择,我们可以很方便的切换。
本章与之前发布的 SpringBoot 自动配置之 Condition 章节相关联,没有看过上一章的小伙伴可以先读一下上一章。
在没有引入 web 依赖前,我们启动项目会直接运行至终止,并不会一直开放供我们访问。
如下图我们可以看到,启动项目后 tomcat 并没有被启动。
引入 web 起步依赖
因为 web 容器才会让程序一直处于启动状态供我们访问。
在 pom.xml 中添加 spring-boot-starter-web 起步依赖。
org.springframework.boot spring-boot-starter-web
添加后我们再次启动项目,现在就可以看到:tomcat 被启动了,端口为默认的 8080,如此就可以证明 SpringBoot 内置默认是使用 tomcat 作为服务器。
在 External Libraries 中找到 spring-boot-autoconfigure 这个 SpringBoot 的自动配置 jar 包,找到 web 包下的 embedded 包,这个包表示嵌入式,内置的。
如图可以看到一共有四种服务器,Jetty,Netty,Tomcat 以及 Undertow :
进入 EmbeddedWebServerFactoryCustomizerAutoConfiguration 配置类,可以看到配置类中有 Tomcat、Jetty、Undertow、Netty 各自的工厂,代码中贴上了部分注释:
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 reactor.netty.http.server.HttpServer;
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;
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Tomcat.class, UpgradeProtocol.class })
public static class TomcatWebServerFactoryCustomizerConfiguration {
@Bean
public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment,
ServerProperties serverProperties) {
return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Server.class, Loader.class, WebAppContext.class })
public static class JettyWebServerFactoryCustomizerConfiguration {
@Bean
public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(Environment environment,
ServerProperties serverProperties) {
return new JettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
public static class UndertowWebServerFactoryCustomizerConfiguration {
@Bean
public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(Environment environment,
ServerProperties serverProperties) {
return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(HttpServer.class)
public static class NettyWebServerFactoryCustomizerConfiguration {
@Bean
public NettyWebServerFactoryCustomizer nettyWebServerFactoryCustomizer(Environment environment,
ServerProperties serverProperties) {
return new NettyWebServerFactoryCustomizer(environment, serverProperties);
}
}
}
切换内置服务器
通过上面的代码可以看出来,我们只需要导入不同的 web 服务器的坐标,就可以实现服务器的动态切换。
在 starter-web 中,其实内部就依赖了 Tomcat 的坐标,右键选择 Show Dependencies 查看:
可以看到 starter-web 内置了 starter-tomcat。
如果我们想要切换内置的服务器,首先我们需要把 tomcat 排除掉,选中 spring-boot-starter-tomcat 右键选择 Exclude。
排除掉之后在 pom.xml 中可以发现新增了一个排除 tomcat 的选项。
那么现在我们想要切换服务器的话就只需要再引入一个依赖就可以了,比如我想使用 Jetty:
在 pom.xml 中添加:
spring-boot-starter-jetty
org.springframework.boot
重启项目,就可以看到当前是 Jetty 的服务器被启动了。
想要切换其他的服务器也是一样的,排除掉 tomcat 后引入对应的依赖就可以了。
总结
本章介绍了如何切换 SpringBoot 内置的服务器,他内部的原理跟上个章节讲的 Condition 是一样的,都是通过判断当前环境中有没有对应的坐标以实现我们需要的功能。



