Spring Boot便于开发支持嵌入式容器(比如:Apache Tomcat, Eclipse Jetty, RedHat Undertow),默认启动嵌入式Tomcat容器,当然也可以打包发不动独立的Servlet容器(比如:Apache Tomcat,WildFly等)。
[b](1)修改Tomcat版本[/b]
1.5.1.RELEASE默认采用的是8.5.11。
[quote]2017-02-07 16:23:44.964 INFO 8040 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11[/quote]
修改pom.xml,重新运行。
[quote]2017-02-07 16:26:37.799 INFO 7372 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.41[/quote]
[b](2)修改端口号/web路径[/b]
1)修改SpringBoot自动配置项
src/main/resources/application.properties
[quote]server.port=8081
server.contextPath=/springboot
server.sessionTimeout=30[/quote]
[quote]2017-02-07 16:23:45.740 INFO 8040 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-02-07 16:23:45.746 INFO 8040 --- [ main] c.r.s.SpringBootDemo4Application : Started SpringBootDemo4Application in 2.178 seconds (JVM running for 2.668)
2017-02-07 16:24:16.474 INFO 8040 --- [nio-8081-exec-1] o.a.c.c.C.[.[localhost].[/springboot] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-02-07 16:24:16.474 INFO 8040 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-02-07 16:24:16.487 INFO 8040 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 13 ms[/quote]
再次访问 http://localhost:8081/springboot/
2)通过EmbeddedServletContainerCustomizer
[color=red]Spring Boot提供了大量的xxxCustomizer类用于扩展已有功能,比如RestTemplateCustomizer、Jackson2ObjectMapperBuilderCustomizer、PlatformTransactionManagerCustomizer等等,这些都会在以后的开发中慢慢用到。[/color]
3)通过EmbeddedServletContainerFactory
[b](3)通过TomcatEmbeddedServletContainerFactory类优化Tomcat[/b]
1)开启SSL(HTTPS)
默认SpringBoot启动8080端口的HTTP服务,通过以下配置可以开启8443端口的HTTPS服务。
src/main/resources/application.properties
[quote]server.port=8443
server.ssl.key-store=keystore.p12
server.ssl.key-store-password=password
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat[/quote]
但是Spring Boot不支持通过application.properties同时支持HTTP和HTTPS。
可以通过在application.properties中设置HTTPS,在代码中添加Connector支持HTTP来实现。
2)AJP Connector
3)apr/apr-util/tomcat-native
前提是必须安装这些软件,并指定java.library.path参数给JVM。
[b](4)容器变更[/b]
以下以Tomcat->Jetty为例:
pom.xml
[quote]2017-02-07 16:27:50.802 INFO 452 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8081 (http/1.1)
2017-02-07 16:27:50.805 INFO 452 --- [ main] c.r.s.SpringBootDemo4Application : Started SpringBootDemo4Application in 2.172 seconds (JVM running for 2.67)[/quote]
再次访问 http://localhost:8081/springboot/
[b](5)多容器配置[/b]
[b](1)修改Tomcat版本[/b]
1.5.1.RELEASE默认采用的是8.5.11。
[quote]2017-02-07 16:23:44.964 INFO 8040 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11[/quote]
修改pom.xml,重新运行。
<properties>
<tomcat.version>8.0.41</tomcat.version>
</properties>
[quote]2017-02-07 16:26:37.799 INFO 7372 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.41[/quote]
[b](2)修改端口号/web路径[/b]
1)修改SpringBoot自动配置项
src/main/resources/application.properties
[quote]server.port=8081
server.contextPath=/springboot
server.sessionTimeout=30[/quote]
[quote]2017-02-07 16:23:45.740 INFO 8040 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-02-07 16:23:45.746 INFO 8040 --- [ main] c.r.s.SpringBootDemo4Application : Started SpringBootDemo4Application in 2.178 seconds (JVM running for 2.668)
2017-02-07 16:24:16.474 INFO 8040 --- [nio-8081-exec-1] o.a.c.c.C.[.[localhost].[/springboot] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-02-07 16:24:16.474 INFO 8040 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-02-07 16:24:16.487 INFO 8040 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 13 ms[/quote]
再次访问 http://localhost:8081/springboot/
2)通过EmbeddedServletContainerCustomizer
@Configuration
public class ContainerConfig {
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// Port Number
container.setPort(8081);
// Context Path
container.setContextPath("/springboot");
// Session Timeout
container.setSessionTimeout(30, TimeUnit.MINUTES);
}
};
}
}
[color=red]Spring Boot提供了大量的xxxCustomizer类用于扩展已有功能,比如RestTemplateCustomizer、Jackson2ObjectMapperBuilderCustomizer、PlatformTransactionManagerCustomizer等等,这些都会在以后的开发中慢慢用到。[/color]
3)通过EmbeddedServletContainerFactory
@Configuration
public class ContainerConfig {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
tomcat.setPort(8081);
tomcat.setContextPath("/springboot");
tomcat.setSessionTimeout(30, TimeUnit.MINUTES);
return tomcat;
}
}
[b](3)通过TomcatEmbeddedServletContainerFactory类优化Tomcat[/b]
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
// ...
}
};
tomcat.addAdditionalTomcatConnectors(initiateHttpConnector());
return tomcat;
}
1)开启SSL(HTTPS)
默认SpringBoot启动8080端口的HTTP服务,通过以下配置可以开启8443端口的HTTPS服务。
src/main/resources/application.properties
[quote]server.port=8443
server.ssl.key-store=keystore.p12
server.ssl.key-store-password=password
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat[/quote]
但是Spring Boot不支持通过application.properties同时支持HTTP和HTTPS。
可以通过在application.properties中设置HTTPS,在代码中添加Connector支持HTTP来实现。
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(8080);
tomcat.addAdditionalTomcatConnectors(connector);
2)AJP Connector
Connector ajpConnector = new Connector("AJP/1.3");
ajpConnector.setProtocol("AJP/1.3");
ajpConnector.setPort(9090);
ajpConnector.setSecure(false);
ajpConnector.setAllowTrace(false);
ajpConnector.setScheme("http");
tomcat.addAdditionalTomcatConnectors(ajpConnector);
3)apr/apr-util/tomcat-native
前提是必须安装这些软件,并指定java.library.path参数给JVM。
LifecycleListener arpLifecycle = new AprLifecycleListener();
tomcat.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
tomcat.addContextLifecycleListeners(arpLifecycle);
@Configuration
public class ContainerConfig {
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
// ...
}
};
tomcat.setPort(8081);
tomcat.setContextPath("/springboot");
tomcat.setSessionTimeout(30, TimeUnit.MINUTES);
LifecycleListener arpLifecycle = new AprLifecycleListener();
tomcat.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
tomcat.addContextLifecycleListeners(arpLifecycle);
Connector ajpConnector = new Connector("org.apache.coyote.ajp.AjpAprProtocol");
ajpConnector.setPort(9090);
ajpConnector.setSecure(false);
ajpConnector.setAllowTrace(false);
ajpConnector.setScheme("http");
tomcat.addAdditionalTomcatConnectors(ajpConnector);
return tomcat;
}
}
[b](4)容器变更[/b]
以下以Tomcat->Jetty为例:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
JettyEmbeddedServletContainerFactory container = new JettyEmbeddedServletContainerFactory();
// Port Number
container.setPort(8081);
// Context Path
container.setContextPath("/springboot");
// Session Timeout
container.setSessionTimeout(30);
return container;
}
[quote]2017-02-07 16:27:50.802 INFO 452 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8081 (http/1.1)
2017-02-07 16:27:50.805 INFO 452 --- [ main] c.r.s.SpringBootDemo4Application : Started SpringBootDemo4Application in 2.172 seconds (JVM running for 2.67)[/quote]
再次访问 http://localhost:8081/springboot/
[b](5)多容器配置[/b]
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
// Port Number
if (TomcatEmbeddedServletContainerFactory.class.isAssignableFrom(container.getClass())) {
TomcatEmbeddedServletContainerFactory tomcat =
TomcatEmbeddedServletContainerFactory.class.cast(container);
tomcat.setPort(8081);
}
if (JettyEmbeddedServletContainerFactory.class.isAssignableFrom(container.getClass())) {
JettyEmbeddedServletContainerFactory jetty =
JettyEmbeddedServletContainerFactory.class.cast(container);
jetty.setPort(8082);
}
if (UndertowEmbeddedServletContainerFactory.class.isAssignableFrom(container.getClass())) {
UndertowEmbeddedServletContainerFactory undertow =
UndertowEmbeddedServletContainerFactory.class.cast(container);
undertow.setPort(8083);
}
// Context Path
container.setContextPath("/springboot");
// Session Timeout
container.setSessionTimeout(30, TimeUnit.MINUTES);
}
};
}