µ±Ç°Î»ÖÃ: > Linux·þÎñÆ÷ > Tomcat >

Tomcatѧϰ֮Connector

ʱ¼ä:2014-12-30 13:26À´Ô´:linux.it.net.cn ×÷Õß:ITÍø

ÈçÏÂͼËùʾ£¬Tomcat·þÎñÆ÷Ö÷ÒªÓÐÁ½´óºËÐÄÄ£¿é×é³É£ºÁ¬½ÓÆ÷ºÍÈÝÆ÷£¬±¾½ÚÖ»·ÖÎöÁ¬½ÓÆ÷µÄʵÏÖ¡£

Á¬½ÓÆ÷Ö÷ÒªÊǽÓÊÕÓû§µÄÇëÇó£¬È»ºó·â×°ÇëÇ󴫵ݸøÈÝÆ÷´¦Àí£¬tomcatÖÐĬÈϵÄÁ¬½ÓÆ÷ÊÇCoyote.Ê×ÏÈÀ´¿´Á¬½ÓÆ÷µÄÀàͼ£º

protocol

ÎÒÃÇ·¢ÏÖÕâ¸öÀàÀïÃæÓкܶàÓëprotocolÓйصÄÊôÐԺͷ½·¨£¬tomcatÖÐÖ§³ÖÁ½ÖÖЭÒéµÄÁ¬½ÓÆ÷£ºHTTP/1.1ÓëAJP/1.3£¬²é¿´tomcatµÄÅäÖÃÎļþserver.xml¿ÉÒÔ¿´µ½ÈçÏÂÅäÖãº

[java] view plaincopyprint?
 
  1. <Connector port="8080" protocol="HTTP/1.1"   
  2.                connectionTimeout="20000"   
  3.                redirectPort="8443" URIEncoding="utf-8"/>  
  4.   
  5. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  
HTTP/1.1ЭÒ鸺Ôð½¨Á¢HTTPÁ¬½Ó£¬webÓ¦ÓÃͨ¹ýä¯ÀÀÆ÷·ÃÎÊtomcat·þÎñÆ÷ÓõľÍÊÇÕâ¸öÁ¬½ÓÆ÷£¬Ä¬ÈϼàÌýµÄÊÇ8080¶Ë¿Ú£»

AJP/1.3ЭÒ鸺ÔðºÍÆäËûHTTP·þÎñÆ÷½¨Á¢Á¬½Ó£¬¼àÌýµÄÊÇ8009¶Ë¿Ú£¬±ÈÈçtomcatºÍapache»òÕßiis¼¯³ÉʱÐèÒªÓõ½Õâ¸öÁ¬½ÓÆ÷¡£

ЭÒéÉÏÓÐÈýÖÖ²»Í¬µÄʵÏÖ·½Ê½£ºJIO¡¢APR¡¢NIO¡£

JIO(java.io)£ºÓÃjava.io´¿JAVA±àдµÄTCPÄ£¿é£¬ÕâÊÇtomcatĬÈÏÁ¬½ÓÆ÷ʵÏÖ·½·¨£»

APR(Apache Portable Runtime)£ºÓÐCÓïÑÔºÍJAVAÁ½ÖÖÓïÑÔʵÏÖ£¬Á¬½ÓApache httpd Web·þÎñÆ÷µÄÀà¿âÊÇÔÚCÖÐʵÏֵģ¬Í¬Ê±ÓÃAPR½øÐÐÍøÂçͨÐÅ£»

NIO(java.nio)£ºÕâÊÇÓô¿Java±àдµÄÁ¬½ÓÆ÷(Conector)µÄÒ»ÖÖ¿ÉÑ¡·½·¨¡£¸ÃʵÏÖÓÃjava.nioºËÐÄJavaÍøÂçÀàÒÔÌṩ·Ç×èÈûµÄTCP°üÌØÐÔ¡£

ProtocolHandler½Ó¿ÚÊǶÔÕâЩЭÒéµÄ³éÏó£¬ÆäÀà²ã´Î½á¹¹Í¼ÈçϺôËùʾ£º

