(1)修改Tomcat版本
1.5.1.RELEASE默认采用的是8.5.11。
引用
2017-02-07 16:23:44.964 INFO 8040 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
修改pom.xml,重新运行。
8.0.41
引用
2017-02-07 16:26:37.799 INFO 7372 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.41
(2)修改端口号/web路径
1)修改SpringBoot自动配置项
src/main/resources/application.properties
引用
server.port=8081
server.contextPath=/springboot
server.sessionTimeout=30
server.contextPath=/springboot
server.sessionTimeout=30
引用
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
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
再次访问 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); } }; } }
Spring Boot提供了大量的xxxCustomizer类用于扩展已有功能,比如RestTemplateCustomizer、Jackson2ObjectMapperBuilderCustomizer、PlatformTransactionManagerCustomizer等等,这些都会在以后的开发中慢慢用到。
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; } }
(3)通过TomcatEmbeddedServletContainerFactory类优化Tomcat
@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
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来实现。
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; } }
(4)容器变更
以下以Tomcat->Jetty为例:
pom.xml
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.springframework.boot spring-boot-starter-jetty
@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)
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)多容器配置
@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); } }; }