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

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

切换内置web服务器

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

演示案例

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

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

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

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

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

8a3f00d129c4013302e4895708a4e52a.png

再找到 web 包,如下:

01ef55cf3e07258d3efb0735a6968620.png

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

77f3c79e9a22f5c869018ca1edcc55af.png

 
/*
 * 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 启动依赖的内部继承关系

ca4db921da46d4f2024a0ba5bac102b0.png

4.排除 tomcat 的依赖

0bcb64f6c5ae527c21acaf42701bce03.png
image-20220224213305912

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

a61097e0a4c4efe1e9b6f1c5ecb9e31f.png

<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服务

787e1881eade70ad75a0344a937a2fec.png

<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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海洋的渔夫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值