{"id":10008207,"date":"2023-08-31T17:59:33","date_gmt":"2023-08-31T09:59:33","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008207"},"modified":"2023-08-31T17:59:33","modified_gmt":"2023-08-31T09:59:33","slug":"%e8%af%b7%e6%b1%82%e5%9c%a8tomcat%e4%b8%ad%e7%9a%84%e6%b5%81%e8%bd%ac%ef%bc%88%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90%e7%af%87%ef%bc%89","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008207","title":{"rendered":"\u8bf7\u6c42\u5728tomcat\u4e2d\u7684\u6d41\u8f6c\uff08\u6e90\u7801\u5206\u6790\u7bc7\uff09"},"content":{"rendered":"<h3>Connector\u7684\u521d\u59cb\u5316<\/h3>\n<p>catalina\u89e3\u6790server.xml\u662f\u901a\u8fc7degister\u6765\u5b9e\u73b0\u7684\uff0cdegister\u89e3\u6790\u5230&lt;Connector\u6807\u7b7e\u540e\u505a\u7684\u4e8b\u60c5\u5982\u4e0b\u4ee3\u7801\u6240\u89c1<\/p>\n<pre><code>ConnectorCreateRule\n\n@Override\n    public void begin(String namespace, String name, Attributes attributes)\n            throws Exception {\n        Service svc = (Service)digester.peek();\n        Executor ex = null;\n        if ( attributes.getValue(&quot;executor&quot;)!=null ) {\n            ex = svc.getExecutor(attributes.getValue(&quot;executor&quot;));\n        }\n        Connector con = new Connector(attributes.getValue(&quot;protocol&quot;));\n        if (ex != null) {\n            setExecutor(con, ex);\n        }\n        String sslImplementationName = attributes.getValue(&quot;sslImplementationName&quot;);\n        if (sslImplementationName != null) {\n            setSSLImplementationName(con, sslImplementationName);\n        }\n        digester.push(con);\n    }\n<\/code><\/pre>\n<p>connector\u6839\u636e\u6807\u7b7e\u5c5e\u6027\uff0c\u62ff\u5230\u5bf9\u5e94\u7684protocol\u534f\u8bae\uff0c\u62ff\u5230\u914d\u5728service\u6807\u7b7e\u5185\u90e8\u7684\u7ebf\u7a0b\u6c60\uff0cprotocol\u7684\u540d\u79f0\u8f6c\u5316\u6210Connector\u4e2dProtocolHandler\u7c7b\u578b\u7684\u6210\u5458\u53d8\u91cf, \u540e\u7eed\u5c06\u4ee5Http11NioProtocol\u6765\u505a\u8bb2\u89e3<\/p>\n<pre><code>public Connector(String protocol) {\n        setProtocol(protocol);\n        \/\/ Instantiate protocol handler\n        ProtocolHandler p = null;\n        try {\n            Class&lt;?&gt; clazz = Class.forName(protocolHandlerClassName);\n            \/\/ \u53cd\u5c04\u8c03\u7528ProtocolHandler\u7684\u6784\u9020\u65b9\u6cd5\u7684\u65f6\u5019\u4f1a\u505a\u540e\u7eed\u7684\u521d\u59cb\u5316\n            p = (ProtocolHandler) clazz.getConstructor().newInstance();\n        } catch (Exception e) {\n            log.error(sm.getString(&quot;coyoteConnector.protocolHandlerInstantiationFailed&quot;), e);\n        } finally {\n            this.protocolHandler = p;\n        }<\/code><\/pre>\n<h3>ProtocolHandler\u7684\u6784\u9020<\/h3>\n<p>ProtocolHandler\u6709\u5176\u62bd\u8c61\u65b9\u6cd5\uff0cHttp11NioProtocol\u6784\u9020\u65b9\u6cd5\u4e2d\u7684tcp\u5b9e\u73b0\u7531NioEndpoint\u6765\u505a\uff0c\u56e0\u6b64Connector\u6784\u9020\u8d77\u6765\u7684\u65f6\u5019\uff0c\u5bf9\u5e94\u7684ProtocolHanlder\u3001endpoint\u7684\u5173\u8054\u5173\u7cfb\u5df2\u7ecf\u5173\u8054\u597d<\/p>\n<pre><code>public AbstractProtocol(AbstractEndpoint&lt;S, ?&gt; endpoint) {\n        this.endpoint = endpoint;\n        ConnectionHandler&lt;S&gt; cHandler = new ConnectionHandler&lt;&gt;(this);\n        setHandler(cHandler);\n        getEndpoint().setHandler(cHandler);\n        setSoLinger(Constants.DEFAULT_CONNECTION_LINGER);\n        setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);\n    }<\/code><\/pre>\n<h3>\u6784\u9020\u540e\u7684init\u65b9\u6cd5<\/h3>\n<p>\u5728\u89e3\u6790server.xml\u7684\u57fa\u672c\u5c42\u6b21\u7ed3\u6784\uff0c\u6210\u5458\u53d8\u91cf\u586b\u5145\u5b8c\u6574\u540e\uff0c\u9700\u8981\u8c03\u7528\u751f\u547d\u5468\u671f\u7684init\u65b9\u6cd5<\/p>\n<pre><code>org\/apache\/catalina\/startup\/Catalina.load       getServer().init(); \n-----&gt; connector.init();    --------------&gt;  protocolHandler.init();\n----------&gt; endpoint.init(); -------------&gt;\n<\/code><\/pre>\n<h4>\u770b\u4e00\u4e0bNioEndpoint\u7684init\u65b9\u6cd5<\/h4>\n<pre><code> protected void initServerSocket() throws Exception {\n        if (getUseInheritedChannel()) {\n            \/\/ Retrieve the channel provided by the OS\n            Channel ic = System.inheritedChannel();\n            if (ic instanceof ServerSocketChannel) {\n                serverSock = (ServerSocketChannel) ic;\n            }\n            if (serverSock == null) {\n                throw new IllegalArgumentException(sm.getString(&quot;endpoint.init.bind.inherited&quot;));\n            }\n        } else {\n\/\/    \u7ed1\u5b9a\u670d\u52a1\u7aef\u7684\u4f20\u8f93\u5c42\u7aef\u53e3\n            serverSock = ServerSocketChannel.open();\n            socketProperties.setProperties(serverSock.socket());\n            InetSocketAddress addr = new InetSocketAddress(getAddress(), getPortWithOffset());\n            serverSock.socket().bind(addr,getAcceptCount());\n        }\n        serverSock.configureBlocking(true); \/\/mimic APR behavior\n    }<\/code><\/pre>\n<h4>\u5efa\u7acbNio\u7684Acceptor\u7ebf\u7a0b\u548cSelector\u4e8b\u4ef6\u7ebf\u7a0b<\/h4>\n<pre><code>org.apache.tomcat.util.net.NioEndpoint#startInternal\n\npublic void startInternal() throws Exception {\n\n        if (!running) {\n            running = true;\n            paused = false;\n\n            if (socketProperties.getProcessorCache() != 0) {\n                processorCache = new SynchronizedStack&lt;&gt;(SynchronizedStack.DEFAULT_SIZE,\n                        socketProperties.getProcessorCache());\n            }\n            if (socketProperties.getEventCache() != 0) {\n                eventCache = new SynchronizedStack&lt;&gt;(SynchronizedStack.DEFAULT_SIZE,\n                        socketProperties.getEventCache());\n            }\n            if (socketProperties.getBufferPool() != 0) {\n                nioChannels = new SynchronizedStack&lt;&gt;(SynchronizedStack.DEFAULT_SIZE,\n                        socketProperties.getBufferPool());\n            }\n\n            \/\/ Create worker collection\n            if (getExecutor() == null) {\n            \/\/   \u521b\u5efaTomcat\u9ed8\u8ba4\u7ebf\u7a0b\u6c60\uff0c\u672a\u624b\u52a8\u914d\u7f6e  \u7ebf\u7a0b\u657010 - 200\n                createExecutor();\n            }\n\n            \/\/ \u8fd9\u4e2a\u662ftomcat\u7684\u8fde\u63a5\u6570\u9650\u5236\u5668\n            initializeConnectionLatch();\n\n            \/\/ Start poller thread\n            poller = new Poller();\n            Thread pollerThread = new Thread(poller, getName() + &quot;-Poller&quot;);\n            pollerThread.setPriority(threadPriority);\n            pollerThread.setDaemon(true);\n            pollerThread.start();\n\n            startAcceptorThread();\n        }\n    }\n<\/code><\/pre>\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e0bPoller\u7684\u5b9e\u73b0\u548cAcctptor\u7684\u5b9e\u73b0<\/p>\n<h4>Poller\u7684\u5b9e\u73b0<\/h4>\n<pre><code>public class Poller implements Runnable {\n\n        private Selector selector;\n        \/\/ PollerEvent          private NioSocketWrapper socketWrapper;   private int interestOps;\n        \/\/ \u5411\u591a\u8def\u590d\u7528\u5668\u6ce8\u518csocket\u548c\u9700\u8981\u5904\u7406\u7684socket\u65f6\u95f4\n        private final SynchronizedQueue&lt;PollerEvent&gt; events =\n                new SynchronizedQueue&lt;&gt;();\n            .........\n\n        public Poller() throws IOException {\n            this.selector = Selector.open();\n        }\n@Override\n        public void run() {\n            \/\/ Loop until destroy() is called\n            while (true) {\n\n                boolean hasEvents = false;\n\n                try {\n                    if (!close) {\n                        hasEvents = events();\n                        if (wakeupCounter.getAndSet(-1) &gt; 0) {\n                            \/\/ If we are here, means we have other stuff to do\n                            \/\/ Do a non blocking select\n                            keyCount = selector.selectNow();\n                        } else {\n                        \/\/ \u9ed8\u8ba4\u963b\u585e1\u79d2\u949f\uff0c\u76d1\u542c\u9700\u8981\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u7684\u5904\u7406\u4efb\u52a1\n                            keyCount = selector.select(selectorTimeout);\n                        }\n                        wakeupCounter.set(0);\n                    }\n                    ...\n                } catch (Throwable x) {\n                    ExceptionUtils.handleThrowable(x);\n                    log.error(sm.getString(&quot;endpoint.nio.selectorLoopError&quot;), x);\n                    continue;\n                }\n\n                Iterator&lt;SelectionKey&gt; iterator =\n                    keyCount &gt; 0 ? selector.selectedKeys().iterator() : null;\n                \/\/ Walk through the collection of ready keys and dispatch\n                \/\/ any active event.\n                while (iterator != null &amp;&amp; iterator.hasNext()) {\n                    SelectionKey sk = iterator.next();\n                    iterator.remove();\n                    NioSocketWrapper socketWrapper = (NioSocketWrapper) sk.attachment();\n                    \/\/ Attachment may be null if another thread has called\n                    \/\/ cancelledKey()\n                    if (socketWrapper != null) {\n                        \/\/ \u4e3b\u89d2\uff0c \u4efb\u52a1\u5904\u7406\u90fd\u5728\u91cc\u9762\n                        processKey(sk, socketWrapper);\n                    }\n                }\n\n                \/\/ Process timeouts\n                timeout(keyCount,hasEvents);\n            }\n.............\n        }\n<\/code><\/pre>\n<h4>Acctptor\u7684\u5b9e\u73b0<\/h4>\n<pre><code>public class Acceptor&lt;U&gt; implements Runnable {\n\n @Override\n    public void run() {\n        int errorDelay = 0;\n        long pauseStart = 0;\n\n        try {\n            \/\/ Loop until we receive a shutdown command\n            while (!stopCalled) {\n            ............\n\n                if (stopCalled) {\n                    break;\n                }\n                state = AcceptorState.RUNNING;\n\n                try {\n                    \/\/if we have reached max connections, wait\n                    endpoint.countUpOrAwaitConnection();\n\n                    \/\/ Endpoint might have been paused while waiting for latch\n                    \/\/ If that is the case, don't accept new connections\n                    if (endpoint.isPaused()) {\n                        continue;\n                    }\n\n                    U socket = null;\n                    try {\n                        \/\/ \u7b49\u5f85\u65b0\u8fde\u63a5\n                        socket = endpoint.serverSocketAccept();\n                    } catch (Exception ioe) {\n                        \/\/ We didn't get a socket\n                        endpoint.countDownConnection();\n                        if (endpoint.isRunning()) {\n                            \/\/ Introduce delay if necessary\n                            errorDelay = handleExceptionWithDelay(errorDelay);\n                            \/\/ re-throw\n                            throw ioe;\n                        } else {\n                            break;\n                        }\n                    }\n                    \/\/ Successful accept, reset the error delay\n                    errorDelay = 0;\n\n                    \/\/ Configure the socket\n                    if (!stopCalled &amp;&amp; !endpoint.isPaused()) {\n                        \/\/ \u7531endpoint\u6765\u5904\u7406\u65b0\u8fde\u63a5\u3001\u628a\u65b0\u8fde\u63a5\u5b58\u5728map\u91cc\u3001\u5411poller\u6ce8\u518cPollerEndpoint\n                        \/\/ \u4ee5\u540e\u8bfb\u5199\u4e8b\u4ef6\u5c31\u7531Poller\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u6765\u7ba1\u7406\n                        if (!endpoint.setSocketOptions(socket)) {\n                            endpoint.closeSocket(socket);\n                        }\n                    } else {\n                        endpoint.destroySocket(socket);\n                    }\n                }\n                ........\n        } finally {\n            stopLatch.countDown();\n        }\n        state = AcceptorState.ENDED;\n    }\n\n}<\/code><\/pre>\n<p>\u5f53\u65b0\u8fde\u63a5\u63a5\u5165\u65f6\uff0c\u4f1a\u628a\u65b0\u8fde\u63a5\u6ce8\u518c\u5230\u81f3\u5e95\u5c42\u4e3aSelector\u7684\u591a\u8def\u590d\u7528\u5668\u4e0a\uff0cTomcat\u7684Connector\u62e5\u6709\u4e86\u63a5\u53d7\u65b0\u8fde\u63a5\u548c\u5904\u7406socket\u4e8b\u4ef6\u7684\u80fd\u529b<\/p>\n<h2>\u9879\u76ee\u5b9e\u6218<\/h2>\n<p>\u4f9d\u65e7\u4f7f\u7528mytomcat.war\uff0c \u91cc\u9762\u6709\u4e00\u4e2aservlet FirstServlet,\u8ddf\u968ftomcat\u4e00\u8d77\u542f\u52a8\uff0c\u5bb9\u5668\u542f\u52a8\uff0c\u9879\u76ee\u7684\u90e8\u7f72\u6682\u4e14\u4e0d\u8ba8\u8bba<br \/>\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u4e00\u4e0b\u4e00\u4e2a\u7b80\u5355\u7684Servlet\u5982\u4f55\u5728tomcat\u4e2d\u6765\u6d41\u8f6c<\/p>\n<p>tomcat\u542f\u52a8\u65f6\uff0c Acceptor\u5c06\u963b\u585e\u5230 socket = endpoint.serverSocketAccept();\u8fd9\u4e00\u884c<br \/>\u5728\u6d4f\u89c8\u5668\u8f93\u5165<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=u2fLcm0JxYsR3CoQVluqbA%3D%3D.8KecKmx6CQNXWv%2FFvc5Cm87C9a1iUXyj1fBwneEtU%2BUGmaH0BU6fT%2BioJI5Wk1sK\" rel=\"noopener noreferrer\">http:\/\/localhost:8080\/mytomcat\/servlet1<\/a>\uff0c Acceptor\u89e3\u9664\u963b\u585e\u5e76\u4e14\u83b7\u53d6\u5230\u5ba2\u6237\u7aefsocket<br \/><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"449\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9uu8\" alt=\"image.png\" title=\"image.png\"><br \/>\u6765\u770b\u4e00\u4e0bendpoint.setSocketOptions(socket)\u505a\u4e86\u5565<\/p>\n<pre><code>\n@Override\n    protected boolean setSocketOptions(SocketChannel socket) {\n       .........\n            NioSocketWrapper newWrapper = new NioSocketWrapper(channel, this);\n            channel.reset(socket, newWrapper);\n            connections.put(socket, newWrapper);\n            socketWrapper = newWrapper;\n\n            \/\/ Set socket properties\n            \/\/ Disable blocking, polling will be used\n            socket.configureBlocking(false);\n          socketProperties.setProperties(socket.socket());\n        \/\/ \u6ce8\u518c\u65b0\u8fde\u63a5\n            poller.register(socketWrapper);\n            return true;\n        ...\n<\/code><\/pre>\n<p>\u518d\u770b\u4e00\u4e0bPoller\u7684\u4ee3\u7801 \u76d1\u542c\u4e86\u65b0\u8fde\u63a5\u7684OP_READ\u4e8b\u4ef6<\/p>\n<pre><code>public void register(final NioSocketWrapper socketWrapper) {\n            socketWrapper.interestOps(SelectionKey.OP_READ);\/\/this is what OP_REGISTER turns into.\n            PollerEvent pollerEvent = createPollerEvent(socketWrapper, OP_REGISTER);\n            addEvent(pollerEvent);\n        }<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=IhC2Oau57S%2BUSpLh47Qbjw%3D%3D.8LX1B1xPoxYg4mTI9vEiDcf45KgWRMJPDADLivvwRPROI%2F4EuvzIj6l3TKOvgDTr\" rel=\"noopener noreferrer\">http:\/\/localhost:8080\/mytomcat\/servlet1<\/a>\u7684\u8bf7\u6c42\u5c31\u4f1a\u4ea4\u7ed9Poller\u5904\u7406\uff0c\u8be6\u7ec6\u4ee3\u7801\u89c1Poller\u7684run\u51fd\u6570<\/p>\n<pre><code>\npublic void run() {\n            \/\/ Loop until destroy() is called\n            while (true) {\n\n                boolean hasEvents = false;\n\n                try {\n                    if (!close) {\n                        hasEvents = events();\n                        if (wakeupCounter.getAndSet(-1) &gt; 0) {\n                            \/\/ If we are here, means we have other stuff to do\n                            \/\/ Do a non blocking select\n                            keyCount = selector.selectNow();\n                        } else {\n                        \/\/ \u9ed8\u8ba4\u963b\u585e1\u79d2\u949f\uff0c\u76d1\u542c\u9700\u8981\u4ea4\u7ed9\u7ebf\u7a0b\u6c60\u7684\u5904\u7406\u4efb\u52a1\n                            keyCount = selector.select(selectorTimeout);\n                        }\n                        wakeupCounter.set(0);\n                    }\n                    ...\n                } catch (Throwable x) {\n                    ExceptionUtils.handleThrowable(x);\n                    log.error(sm.getString(&quot;endpoint.nio.selectorLoopError&quot;), x);\n                    continue;\n                }\n\n                Iterator&lt;SelectionKey&gt; iterator =\n                    keyCount &gt; 0 ? selector.selectedKeys().iterator() : null;\n                \/\/ Walk through the collection of ready keys and dispatch\n                \/\/ any active event.\n                while (iterator != null &amp;&amp; iterator.hasNext()) {\n                    SelectionKey sk = iterator.next();\n                    iterator.remove();\n                    NioSocketWrapper socketWrapper = (NioSocketWrapper) sk.attachment();\n                    \/\/ Attachment may be null if another thread has called\n                    \/\/ cancelledKey()\n                    if (socketWrapper != null) {\n                        \/\/ \u4e3b\u89d2\uff0c \u4efb\u52a1\u5904\u7406\u90fd\u5728\u91cc\u9762\n                        processKey(sk, socketWrapper);\n                    }\n                }\n\n                \/\/ Process timeouts\n                timeout(keyCount,hasEvents);\n            }\n.............\n        }<\/code><\/pre>\n<p>processKey\u7684\u65f6\u5019\u5411\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4e00\u4e2aSocketProcessor\u4efb\u52a1<br \/><img decoding=\"async\" width=\"723\" height=\"334\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9uzU\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p>\u591a\u7ebf\u7a0b\u4efb\u52a1\u7684\u6267\u884c<br \/>\u7ecf\u8fc7\u4e00\u7cfb\u5217\u53d8\u6362\uff0cconnecotr\u5bf9\u5e94\u7684Handler\u4e3aHttp11Nio Protocol\uff0c<br \/>\u5c06\u534f\u8bae\u90e8\u5206\u7684\u5904\u7406\u4ea4\u7531Http11Processor<\/p>\n<h2>CoyoteAdapter<\/h2>\n<p>tomcat\u5e76\u975e\u76f4\u63a5\u628a\u8bf7\u6c42\u5c01\u88c5\u4e3aHttpServletRequest\u5bf9\u8c61\u548cHttpServletResponse\u5bf9\u8c61\uff0c\u672c\u8eabconnector\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u4e0d\u53ea\u4f7f\u7528http\u534f\u8bae\u3002\u6240\u4ee5tomcat\u5b58\u5728\u6bd4\u8f83\u5e95\u5c42\u7684org.apache.coyote.Request\uff0c \u4e5f\u5b58\u5728\u7ee7\u627fHttpServletRequest\u7684org.apache.catalina.connector.Request\uff0cCoyoteAdapter\u8d1f\u8d23\u505a\u4e8c\u8005\u7684\u8f6c\u5316\u5e76\u4e14\u628a\u8f6c\u5316\u540e\u7684HttpServletRequest\u5bf9\u8c61\u548cHttpServletResponse\u5bf9\u8c61\u4ea4\u7ed9tomcat\u5bb9\u5668\u7684Pipeline\u3002<br \/>\u6765\u770b\u4e00\u4e0bCoyoteAdapter\u7684service\u65b9\u6cd5<\/p>\n<pre><code> public void service(org.apache.coyote.Request req, org.apache.coyote.Response res) throws Exception {\n\n        Request request = (Request) req.getNote(ADAPTER_NOTES);\n        Response response = (Response) res.getNote(ADAPTER_NOTES);\n\n        if (request == null) {\n            \/\/ Create objects\n            request = connector.createRequest();\n            request.setCoyoteRequest(req);\n            response = connector.createResponse();\n            response.setCoyoteResponse(res);\n\n            \/\/ \u505a\u4e24\u79cd\u7c7b\u578brequest\u7684\u8f6c\u6362\n            request.setResponse(response);\n            response.setRequest(request);\n        }\n\n\n        try {\n            \/\/ Parse and set Catalina and configuration specific\n            \/\/ \u4e0b\u9762\u6709\u8bb2\u89e3\u8fd9\u4e2a\u65b9\u6cd5\n            postParseSuccess = postParseRequest(req, request, res, response);\n            if (postParseSuccess) {\n                \/\/ check valves if we support async\n                request.setAsyncSupported(connector.getService().getContainer().getPipeline().isAsyncSupported());\n                \/\/ Calling the container\n               \/\/\u628aHttpServletRequest \u548cHttpServletRequest\u5bf9\u8c61\u4ea4\u7ed9tomcat\u7684\u804c\u8d23\u94fePipeline\u3002\nconnector.getService().getContainer().getPipeline().getFirst().invoke(request, response);\n            }\n            ........\n    }\n<\/code><\/pre>\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e0bpostParseRequest(req, resp)\u65b9\u6cd5<\/p>\n<pre><code>protected boolean postParseRequest(org.apache.coyote.Request req, Request request, org.apache.coyote.Response res,\n            Response response) throws IOException, ServletException {\n\n           \/\/ \u89e3\u6790\u51fa\u662f\u5426\u8981Https\n        if (req.scheme().isNull()) {\n            \/\/ Use connector scheme and secure configuration, (defaults to\n            \/\/ &quot;http&quot; and false respectively)\n            req.scheme().setString(connector.getScheme());\n            request.setSecure(connector.getSecure());\n        } else {\n            \/\/ Use processor specified scheme to determine secure state\n            request.setSecure(req.scheme().equals(&quot;https&quot;));\n        }\n\/\/ \u4ee3\u7406\u76f8\u5173\n\nString proxyName = connector.getProxyName();\n        int proxyPort = connector.getProxyPort();\n        if (proxyPort != 0) {\n            req.setServerPort(proxyPort);\n        } else if (req.getServerPort() == -1) {\n            \/\/ Not explicitly set. Use default ports based on the scheme\n            if (req.scheme().equals(&quot;https&quot;)) {\n                req.setServerPort(443);\n            } else {\n                req.setServerPort(80);\n            }\n        }\n        if (proxyName != null) {\n            req.serverName().setString(proxyName);\n        }\n\/\/ \u9884\u68c0\u65b9\u6cd5\u8bf7\u6c42\n\/\/ Check for ping OPTIONS * request\n        if (undecodedURI.equals(&quot;*&quot;)) {\n            if (req.method().equalsIgnoreCase(&quot;OPTIONS&quot;)) {\n                StringBuilder allow = new StringBuilder();\n                allow.append(&quot;GET, HEAD, POST, PUT, DELETE, OPTIONS&quot;);\n                \/\/ Trace if allowed\n                if (connector.getAllowTrace()) {\n                    allow.append(&quot;, TRACE&quot;);\n                }\n                res.setHeader(&quot;Allow&quot;, allow.toString());\n                \/\/ Access log entry as processing won't reach AccessLogValve\n                connector.getService().getContainer().logAccess(request, response, 0, true);\n                return false;\n            } else {\n                response.sendError(400, sm.getString(&quot;coyoteAdapter.invalidURI&quot;));\n            }\n        }\n        ........\n while (mapRequired) {\n            \/\/ This will map the the latest version by default\n          \/\/ tomcat\u53ef\u4ee5\u90e8\u7f72\u591a\u4e2a\u5e94\u7528\uff0c\u627e\u5230\u5177\u4f53\u5728\u67d0\u4e2aContext\u3001Host\u3001\u586b\u5145\u5230request\u5bf9\u8c61\u4e2d\u3002 \nconnector.getService().getMapper().map(serverName, decodedURI, version, request.getMappingData());\n\n     }\n.......\n\n<\/code><\/pre>\n<h3>\u63a2\u7d22\u4e00\u4e0btomcat\u7684\u804c\u8d23\u94fe<\/h3>\n<p>\u6b63\u5f0f\u8fdb\u5165Tomcat\u7684\u804c\u8d23\u94fe\u4e2d\uff0c\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e0bpipeline\u4e2d\u6709\u4ec0\u4e48<br \/>\u56e0\u4e3a\u5df2\u6709\u73af\u5883\uff0c\u672c\u6587\u76f4\u63a5\u7ed9\u51fa\u9ed8\u8ba4\u7684\u94fe\u88c5\u7ed3\u6784<br \/>StandardEngineValve&#8212;-&gt;  AccessLogValve&#8212;-&gt;  ErrorReportValve&#8212;- &gt; StandardHostValve &#8212;-&gt; StandardContextValve &#8212;&#8211;&gt; StandardWrapperValve&#8212;&gt; ApplicationFilterChain &#8212;\u540e\u7eed\u6267\u884c\u6307\u5b9a\u7684Servlet<\/p>\n<p>\u6392\u9664\u5bb9\u5668\u672c\u8eab\u7684\u8282\u70b9StandardEngineValve\uff0c StandardHostValve\uff0cStandardContextValve\u4e09\u4e2a\u8282\u70b9\u3002 AccessLogValve\uff0cErrorReportValve\uff0c  StandardWrapperValve\u503c\u5f97\u6211\u4eec\u5173\u6ce8<br \/>AccessLogValve:\u4f7f\u7528\u6765\u8bb0\u5f55\u8bbf\u95ee\u8bf7\u6c42\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u8bb0\u5f55\u5230access.log\u4e2d\u7b49<br \/>ErrorReportValve:\u6765\u5904\u7406\u540e\u9762\u7684\u9519\u8bef\uff0c\u8df3\u5230\u5931\u8d25\u9875\u9762\u7b49<br \/>StandardWrapperValve: \u5982\u4e0b\u8be6\u7ec6\u89e3\u8bf4<\/p>\n<pre><code>@Override\n    public void invoke(Request request, Response response) throws IOException, ServletException {\n        ...........\n        \/\/ \u83b7\u53d6\u5bf9\u5e94servlet\u5bf9\u5e94\u7684Wrapper\uff0c servlet\u53ef\u80fd\u8fd8\u672a\u52a0\u8f7d\n        StandardWrapper wrapper = (StandardWrapper) getContainer();\n        Servlet servlet = null;\n        Context context = (Context) wrapper.getParent();\n\n        ........\n        try {\n            if (!unavailable) {\n            \/\/ \u83b7\u53d6\u771f\u6b63\u7684servlet\uff0c \u5982\u679c\u672a\u52a0\u8f7d\u521d\u59cb\u5316\uff0c\u5728\u52a0\u8f7d\u521d\u59cb\u5316\u540e\u8fd4\u56de\n                servlet = wrapper.allocate();\n            }\n        } catch (UnavailableException e) {\n            container.getLogger().error(sm.getString(&quot;standardWrapper.allocateException&quot;, wrapper.getName()), e);\n            checkWrapperAvailable(response, wrapper);\n        } \n    \n        .................\n\n\/\/ ApplicationFilterChain\u672c\u8eab\u662fservlet\u89c4\u8303\u4e2d\u7684\u8fc7\u6ee4\u5668\uff0c \u6301\u6709servelt\u5bf9\u8c61\uff0c\u4f7f\u7528\u804c\u8d23\u94fe\u8c03\u7528\u5b8c\u6210\u540e\uff0c\u518d\u8c03\u7528servlet\uff0c \u81f3\u6b64\uff0ctomcat\u7684\u8bf7\u6c42\u6d41\u7a0b\u5230\u6b64\uff0c\u540e\u7eed\u4ea4\u7ed9\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\u6765\u5904\u7406\u8bf7\u6c42\n        ApplicationFilterChain filterChain = ApplicationFilterFactory.createFilterChain(request, wrapper, servlet);\n\n        .........\n\n         filterChain.doFilter(request.getRequest(), response.getResponse());\n \n<\/code><\/pre>\n<p>\u81f3\u6b64\uff0ctomcat\u8bb2\u8bf7\u6c42\u5904\u7406\u5b8c\u540e\u9012\u4ea4\u7ed9\u4e0a\u5c42\u5e94\u7528\u7a0b\u5e8f\u6765\u5904\u7406\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u4f9d\u6b21\u5199\u56de<br \/>\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0ctomcat\u7684\u6e90\u7801\u8fd8\u6709\u5f88\u591a\uff0c\u5982\u5bb9\u5668\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u591a\u7c7b\u7684\u52a0\u8f7d\u5b89\u5168\u6027\uff0c\u52a8\u6001\u90e8\u7f72\u7b49\uff0c\u6709\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u7ee7\u7eed\u7814\u7a76<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044169630\">https:\/\/segmentfault.com\/a\/1190000044169630<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connector\u7684\u521d\u59cb\u5316 catalina\u89e3\u6790server.xml\u662f\u901a\u8fc7degister\u6765\u5b9e\u73b0\u7684\uff0cdegis [&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-10008207","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\/10008207","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=10008207"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008207\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}