Ç°ÃæÌáµ½tomcatĬÈϲÉÓõÄÊÇHttp11Protocol,ҪôҪÔõô¸ü»»Ä¬ÈϵÄprotocolHandlerÄØ£¬ÏÈ¿´¿´setProtocol·½·¨µÄÔ´Â룺

[java] view plaincopyprint?
 
  1. public void setProtocol(String protocol) {  
  2.   
  3.     if (AprLifecycleListener.isAprAvailable()) {  
  4.         if ("HTTP/1.1".equals(protocol)) {  
  5.             setProtocolHandlerClassName  
  6.                 ("org.apache.coyote.http11.Http11AprProtocol");  
  7.         } else if ("AJP/1.3".equals(protocol)) {  
  8.             setProtocolHandlerClassName  
  9.                 ("org.apache.coyote.ajp.AjpAprProtocol");  
  10.         } else if (protocol != null) {  
  11.             setProtocolHandlerClassName(protocol);  
  12.         } else {  
  13.             setProtocolHandlerClassName  
  14.                 ("org.apache.coyote.http11.Http11AprProtocol");  
  15.         }  
  16.     } else {  
  17.         if ("HTTP/1.1".equals(protocol)) {  
  18.             setProtocolHandlerClassName  
  19.                 ("org.apache.coyote.http11.Http11Protocol");  
  20.         } else if ("AJP/1.3".equals(protocol)) {  
  21.             setProtocolHandlerClassName  
  22.                 ("org.apache.coyote.ajp.AjpProtocol");  
  23.         } else if (protocol != null) {  
  24.             setProtocolHandlerClassName(protocol);  
  25.         }  
  26.     }  
  27. }  
´ÓÒÔÉÏ´úÂë¿ÉÒÔ¿´³öÖ»Òªprotocol²»ÎªHTTP/1.1Ò²²»ÎªAJP/1.3¾Í»áµ÷ÓÃsetProtocolHandlerClassNameÀ´ÉèÖÃprotocolHandler£¬Ò²¾ÍÊÇ˵ҪÌ滻ĬÈϵÄprotocolHandler£¬Ö»ÐèÒªÐÞ¸Äserver.xmlÎļþConnectorÖеÄprotocolÊôÐÔ¼´¿É£¡

 

 

Service

Á¬½ÓÆ÷ºÍÈÝÆ÷Ò»Æð²ÅÄܶÔÍâÌṩ·þÎñ£¬ServiceÀïÃæ°üº¬ÁËÒ»¸öÈÝÆ÷ºÍ¶à¸öÁ¬½ÓÆ÷£¬Á¬½ÓÆ÷ÊÇÔõô¼ÓÈëµ½ServiceÖеģ¬¿ÉÒÔ¿´Ò»ÏÂStandardServiceÖеĴúÂ룺
[html] view plaincopyprint?
 
  1. public void addConnector(Connector connector) {  
  2.   
  3.     synchronized (connectors) {  
  4.         connector.setService(this);  
  5.         Connector results[] = new Connector[connectors.length + 1];  
  6.         System.arraycopy(connectors, 0, results, 0, connectors.length);  
  7.         results[connectors.length] = connector;  
  8.         connectors = results;  
  9.   
  10.         if (getState().isAvailable()) {  
  11.             try {  
  12.                 connector.start();  
  13.             } catch (LifecycleException e) {  
  14.                 log.error(sm.getString(  
  15.                         "standardService.connector.startFailed",  
  16.                         connector), e);  
  17.             }  
  18.         }  
  19.   
  20.         // Report this property change to interested listeners  
  21.         support.firePropertyChange("connector", null, connector);  
  22.     }  
  23.   
  24. }  
StandardServiceÖл¹ÓÐsetContainer·½·¨£¬ÕýÊÇService°ÑÈÝÆ÷ºÍÁ¬½ÓÆ÷¹ØÁªÔÚÒ»ÆðµÄ

mapperListener

ÔÚÁ¬½ÓÆ÷³õʼ»¯µÄʱºò»á³õʼ»¯mapperListener£¬mapperListenerÔÚ³õʼ»¯µÄʱºò»áµ÷ÓÃmapper¶ÔÏóµÄaddXXX·½·¨

Mapper¶ÔÏóÔÚtomcatÖдæÔÚÓÚÁ½¸öµØ·½:

