15-SpringBoot自动配置-切换内置web服务器

2022-03-23 16:11:15 浏览数 (5)

15-SpringBoot自动配置-切换内置web服务器

切换内置web服务器

SpringBoot的Web环境中默认使用tomcat作为内置服务器,其实SpringBoot提供了4种内置服务器供我们选择,我们可以很方便的切换。

演示案例

1.我们首先还是使用上一篇的项目,启动看看 Tomcat 作为内置 web 服务启动

首先要确认一下项目的 pom.xml 是否有 web 启动依赖:

代码语言:javascript复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.确认了存在依赖之后,我们在从源码查看内置的web服务器

我们首先打开外部库,依然是找到 autoconfigure 的包:

再找到 web 包,如下:

可以看到内置了几种web服务器。

代码语言:javascript复制
/*
 * 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
@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);
      }

   }

   /**
    * Nested configuration if Netty is being used.
    */
   @Configuration
   @ConditionalOnClass(HttpServer.class)
   public static class NettyWebServerFactoryCustomizerConfiguration {

      @Bean
      public NettyWebServerFactoryCustomizer nettyWebServerFactoryCustomizer(Environment environment,
            ServerProperties serverProperties) {
         return new NettyWebServerFactoryCustomizer(environment, serverProperties);
      }

   }

}

也就是说,我们只要改变类的依赖,那么就应该可以切换内置的web服务器了。

3.查看 spring-boot-starter-web 启动依赖的内部继承关系

4.排除 tomcat 的依赖

image-20220224213305912

设置了排除之后,我们回头看看 pom.xml 里面的配置如下:

代码语言:javascript复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>

可以看到已经排除了依赖。

5.增加 jetty 依赖,使用 jetty 启动web服务

代码语言:javascript复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!--  排除tomcat依赖  -->
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 配置jetty依赖    -->
<dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>

0 人点赞