{"id":10008190,"date":"2023-08-29T15:02:19","date_gmt":"2023-08-29T07:02:19","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008190"},"modified":"2023-08-29T15:02:19","modified_gmt":"2023-08-29T07:02:19","slug":"%e4%b8%80%e6%ac%a1%e8%af%b7%e6%b1%82%e5%9c%a8tomcat%e6%ba%90%e7%a0%81%e4%b8%ad%e7%9a%84%e8%bd%ae%e8%bd%ac%ef%bc%88%e5%9f%ba%e7%a1%80%e7%af%87%ef%bc%89","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008190","title":{"rendered":"\u4e00\u6b21\u8bf7\u6c42\u5728Tomcat\u6e90\u7801\u4e2d\u7684\u8f6e\u8f6c\uff08\u57fa\u7840\u7bc7\uff09"},"content":{"rendered":"<h3>\u524d\u8a00<\/h3>\n<p>Tomcat\u4f5c\u4e3aJava\u5f00\u53d1\u8005\u63a5\u89e6\u8fc7\u6700\u91cd\u8981\u7684web\u5bb9\u5668\uff0c\u5728\u542f\u52a8\u548c\u5904\u7406\u8bf7\u6c42\u8fc7\u7a0b\u4e2d\u505a\u4e86\u6d77\u91cf\u7684\u4e8b\u60c5\uff0c\u521d\u7ea7\u5f00\u53d1\u8005\u5f88\u5c11\u5173\u5fc3\uff0c\u4f7f\u7528SpringMvc\u4e4b\u7c7b\u4e0a\u5c42\u6846\u67b6\u4e00\u5e26\u800c\u8fc7\uff0c\u7136\u800c\u8fd9\u4e9b\u90e8\u5206\u662fJava\u548c\u7f51\u7edc\u96c6\u5927\u6210\u4e4b\u4f5c\uff0c\u7b14\u8005\u8981\u5e26\u7740\u5927\u5bb6\u8d70\u4e00\u904d\u4e00\u6b21\u8bf7\u6c42\uff0c\u52a0\u6df1tomcat\u7684\u8ba4\u77e5\u3002\u6700\u597d\u5148\u8c03\u8bd5\u597dTomcat\u6e90\u7801<\/p>\n<h2>Tomcat\u57fa\u7840\u67b6\u6784<\/h2>\n<h3>BootStrap\u548cCatalina<\/h3>\n<h4>BootStrap<\/h4>\n<p>BootStrap\u5c31\u662fTomcat\u7684main\u51fd\u6570\u6240\u5728\u4f4d\u7f6e\uff0c\u5728\u4f7f\u7528\u8fc7\u7a0b\u4e2d\u6267\u884c\u811a\u672ccatlina.sh\u6216\u8005bat\u6587\u4ef6\u5373\u53ef\u6267\u884cjava\u547d\u4ee4\u5e76\u8c03\u7528BootStrap\u7684main\u51fd\u6570\u5b9e\u73b0tomcat\u7684\u542f\u52a8<\/p>\n<pre><code> public static void main(String args[]) {\n if (daemon == null) {\n                \/\/ Don't set daemon until init() has completed\n                Bootstrap bootstrap = new Bootstrap();\n                try {\n                    bootstrap.init();\n                } catch (Throwable t) {\n                    handleThrowable(t);\n                    t.printStackTrace();\n                    return;\n                }\n                daemon = bootstrap;\n            } else {\n                \/\/ When running as a service the call to stop will be on a new\n                \/\/ thread so make sure the correct class loader is used to\n                \/\/ prevent a range of class not found exceptions.\n                Thread.currentThread().setContextClassLoader(daemon.catalinaLoader);\n            }\n......\n}<\/code><\/pre>\n<h4>Catalina<\/h4>\n<p>BootStrap\u662f\u670d\u52a1\u5668\u7684\u5165\u53e3\uff0c \u4f1a\u901a\u8fc7start\u3001stop\u3001stopServer\u3001stopServer\u7b49\u53cd\u5c04\u8c03\u7528Catalina\u7684\u5bf9\u5e94\u65b9\u6cd5<\/p>\n<pre><code>public void stopServer() throws Exception {\n\n        Method method =\n            catalinaDaemon.getClass().getMethod(&quot;stopServer&quot;, (Class []) null);\n        method.invoke(catalinaDaemon, (Object []) null);\n    }<\/code><\/pre>\n<h3>Tomcat\u7684\u6838\u5fc3\u914d\u7f6e\u6587\u4ef6server.xml<\/h3>\n<p>\u5148\u6765\u770b\u4e00\u4e0btomcat\u7684\u5bb9\u5668\u7ed3\u6784<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"543\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9rwt\" alt=\"image.png\" title=\"image.png\"><br \/>tomcat\u5bb9\u5668\u6709\u591a\u5c42\uff0cServer\u3001service\u3001Engine\u3001Host\u3001Context\u7b49\uff0c\u56fe\u4e2d\u6709\u7701\u7565<br \/>\u6bd4\u5982\u4e00\u4e2a\u540d\u79f0\u4e3amytomcat\u7684web\u9879\u76ee\uff0c\u5bf9\u5e94\u5230Container\u90e8\u5206\uff0cContext\u540d\u79f0\u5c31\u662fmytomcat<br \/>\u518d\u770b\u4e00\u4e0bTomcat\u7684\u914d\u7f6e\u6587\u4ef6server.xml<\/p>\n<pre><code>\n&lt;Server port=&quot;8005&quot; shutdown=&quot;SHUTDOWN&quot;&gt;\n    &lt;Listener className=&quot;org.apache.catalina.startup.VersionLoggerListener&quot; \/&gt;\n    &lt;Listener SSLEngine=&quot;on&quot;\n        className=&quot;org.apache.catalina.core.AprLifecycleListener&quot; \/&gt;\n    &lt;Listener className=&quot;org.apache.catalina.core.JasperListener&quot; \/&gt;\n    &lt;Listener className=&quot;org.apache.catalina.core.JreMemoryLeakPreventionListener&quot; \/&gt;\n    &lt;Listener className=&quot;org.apache.catalina.mbeans.GlobalResourcesLifecycleListener&quot; \/&gt;\n    &lt;Listener className=&quot;org.apache.catalina.core.ThreadLocalLeakPreventionListener&quot; \/&gt;\n\n    &lt;GlobalNamingResources&gt;\n        &lt;Resource auth=&quot;Container&quot; description=&quot;User database that can be updated and saved&quot;\n            factory=&quot;org.apache.catalina.users.MemoryUserDatabaseFactory&quot; name=&quot;UserDatabase&quot;\n            pathname=&quot;conf\/tomcat-users.xml&quot; type=&quot;org.apache.catalina.UserDatabase&quot; \/&gt;\n    &lt;\/GlobalNamingResources&gt;\n\n    &lt;Service name=&quot;Catalina&quot;&gt;\n\n        &lt;Connector URIEncoding=&quot;UTF-8&quot; connectionTimeout=&quot;20000&quot;\n            port=&quot;80&quot; protocol=&quot;HTTP\/1.1&quot; redirectPort=&quot;8443&quot; \/&gt;\n        &lt;Connector port=&quot;8009&quot; protocol=&quot;AJP\/1.3&quot; redirectPort=&quot;8443&quot; \/&gt;\n\n        &lt;Engine defaultHost=&quot;localhost&quot; name=&quot;Catalina&quot;&gt;\n\n            &lt;Realm className=&quot;org.apache.catalina.realm.LockOutRealm&quot;&gt;\n                &lt;Realm className=&quot;org.apache.catalina.realm.UserDatabaseRealm&quot;\n                    resourceName=&quot;UserDatabase&quot; \/&gt;\n            &lt;\/Realm&gt;\n\n            &lt;Host appBase=&quot;webapps&quot; autoDeploy=&quot;true&quot; name=&quot;localhost&quot;\n                unpackWARs=&quot;true&quot;&gt;\n\n                &lt;Valve className=&quot;org.apache.catalina.valves.AccessLogValve&quot;\n                    directory=&quot;logs&quot; pattern=&quot;%h %l %u %t &quot;%r&quot; %s %b&quot; prefix=&quot;localhost_access_log.&quot;\n                    suffix=&quot;.txt&quot; \/&gt;\n\n            &lt;\/Host&gt;\n        &lt;\/Engine&gt;\n    &lt;\/Service&gt;<\/code><\/pre>\n<p>\u5728\u542f\u52a8tomcat\u7684\u65f6\u5019\uff0ccatalina\u7684load\u65b9\u6cd5\uff0c\u4f1a\u8bfb\u53d6server.xml\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u901a\u8fc7Digester\u7ec4\u4ef6\u628aserver.xml\u89e3\u6790\u6210Server\u5bf9\u8c61\uff0c\u5e76\u4e14\u628aServer\u53d8\u6210Catalina\u7684\u6210\u5458\u53d8\u91cf\u3002<\/p>\n<pre><code>\u6211\u4eec\u770b\u4e00\u4e0bcatalina\u7684load\u65b9\u6cd5\u4e2ddigester.parse()\u65b9\u6cd5\u6267\u884c\u540e\u7684\u5bb9\u5668\u4ee3\u7801\u7ed3\u6784<\/code><\/pre>\n<p><img decoding=\"async\" width=\"723\" height=\"568\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9rxn\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u8fd9\u6837\u7684\u8bdd\uff0cTomcat\u7684\u5bb9\u5668\u7684\u5c42\u7ea7\u7ed3\u6784\u5c31\u5efa\u7acb\u8d77\u6765\u4e86\u3002<\/p>\n<h3>\u7f51\u7edc\u5904\u7406\u90e8\u5206<\/h3>\n<h4>Connector<\/h4>\n<p>tomcat\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u534f\u8bae\uff0c\u7edf\u4e00\u4f7f\u7528Connector\u7ec4\u4ef6\u6765\u5b9e\u73b0\uff0c\u5728tomcat\u4e2d\u4e00\u4e2aService\u53ef\u4ee5\u5bf9\u5e94\u591a\u4e2aConnector<br \/><img decoding=\"async\" width=\"722\" height=\"282\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9rAj\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h4>ProtocolHandler<\/h4>\n<p>connector\u4e2d\u6709\u4e2a\u5f88\u91cd\u8981\u7684\u6210\u5458\u53d8\u91cfprotocolHandler\uff0c \u7528\u6765\u5b9e\u73b0\u591a\u79cd\u534f\u8bae\u7684\u89e3\u6790\uff0c<img decoding=\"async\" width=\"351\" height=\"313\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9rAH\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u6bd4\u5982http\u534f\u8bae\u3002\u57fa\u4e8etcp\u505a\u96c6\u7fa4\u7684AJP\u534f\u8bae<\/p>\n<h4>Endpoint<\/h4>\n<p>AbstractEndpoint\u662fTcp\u7684\u5904\u7406\u5165\u53e3\uff0c\u5e76\u4e14\u6301\u6709Executor\uff1bConnector\u7684executor\u548cProtocolHandler\u7684\u7ebf\u7a0b\u6c60\u90fd\u662f\u4f7f\u7528\u8fd9\u4e2aexecutor<br \/>\u5f53\u6709\u7f51\u7edc\u4efb\u52a1\u5230\u6765\u7684\u65f6\u5019\uff0ctomcat\u4f1a\u4f7f\u7528\u8fd9\u4e2a\u7ebf\u7a0b\u6c60\u6765\u5904\u7406socket\u4e8b\u4ef6<br \/><img decoding=\"async\" width=\"723\" height=\"486\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9rC2\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h4>Acceptor \u548c Poller<\/h4>\n<p>Acceptor\u662f\u5c01\u88c5\u65b0\u8fde\u63a5\u7684\u4efb\u52a1\uff0cEndpoint\u4f7f\u7528\u5355\u72ec\u7684\u7ebf\u7a0b\u7ef4\u62a4 Poller \u662fjava NIO\u4e2dSelector\u7684\u5c01\u88c5\uff0c\u5f53\u65b0\u8fde\u63a5\u5230\u6765\u65f6\u5524\u9192Acceptor\u7ebf\u7a0b\u6ce8\u518cpollerEvent\u4e8b\u4ef6\u6765\u76d1\u542c\u8fde\u63a5<br \/><img decoding=\"async\" width=\"723\" height=\"403\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9rCm\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>Poller\u7684\u5b9e\u73b0\u662fjava\u7684Selector<br \/><img decoding=\"async\" width=\"723\" height=\"484\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9rCu\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044157864\">https:\/\/segmentfault.com\/a\/1190000044157864<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 Tomcat\u4f5c\u4e3aJava\u5f00\u53d1\u8005\u63a5\u89e6\u8fc7\u6700\u91cd\u8981\u7684web\u5bb9\u5668\uff0c\u5728\u542f\u52a8\u548c\u5904\u7406\u8bf7\u6c42\u8fc7\u7a0b\u4e2d\u505a\u4e86\u6d77\u91cf\u7684\u4e8b\u60c5\uff0c\u521d\u7ea7\u5f00\u53d1\u8005 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008190","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008190"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008190\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}