- 啟用Apr模式,需要調(diào)用方法
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#setProtocol
- windows下安裝配置native和apr只需要在環(huán)境變量path里能夠找到
tcnative-1.dll
即可,這個文件在 高版本tomcat\bin\
目錄下會存在
- linux下安裝配置native和apr,參考 https://tomcat.apache.org/native-doc/
TomcatConfig.java
package site.yuyanjia.template.common.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.lang.reflect.Field;
/**
* TomcatConfig
* <p>
* Tomcat優(yōu)化:
* 1票髓、增加JVM內(nèi)存:java -jar -Xms256m -Xmx256m abc.jar
* 2、優(yōu)化Tomcat配置參數(shù)
* 3铣耘、使用APR
*
* @author seer
* @date 2018/7/17 11:48
*/
@Configuration
@Slf4j
@EnableConfigurationProperties(ConfigProperties.class)
public class TomcatConfig {
@Bean
public ServletWebServerFactory servletWebServerFactory(ConfigProperties configProperties) {
ConfigProperties.Tomcat tomcat = configProperties.getTomcat();
TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory();
tomcatServletWebServerFactory.setProtocol(tomcat.getProtocol());
tomcatServletWebServerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
Field[] fields = tomcat.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
connector.setAttribute(field.getName(), field.get(tomcat));
} catch (IllegalAccessException e) {
log.error("Tomcat connector 配置異常", e);
continue;
}
}
});
return tomcatServletWebServerFactory;
}
}
ConfigProperties.java
package site.yuyanjia.template.common.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 配置資源
*
* @author seer
* @date 2018/7/24 14:38
*/
@ConfigurationProperties(value = ConfigProperties.PREFIX)
public class ConfigProperties {
public static final String PREFIX = "yuyanjia";
private Tomcat tomcat;
public Tomcat getTomcat() {
return tomcat;
}
public void setTomcat(Tomcat tomcat) {
this.tomcat = tomcat;
}
/**
* Tomcat配置文件
*
* @author seer
* @date 2018/7/24 14:45
*/
public static class Tomcat {
/**
* 連接超時洽沟,單位ms
*/
private Integer connectionTimeout = 20000;
/**
* 接收連接線程數(shù)量,參考cpu核數(shù)
*/
private Integer acceptorThreadCount = 2;
/**
* 最小監(jiān)聽線程
*/
private Integer minSpareThreads = 5;
/**
* 最大監(jiān)聽線程
* 同時相應(yīng)客戶請求最大值
*/
private Integer maxSpareThreads = 200;
/**
* 最大排隊數(shù)
*/
private Integer acceptCount = 200;
/**
* 最大連接數(shù)
*/
private Integer maxConnections = 800;
/**
* 最大線程數(shù)
*/
private Integer maxThreads = 500;
/**
* 運行模式
*/
private String protocol = "org.apache.coyote.http11.Http11NioProtocol";
public Integer getConnectionTimeout() {
return connectionTimeout;
}
public void setConnectionTimeout(Integer connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public Integer getAcceptorThreadCount() {
return acceptorThreadCount;
}
public void setAcceptorThreadCount(Integer acceptorThreadCount) {
this.acceptorThreadCount = acceptorThreadCount;
}
public Integer getMinSpareThreads() {
return minSpareThreads;
}
public void setMinSpareThreads(Integer minSpareThreads) {
this.minSpareThreads = minSpareThreads;
}
public Integer getMaxSpareThreads() {
return maxSpareThreads;
}
public void setMaxSpareThreads(Integer maxSpareThreads) {
this.maxSpareThreads = maxSpareThreads;
}
public Integer getAcceptCount() {
return acceptCount;
}
public void setAcceptCount(Integer acceptCount) {
this.acceptCount = acceptCount;
}
public Integer getMaxConnections() {
return maxConnections;
}
public void setMaxConnections(Integer maxConnections) {
this.maxConnections = maxConnections;
}
public Integer getMaxThreads() {
return maxThreads;
}
public void setMaxThreads(Integer maxThreads) {
this.maxThreads = maxThreads;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
}
}
application.yml
yuyanjia:
tomcat:
connection-timeout: 10000
acceptor-thread-count: 4
min-spare-threads: 100
max-spare-threads: 300
accept-count: 200
max-connections: 800
max-threads: 500
protocol: org.apache.coyote.http11.Http11AprProtocol