1¡¢Ã¿¸öcontextÈÝÆ÷¶ÔÏóÖУ¬ËüÖ»¼Ç¼ÁË´ËcontextÄÚ²¿µÄ·ÃÎÊ×ÊÔ´ÓëÏà¶ÔÓ¦µÄwrapper×ÓÈÝÆ÷µÄÓ³É䣻

2¡¢connectorÄ£¿éÖУ¬ÕâÊÇtomcatÈ«¾ÖµÄ±äÁ¿£¬Ëü¼Ç¼ÁËÒ»¸öÍêÕûµÄÓ³Éä¶ÔÓ¦¹Øϵ£¬¼´¸ù¾Ý·ÃÎʵÄÍêÕûURLÈçºÎ¶¨Î»µ½ÄĸöhostϵÄÄĸöcontextµÄÄĸöwrapperÈÝÆ÷¡£
ServletÖÐforwardÌøת»áÓõ½µÚÒ»ÖÖmapper,Ò²¾ÍÊÇ˵forwardÊÇ·þÎñÆ÷ÄÚ²¿µÄÖض¨Ïò¡£

³õʼ»¯ÓëÆô¶¯

ConnectorµÄ³õʼ»¯¹ý³ÌÈçÏ£º
1¡¢Tomcat³õʼ»¯Ê±»áµ÷ÓÃBootstrapµÄLoad·½·¨£¬ÕâÀï»á½âÎöXMLÎļþ£¬Digester½âÎöµÄ¹ý³ÌÖУ¬»áµ÷ÓÃConnectorµÄ¹¹Ôì·½·¨
[html] view plaincopyprint?
 
  1. public Connector(String protocol) {  
  2.        setProtocol(protocol);  
  3.        // Instantiate protocol handler  
  4.        try {  
  5.            Class<?> clazz = Class.forName(protocolHandlerClassName);  
  6.            this.protocolHandler = (ProtocolHandler) clazz.newInstance();  
  7.        } catch (Exception e) {  
  8.            log.error(sm.getString(  
  9.                    "coyoteConnector.protocolHandlerInstantiationFailed"), e);  
  10.        }  
  11.    }  
Õâ¸ö¹¹Ôì·½·¨Ê×ÏÈÉèÖÃprotocol£¬È»ºó³õʼ»¯protocolhandler
2¡¢½ô½Ó×ųõʼ»¯Server,Ò»¸öServer¿ÉÄÜÓжà¸öService£¬ÔÚ³õʼ»¯ServerµÄ¹ý³ÌÖлá³õʼ»¯¶à¸öService.ServiceÓÉÒ»¸öÈÝÆ÷ºÍ¶à¸öÁ¬½ÓÆ÷×é³É£¬»áÏȳõʼ»¯ÈÝÆ÷£¬È»ºó³õʼ»¯»¯Á½¸öÁ¬½ÓÆ÷£¬Á¬½ÓÆ÷µÄ³õʼ»¯Êǵ÷ÓõÄConnectorµÄinitInternal·½·¨
[html] view plaincopyprint?
 
  1. protected void initInternal() throws LifecycleException {  
  2.   
  3.     super.initInternal();  
  4.   
  5.     // Initialize adapter  
  6.     adapter = new CoyoteAdapter(this);  
  7.     protocolHandler.setAdapter(adapter);  
  8.   
  9.     // Make sure parseBodyMethodsSet has a default  
  10.     if( null == parseBodyMethodsSet ) {  
  11.         setParseBodyMethods(getParseBodyMethods());  
  12.     }  
  13.   
  14.     try {  
  15.         protocolHandler.init();  
  16.     } catch (Exception e) {  
  17.         throw new LifecycleException  
  18.             (sm.getString  
  19.              ("coyoteConnector.protocolHandlerInitializationFailed"), e);  
  20.     }  
  21.   
  22.     // Initialize mapper listener  
  23.     mapperListener.init();  
  24. }  
