server.xml文件中的Tomcat连接器定义

时间:2015-10-26 08:57:40

标签: java tomcat7

我想了解server.xml文件中Tomcat的连接器定义。以下配置有什么区别:

    <Connector port="8080"
               protocol="HTTP/1.1"
               socket.directBuffer="true"
               redirectPort="8083"
               keyAlias="tomcat"
               enableLookups="true"
               connectionTimeout="60000"
               URIEncoding="UTF-8"
               server="NA"
               minSpareThreads="2"
               maxThreads="10"
               acceptCount="10"/>

    <Connector address="127.0.0.1"
               port="8080"
               protocol="HTTP/1.1"
               socket.directBuffer="true"
               redirectPort="8083"
               keyAlias="tomcat"
               enableLookups="false"
               connectionTimeout="60000"
               URIEncoding="UTF-8"
               server="NA"
               minSpareThreads="2"
               maxThreads="10"
               acceptCount="10"/>

    <Connector address="::1"
               port="8080"
               protocol="HTTP/1.1"
               redirectPort="8083"
               enableLookups="false"
               connectionTimeout="60000"
               URIEncoding="UTF-8"
               server="NA"
               minSpareThreads="2"
               maxThreads="5"
               acceptCount="10"/>

根据Tomcat 8 connectors documentation,仅当服务器具有多个IP地址时才应使用address属性。通常,此配置会在Tomcat启动期间导致下一个2异常(因为所有3个连接器都尝试侦听相同的IP和端口):

26/10/2015 10:07:56 SEVERE org.apache.coyote.AbstractProtocol init Failed to initialize end point associated with ProtocolHandler ["http-nio-127.0.0.1-8080"]
java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:437)
    at sun.nio.ch.Net.bind(Net.java:429)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:743)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:457)
    at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:120)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:960)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardService.initInternal(StandardService.java:567)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:851)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:576)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:599)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:310)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:484)

26/10/2015 10:07:56 SEVERE org.apache.coyote.AbstractProtocol init Failed to initialize end point associated with ProtocolHandler ["http-nio-0:0:0:0:0:0:0:1-8080"]
java.net.BindException: Address already in use
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:437)
    at sun.nio.ch.Net.bind(Net.java:429)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:743)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:457)
    at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:120)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:960)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardService.initInternal(StandardService.java:567)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:851)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:576)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:599)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:310)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:484)

有什么理由可以像上面那样定义3个连接器吗?

1 个答案:

答案 0 :(得分:0)

除非每个绑定也指向特定的IP地址,否则不能多次绑定到同一个TCP端口。您的第一个Connector未通过此测试。另外两个指定address=,这使它们有效。

  

根据Tomcat 8连接器文档,仅当服务器具有多个IP地址时才应使用address属性

实际上并没有这么说,但它并不相关,因为每个主机至少有两个地址:127.0.0.1及其真实IP地址。在您的情况3中,因为您还有::1,IPv6环回地址。