SpringBoot 自动配置之切换内置服务器


前言

SpringBoot 的 web 环境中默认使用 tomcat 作为内置服务器,其实 SpringBoot 提供了4种内置服务器供我们选择,我们可以很方便的切换。
本章与之前发布的 SpringBoot 自动配置之 Condition 章节相关联,没有看过上一章的小伙伴可以先读一下上一章。

在没有引入 web 依赖前,我们启动项目会直接运行至终止,并不会一直开放供我们访问。
如下图我们可以看到,启动项目后 tomcat 并没有被启动。
在这里插入图片描述


引入 web 起步依赖

因为 web 容器才会让程序一直处于启动状态供我们访问。
在 pom.xml 中添加 spring-boot-starter-web 起步依赖。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

添加后我们再次启动项目,现在就可以看到:tomcat 被启动了,端口为默认的 8080,如此就可以证明 SpringBoot 内置默认是使用 tomcat 作为服务器。
在这里插入图片描述

查看 SptingBoot 提供的内置服务器

在 External Libraries 中找到 spring-boot-autoconfigure 这个 SpringBoot 的自动配置 jar 包,找到 web 包下的 embedded 包,这个包表示嵌入式,内置的。
如图可以看到一共有四种服务器,Jetty,Netty,Tomcat 以及 Undertow :
在这里插入图片描述
进入 EmbeddedWebServerFactoryCustomizerAutoConfiguration 配置类,可以看到配置类中有 Tomcat、Jetty、Undertow、Netty 各自的工厂,代码中贴上了部分注释:

/*
 * Copyright 2012-2019 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
 *
 *      https://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 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;

/**
 * {@link EnableAutoConfiguration Auto-configuration} for embedded servlet and reactive
 * web servers customizations.
 *
 * @author Phillip Webb
 * @since 2.0.0
 */
 /** 配置类 */
@Configuration(proxyBeanMethods = false)
 /** 当是 web 环境时才会加载 */
@ConditionalOnWebApplication
@EnableConfigurationProperties(ServerProperties.class)
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {

	/**
	 * Nested configuration if Tomcat is being used.
	 */
	@Configuration(proxyBeanMethods = false)
	 /** 
	  * 在环境中必须有 Tomcat 的 class 以及 UpgradeProtocol 的 class
	  *	才会初始化,这些 class 只有导入了 Tomcat 对应的坐标才会有
	  */
	@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(proxyBeanMethods = false)
	 /** 
	  * 在环境中必须有 Server 的 class、Loader 的 class 以及 WebAppContext 的 class
	  *	才会初始化,这些 class 只有导入了 Jetty 对应的坐标才会有
	  */
	@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(proxyBeanMethods = false)
	 /** 
	  * 在环境中必须有 Undertow 的 class 以及 SslClientAuthMode 的 class
	  *	才会初始化,这些 class 只有导入了 Undertow 对应的坐标才会有
	  */
	@ConditionalOnClass({ Undertow.class, SslClientAuthMode.class })
	public static class UndertowWebServerFactoryCustomizerConfiguration {

		@Bean
		public UndertowWebServerFactoryCustomizer undertowWebServerFactoryCustomizer(Environment environment,
				ServerProperties serverProperties) {
			return new UndertowWebServerFactoryCustomizer(environment, serverProperties);
		}

	}

	/**
	 * Nested configuration if Netty is being used.
	 */
	@Configuration(proxyBeanMethods = false)
	 /** 
	  * 在环境中必须有 HttpServer 的 class 才会初始化,
	  *	这个 class 只有导入了 Netty 对应的坐标才会有
	  */
	@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 中添加:

        <!-- 引入 Jetty 的依赖 -->
        <dependency>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <groupId>org.springframework.boot</groupId>
        </dependency>

重启项目,就可以看到当前是 Jetty 的服务器被启动了。
在这里插入图片描述
想要切换其他的服务器也是一样的,排除掉 tomcat 后引入对应的依赖就可以了。


总结

本章介绍了如何切换 SpringBoot 内置的服务器,他内部的原理跟上个章节讲的 Condition 是一样的,都是通过判断当前环境中有没有对应的坐标以实现我们需要的功能。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值