(1)Ê×ÏÈΪprotocolHandlerÉèÖÃÒ»¸öadapter£¬È»ºó³õʼ»¯protocolHandler
(2)³õʼ»¯mapperListener£¬É¶¶¼Ã»×ö£¬Ö»Êǵ÷ÓÃÁ˸¸Àà(LifecycleMBeanBase)µÄinitInternal·½·¨!
ÓÉÓÚTomcatµÄÉúÃüÖÜÆÚ¿ØÖÆ£¬Á¬½ÓÆ÷µÄÆô¶¯¹ý³ÌºÍ³õʼ»¯¹ý³Ì¼¸ºõÒ»Ñù£¬Ò²ÊÇÓÉCatalinaµÄstart·½·¨¿ªÊ¼£¬ServerÆô¶¯£¬ServiceÆô¶¯£¬ContainerÆô¶¯£¬ConnectorÆô¶¯¡£ConnectorÆô¶¯µ÷ÓÃÁËËüµÄstartInternal·½·¨£¬Õâ¸ö·½·¨Ö»×öÁËÁ½¼þÊ£ºÆô¶¯protocolHandlerºÍmapperListener£¬Á¬½ÓÆ÷µÄÆô¶¯¹ý³Ì¾ÍÊÇÕâÑù!ÏÂÃæ·Ö±ðÀ´¿´¿´protocolHandlerºÍmapperListenerÆô¶¯Ê±¶¼×öÁËÄÄЩÊ£¿
protocolHandlerµÄ³õʼ»¯ÊÇÔÚÆ丸ÀàAbstractProtocolµÄstart·½·¨ÖÐÍê³ÉµÄ£¬
[html] view plaincopyprint?
 
  1. public void start() throws Exception {  
  2.     if (getLog().isInfoEnabled())  
  3.         getLog().info(sm.getString("abstractProtocolHandler.start",  
  4.                 getName()));  
  5.     try {  
  6.         endpoint.start();  
  7.     } catch (Exception ex) {  
  8.         getLog().error(sm.getString("abstractProtocolHandler.startError",  
  9.                 getName()), ex);  
  10.         throw ex;  
  11.     }  
  12. }  
µ÷ÓÃÁËendpointµÄstart·½·¨£¬Õâ¸ö·½·¨ÀïÃæ×öÁËÒÔϼ¸¼þÊ£º
(1)ÉèÖýÓÊÕÏß³ÌÊýºÍ×î´óÁ¬½ÓÊý£¬´´½¨socket
(2)´´½¨Ï̳߳Ø,Æô¶¯¼àÌýµÄÏ̼߳àÌýÓû§ÇëÇó
(3)Æô¶¯Ò»¸öÏß³ÌÀ´¸ºÔðÒì²½ÇëÇó
mapperListenerÒ²¼Ì³ÐÁËLifecycleMBeanBaseÀ࣬ҲÊÇÊÜÉúÃüÖÜÆÚ¿ØÖƵġ£ËùÒÔËüµÄÆô¶¯ÊÇÔÚstartInternal·½·¨ÖÐÍê³ÉµÄ
[html] view plaincopyprint?
 
  1. public void startInternal() throws LifecycleException {  
  2.   
  3.        setState(LifecycleState.STARTING);  
  4.   
  5.        // Find any components that have already been initialized since the  
  6.        // MBean listener won't be notified as those components will have  
  7.        // already registered their MBeans  
  8.        findDefaultHost();  
  9.   
  10.        Engine engine = (Engine) connector.getService().getContainer();  
  11.        addListeners(engine);  
  12.   
  13.        Container[] conHosts = engine.findChildren();  
  14.        for (Container conHost : conHosts) {  
  15.            Host host = (Host) conHost;  
  16.            if (!LifecycleState.NEW.equals(host.getState())) {  
  17.                // Registering the host will register the context and wrappers  
  18.                registerHost(host);  
  19.            }  
  20.        }  
  21.    }  
(1)×¢²áÒѳõʼ»¯µÄ×é¼þ
(2)Ϊ¸÷ÖÖÈÝÆ÷Ìí¼Ó¼àÌýÆ÷
(3)Ϊ¸÷ÖÖÈÝÆ÷½¨Á¢Ó³Éä¹Øϵ
(ÔðÈα༭£ºIT)
------·Ö¸ôÏß----------------------------
À¸Ä¿Áбí
ÍƼöÄÚÈÝ