说明
- 因为项目用的springboot 没有用外置tomcat,但是IO压力导致需要提高并发等所以需要配置tomcat 网络请求模式从NIO成APR,几经周折……
- 环境 AWS / Centos6.5
- 本文参考部分网络大神文章多篇,整合到一起
安装步骤
- apr
- apr-iconv
- apr-util
- tomcat-native
- 配置环境变量
参见如下脚本,局部修改可以直接跑
修改地方如下:
- 针对不同内嵌tomcat版本下载版本的tomcat-native 我的是tomcat8 native是1.2最高版本
- 脚本下面的JAVAHOME 要换一下
yum install expat-devel
cd /usr/local/src
wget https://mirrors.cnnic.cn/apache/apr/apr-1.6.5.tar.gz
tar xf apr-1.6.5.tar.gz
cd apr-1.6.5
./configure --prefix=/usr/local/apr
make && make install
cd /usr/local/src
wget https://mirrors.cnnic.cn/apache/apr/apr-iconv-1.2.2.tar.gz
tar xf apr-iconv-1.2.2.tar.gz
cd apr-iconv-1.2.2/
./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-iconv
make && make install
cd /usr/local/src
wget https://mirrors.cnnic.cn/apache/apr/apr-util-1.6.1.tar.gz
tar xf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1/
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv
make && make install
cd /usr/local
mkdir tomcat
cd tomcat
mkdir bin
cd bin
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-connectors/native/1.2.21/source/tomcat-native-1.2.21-src.tar.gz
tar xf tomcat-native-1.2.21-src.tar.gz
cd /usr/local/tomcat/bin/tomcat-native-1.2.21-src/native
./configure --with-apr=/usr/local/apr --with-java-home=/opt/jdk
make && make install
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
export LD_RUN_PATH=$LD_RUN_PATH:/usr/local/apr/lib' >> /etc/profile
source /etc/profile
说明
- 安装后可以看看/usr/local/apr/lib下有没有安装好的类库比如 apr aprutil apr iconv tomcat-native等
- LD_LIBRARY_PATH 配置是lunux配置库lib的配置
Springboot 配置tomcat connector使用APR模式
package com.tapmobi.xrtb.conf;
import com.tapmobi.xrtb.routers.bidding.BiddingController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
@Configuration
@EnableConfigurationProperties(APRTomcatProperties.class)
public class APRconf {
private static final Logger logger = LoggerFactory.getLogger(BiddingController.class);
@Bean
public ServletWebServerFactory servletWebServerFactory(APRTomcatProperties configProperties) {
APRTomcatProperties.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) {
logger.error("Tomcat connector 配置异常", e);
continue;
}
}
});
return tomcatServletWebServerFactory;
}
}
package com.tapmobi.xrtb.conf;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(value = APRTomcatProperties.PREFIX)
public class APRTomcatProperties {
public static final String PREFIX = "aprio";
private Tomcat tomcat;
public Tomcat getTomcat() {
return tomcat;
}
public void setTomcat(Tomcat tomcat) {
this.tomcat = tomcat;
}
public static class Tomcat {
/**
* 连接超时,单位ms
*/
private Integer connectionTimeout = 20000;
/**
* 接收连接线程数量,参考cpu核数
*/
private Integer acceptorThreadCount = 8;
/**
* 最小监听线程
*/
private Integer minSpareThreads = 5;
/**
* 最大监听线程
* 同时相应客户请求最大值
*/
private Integer maxSpareThreads = 200;
/**
* 最大排队数
*/
private Integer acceptCount = 200;
/**
* 最大连接数
*/
private Integer maxConnections = 800;
/**
* 最大线程数
*/
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;
}
}
}
网友评论