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

提示:这是我的个人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.bootgroupId>
	    <artifactId>spring-boot-starter-webartifactId>
	    <exclusions>
	        <exclusion>
	            <artifactId>spring-boot-starter-tomcatartifactId>
	            <groupId>org.springframework.bootgroupId>
	        exclusion>
	    exclusions>
	dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-jettyartifactId>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-undertowartifactId>
    dependency>

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
dependencies>

效果:

三个依赖都存在
SpringBoot内嵌容器tomcat、jetty、undertow切换_第1张图片
SpringBoot内嵌容器tomcat、jetty、undertow切换_第2张图片
移除tomcat,保留jetty和undertow的依赖
SpringBoot内嵌容器tomcat、jetty、undertow切换_第3张图片
SpringBoot内嵌容器tomcat、jetty、undertow切换_第4张图片
移除tomcat和jetty,保留undertow
SpringBoot内嵌容器tomcat、jetty、undertow切换_第5张图片
SpringBoot内嵌容器tomcat、jetty、undertow切换_第6张图片

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

//代表是配置类
@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
SpringBoot内嵌容器tomcat、jetty、undertow切换_第7张图片
SpringBoot内嵌容器tomcat、jetty、undertow切换_第8张图片
配置jetty
SpringBoot内嵌容器tomcat、jetty、undertow切换_第9张图片
SpringBoot内嵌容器tomcat、jetty、undertow切换_第10张图片
配置undertow
SpringBoot内嵌容器tomcat、jetty、undertow切换_第11张图片
SpringBoot内嵌容器tomcat、jetty、undertow切换_第12张图片

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

你可能感兴趣的:(JAVA,后端系列,spring,boot,spring,boot)