Tomcat Connector(连接器)有三种运行模式:bio nio apr
bio(blocking I/O)
nio(new I/O)
<Connector port="1024" protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" redirectPort="8443" />
apr(Apache Portable Runtime/Apache可移植运行时)
而SpringBoot默认是以 java -Xmx256m -Xss256k -jar xx.jar
来运行内置Tomcat启动方式默认是NIO,所以想用Apr方式启动怎么办呢?
更改pom文件
<packaging>jar</packaging> 改为=> <packaging>war</packaging> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 移除内嵌Tomcat需要重新添加servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency>
在 Application 启动类中继承SpringBootServletInitializer具体类代码如下
package com.ctx.springboot; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(SpringBootStartApplication.class); } }
这样就可以使用把SpringBoot项目打包成war扔到8.0以上的tomcat里跑运行方式默认就变成apr了如下图: