以下内容来源于互联网,具体出处不详
据说服务器运行TOMCAT+JDK环境能负载到动态1W的并发,贴上他的配置,以后有机会在测试!
java 环境配置:
export JAVA_OPTS="-server -Xms8g -Xmx8g -Xss128k -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=31"
sysctl.conf的配置:
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 180000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 327680
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.ip_local_port_range = 1024 650000
net.ipv4.tcp_keepalive_time = 5
net.ipv4.tcp_keepalive_probes=2
net.ipv4.tcp_keepalive_intvl=2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recyle = 1
net.ipv4.tcp_max_syn_backlog=8192
net.ipv4.tcp_no_metrics_save = 0
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
fs.file-max = 9553600
net.ipv4.netfilter.ip_conntrack_max = 655360
net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 60
net.ipv4.tcp_max_tw_buckets = 60000
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
tomcat的SERVER.XML配置:
server.conf 配置
<Connector port="8188" protocol="HTTP/1.1"
maxThreads="30000"
minSpareThreads="512"
maxSpareThreads="2048"
enableLookups="false"
redirectPort="8443" acceptCount="35000"
debug="0"
connectionTimeout="40000"
disableUploadTimeout="true"
URIEncoding="UTF-8" />
参数说明:
connectionTimeout - 网络连接超时,单位:毫秒。设置为0表示永不超时,这样设置有隐患的。通常可设置为30000毫秒。
keepAliveTimeout - 长连接最大保持时间(毫秒)。此处为15秒。
maxKeepAliveRequests - 最大长连接个数(1表示禁用,-1表示不限制个数,默认100个。一般设置在100~200之间)
maxHttpHeaderSize - http请求头信息的最大程度,超过此长度的部分不予处理。一般8K。
URIEncoding - 指定Tomcat容器的URL编码格式。
acceptCount - 指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理,默认为10个。
disableUploadTimeout - 上传时是否使用超时机制
enableLookups - 是否反查域名,取值为:true或false。为了提高处理能力,应设置为false
bufferSize - defines the size (in bytes) of the buffer to be provided for input streams created by this connector. By default, buffers of 2048 bytes are provided.
maxSpareThreads - 最大空闲连接数,一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程 The default value is 50.
maxThreads - 最多同时处理的连接数,Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。 minSpareThreads - 最小空闲线程数,Tomcat初始化时创建的线程数 .
minProcessors - 最小空闲连接线程数,用于提高系统处理性能,默认值为10。
maxProcessors - 最大连接线程数,即:并发处理的最大请求数,默认值为75。
修改启动时内存参数
window下, 在catalina.bat最前面:
set JAVA_OPTS=-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m
一定加在catalina.bat最前面。
linux下,在catalina.sh最前面增加:
JAVA_OPTS="-XX:PermSize=64M -XX:MaxPermSize=128m -Xms512m -Xmx1024m -Duser.timezone=Asia/Shanghai"
注意:前后二者区别,有无set,有无双引号。
写Socket做测试
以下是Groovy代码
content ='''GET /index.jsp HTTP/1.1
Accept-Language: zh-cn
Connection: Keep-Alive
Host: 192.168.0.53
Content-Length: 36
'''
def sleepTime=1000
Socket socket = new Socket('localhost',8080)
println 'is keep alive? ' + socket.getKeepAlive()
println 'sleep ' + sleepTime + ' ms.'
Thread.sleep(sleepTime)
println 'is closed? ' + socket.isClosed()
println 'sleep ' + sleepTime + ' ms.'
//Thread.sleep(sleepTime)
println 'write socket begin======'
writeStream(content, socket.getOutputStream())
println 'read socket begin======'
println readStream(socket.getInputStream())[0..300]
void writeStream(content, stream) {
OutputStream buf = new BufferedOutputStream(stream);
OutputStreamWriter out = new OutputStreamWriter(buf, "UTF-8");
out.write(content)
out.flush();
print content
//out.close();
}
String readStream(stream){
String sResult=''
byte[] buffer = new byte[1024];
int readCount = stream.read(buffer);
while (readCount != -1) {
sResult += new String(buffer, 0,
readCount, "utf-8");
readCount = stream.read(buffer);
}
stream.close()
return sResult
}