SpringBoot内嵌容器tomcat、jetty、undertow切换
SpringBoot内嵌容器tomcat、jetty、undertow切换
今天单独说一下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);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
如果三个容器同时存在,那么他们的优先级是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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
效果:
三个依赖都存在
移除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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
效果:
配置tomcat
配置jetty
配置undertow
以上就是SpringBoot切换内嵌容器的两种方式,个人感觉还是通过配置类来切换还是比较灵活的。
————————————————
版权声明:本文为CSDN博主「ITMANLZY」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45345374/article/details/114696151