Spring Boot提供了大量的xxxCustomizer类用于扩展已有功能,比如RestTemplateCustomizer、Jackson2ObjectMapperBuilderCustomizer、PlatformTransactionManagerCustomizer等等,这些都会在以后的开发中慢慢用到。
3)通过EmbeddedServletContainerFactory
Java代码
@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;
}
}
(3)通过TomcatEmbeddedServletContainerFactory类优化Tomcat
Java代码
- @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
引用
server.port=8443
server.ssl.key-store=keystore.p12
server.ssl.key-store-password=password
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat
但是Spring Boot不支持通过application.properties同时支持HTTP和HTTPS。
可以通过在application.properties中设置HTTPS,在代码中添加Connector支持HTTP来实现。
Java代码
- Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
- connector.setPort(8080);
- tomcat.addAdditionalTomcatConnectors(connector);
2)AJP Connector
Java代码
- 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。
Java代码
- LifecycleListener arpLifecycle = new AprLifecycleListener();
- tomcat.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
- tomcat.addContextLifecycleListeners(arpLifecycle);
Java代码
@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;
}
}
(4)容器变更
以下以Tomcat->Jetty为例:
pom.xml
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>
Java代码
- @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;
- }
引用
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)
再次访问 http://localhost:8081/springboot/
(5)多容器配置
Java代码
@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);
}
};
}