今天小戚发出一封邮件,说由于线上系统中tomcat的链接超时(connectionTimeout)设置成60ms,形成第三方访问公司的服务,老是502异常。html
这个设置在$tomcat/conf/server.xml中web
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="2000" disableUploadTimeout="true" />
http://tomcat.apache.org/tomcat-5.5-doc/config/http.htmlapache
connectionTimeout :浏览器
The number of milliseconds this Connector will wait, after accepting a connection, for the request URI line to be presented. The default value is 60000 (i.e. 60 seconds).tomcat
以上中文是通过以下分析过程得出的。服务器
写了一个servlet,doGet先sleep一段时间,再写一个输出,直接用浏览器访问。cookie
通过测试,发现和这个时间无关。session
写客户端模拟超时,多是由于API直接实现到提交URI了,另外还怀疑底层有自动保持链接的动做,反正怎么Sleep都不超时,得换个写法了。app
明天连上TCPMon看看后台有没有自动保持链接的动做。socket
def sURL='http://localhost:8080/index.jsp'
URL url = new URL(sURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
直接用telnet连上tomcat,若是什么都不输入,socket很快回断开,输入完整GET。。。,可以得到输出。若是不保持输入,则链接很快会断开。若是一直不停输入,链接继续保持。
$ telnet localhost 8080
GET /index.jsp HTTP/1.1
Accept-Language: zh-cn
Connection: Keep-Alive
Host: 192.168.0.53
Content-Length: 36
仍是2s超时,睡一秒可以正确得到输出,睡2秒,输出为空。
如下是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
}
用Groovy写测试代码真舒服,呵呵。
JBoss使用Tomcat做为Web容器,所以在JBoss中对于Web容器的配置也相似于在Tomcat中的配置,主要就是对于server.xml文件的编辑,在JBoss 5.x中,这个文件位于${JBOSS.HOME}\server\${confifure}\deploy\jbossweb.sar下,其中configure的值能够是all,
default,web,standard, minimal等。下面的代码展现了一个JBoss default配置下的server.xml,因为篇幅缘由,将其中的注释都已经去掉了。
<Server> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Service name="jboss.web"> <Connector protocol="HTTP/1.1" port="8080" address="${jboss.bind.address}" connectionTimeout="20000" redirectPort="8443" compression="on" compressionMinSize="1" compressableMimeType="text/html,text/xml" /> <Engine name="jboss.web" defaultHost="localhost"> <Realm className="org.jboss.web.tomcat.security.JBossWebRealm" certificatePrincipal="org.jboss.security.auth.certs.SubjectDNMapping" allRolesMode="authOnly" /> <Host name="localhost"> <Valve className="org.jboss.web.tomcat.service.jca.CachedConnectionValve" cachedConnectionManagerObjectName="jboss.jca:service=CachedConnectionManager" transactionManagerObjectName="jboss:service=TransactionManager" /> </Host> </Engine> </Service> </Server>
在上面的配置文件中,Server是根节点,一个Server就表明一个Servlet容器,所以在server.xml中,这个节点只能有一个,在Server节点下,能够存在一个或者多个Service节点。
一个Service节点表明了一个或者多个Connector和一个Engine,而Connector和Engine是在server.xml中两个重要的配置项,Connector的主要功能是接受、响应用户请求。经常使用的Connector有HTTP/1.1 Connector和AJP Connector,HTTP/1.1 Connector主要用于处理用户的HTTP请求,须要注意的是虽然它名叫HTTP/1.1 Connector,可是是彻底兼容HTTP/1.0协议的。AJP Connector主要使用AJP协议和Web
Connector通讯,一般用于集群中。
HTTP/1.1 Connector的实例监听在用户配置的端口上,当应用服务器启动时,HTTP/1.1 Connector负责建立若干线程,用于处理用户请求,建立的线程数目取决于用户配置的minThreads值,默认为5,当有更多的用户请求到来时,HTTP/1.1 Connector将会建立更多的线程用于处理请求,建立线程的最大值由maxThreads定义,默认值为20,当全部的线程都在忙于处理用户请求时,新到来的请求将会放入HTTP/1.1 Connector建立的Socket队列中,队列的长度由acceptCount属性定义,当等待队列也被占用满了,新来的用户请求将会收到connection
refused错误。
全部的Connector提供的配置项(不彻底版scheme, isSecure, xpoweredBy, useIPVHosts ):
Http/1.1 Connector提供的配置项: