SpringBoot内嵌容器tomcat、jetty、undertow切换

41 篇文章 0 订阅
16 篇文章 0 订阅

提示这是我的个人IT资源网站,所有资源都免费,注册登录后就可以看到密码,需要什么大家尽情选取!

今天单独说一下SpringBoot的内嵌容器,我们可以直接启动SpringBoot项目,是因为SpringBoot默认给我们提供了tomcat容器,除了tomcat,SpringBoot还给我们提供了jetty、undertow两种容器选择,我们说一下如何切换使用jetty、undertow容器,首先我们找到源码,看一看

@Configuration(
    proxyBeanMethods = false
)
//首先必须是一个Web应用
@ConditionalOnWebApplication
//会加载我们在application.properties中的自定义配置
@EnableConfigurationProperties({ServerProperties.class})
public class EmbeddedWebServerFactoryCustomizerAutoConfiguration {
    public EmbeddedWebServerFactoryCustomizerAutoConfiguration() {
    }

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnClass({HttpServer.class})
    public static class NettyWebServerFactoryCustomizerConfiguration {
        public NettyWebServerFactoryCustomizerConfiguration() {
        }

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

    @Configuration(
        proxyBeanMethods = false
    )
    //当Undertow.class、SslClientAuthMode.class存在时生效
    @ConditionalOnClass({Undertow.class, SslClientAuthMode.class})
    public static class UndertowWebServerFactoryCustomizerConfiguration {
        public UndertowWebServerFactoryCustomizerConfiguration() {
        }

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

    @Configuration(
        proxyBeanMethods = false
    )
    //当Server.class、Loader.class、WebAppContext.class存在时生效
    @ConditionalOnClass({Server.class, Loader.class, WebAppContext.class})
    public static class JettyWebServerFactoryCustomizerConfiguration {
        public JettyWebServerFactoryCustomizerConfiguration() {
        }

        @Bean
        public JettyWebServerFactoryCustomizer jettyWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) {
            return new JettyWebServerFactoryCustomizer(environment, serverProperties);
        }
    }

    @Configuration(
        proxyBeanMethods = false
    )
    //当Tomcat.class,UpgradeProtocol.class存在时生效
    @ConditionalOnClass({Tomcat.class, UpgradeProtocol.class})
    public static class TomcatWebServerFactoryCustomizerConfiguration {
        public TomcatWebServerFactoryCustomizerConfiguration() {
        }

        @Bean
        public TomcatWebServerFactoryCustomizer tomcatWebServerFactoryCustomizer(Environment environment, ServerProperties serverProperties) {
            return new TomcatWebServerFactoryCustomizer(environment, serverProperties);
        }
    }
}

如果三个容器同时存在,那么他们的优先级是tomcat > jetty > undertow,引入spring-boot-starter-web依赖中就有tomcat容器,所以我们不需要另外引入,如果要使用jetty或者undertow,那么就需要我们单独引入他们的依赖了,这些SpringBoot都给我们默认了版本,所以也不需要再添加版本。

一、pom.xml文件配置
当我们要使用jetty或者undertow时,需要将tomcat容器移除掉,如果jetty和undertow的依赖也同时存在,当我们想使用undertow时,需要将jetty的依赖移除掉,因为jetty的优先级高于undertow,当使用jetty时,undertow的依赖无需移除掉。

<dependencies>
	<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>

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

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

效果:

三个依赖都存在
在这里插入图片描述
在这里插入图片描述
移除tomcat,保留jetty和undertow的依赖
在这里插入图片描述
在这里插入图片描述
移除tomcat和jetty,保留undertow
在这里插入图片描述
在这里插入图片描述

二、通过编写配置类
编写配置类的方式,可以让我们不用管依赖,但是只能配置一个容器,在配置类配置,没有优先级,如果配置了两个或以上容器,启动时会报错

//代表是配置类
@Configuration
public class AuthServerConfiguration {
    
    //声明为一个组件,才会被容器加载
    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
        TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
        tomcatServletWebServerFactory.setPort(8085);//我们可以设置容器的一些属性
        return tomcatServletWebServerFactory;
    }

    //@Bean
    public JettyServletWebServerFactory jettyServletWebServerFactory(){
        JettyServletWebServerFactory jettyServletWebServerFactory = new JettyServletWebServerFactory();
        jettyServletWebServerFactory.setPort(8086);
        return jettyServletWebServerFactory;
    }

    //@Bean
    public UndertowServletWebServerFactory undertowServletWebServerFactory(){
        UndertowServletWebServerFactory undertowServletWebServerFactory = new UndertowServletWebServerFactory();
        undertowServletWebServerFactory.setPort(8087);
        return undertowServletWebServerFactory;
    }

}

效果:

配置tomcat
在这里插入图片描述
在这里插入图片描述
配置jetty
在这里插入图片描述
在这里插入图片描述
配置undertow
在这里插入图片描述
在这里插入图片描述

以上就是SpringBoot切换内嵌容器的两种方式,个人感觉还是通过配置类来切换还是比较灵活的。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot 内 Tomcat 和外部 Tomcat 对比 Spring Boot 是一个非常方便的框架,它可以快速创建一个独立的 Spring 应用程序,并且内TomcatJetty 或者 Undertow 等 Web 容器,可以通过简单的配置来启动和停止 Web 服务器。 与外部 Tomcat 相比,Spring Boot 内 Tomcat 的优点主要有以下几点: 1. 快速启动 Spring Boot 内 Tomcat 可以快速启动应用程序,不需要安装和配置外部 Tomcat 服务器,这使得开发人员更加专注于应用程序开发,节省了时间和精力。 2. 简单配置 Spring Boot 内 Tomcat 的配置非常简单,只需要在 application.properties 或 application.yml 配置文件中定义相关的配置参数,就可以轻松地启动和停止 Web 服务器。 3. 简化部署 使用 Spring Boot 内 Tomcat 可以将应用程序打包成一个独立的可执行的 JAR 文件,这样可以方便地进行部署和维护,也可以方便地进行版本控制和升级。 4. 更好的可移植性 Spring Boot 内 Tomcat 可以在不同的操作系统和环境中运行,这使得应用程序更加可移植和易于部署。 虽然 Spring Boot 内 Tomcat 有很多优点,但是与外部 Tomcat 相比,还是存在一些缺点: 1. 硬件资源消耗 由于内 Tomcat 需要占用一定的硬件资源,所以对于大型的应用程序来说,可能需要更多的硬件资源才能保证其正常运行。 2. 自定义性 内 Tomcat 的功能相对简单,对于一些需要自定义的功能,可能需要使用外部 Tomcat 来实现。 3. 集成其他应用程序 如果需要与其他应用程序进行集成,可能需要使用外部 Tomcat 来实现。 总之,Spring Boot 内 Tomcat 和外部 Tomcat 都有各自的优缺点,应该根据具体的应用场景来选择合适的方案。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值