【皇冠盘网址官网】国信安教育 | 中国电科旗下高技能it教育培训机构-皇冠盘网址

中国电科旗下高技能it教育培训机构

当前位置: 皇冠盘网址-最新皇冠会员网址 公司动态 > 技术文章 > 详解tomcat系列(一)-从源码分析tomcat的启动

详解tomcat系列(一)-从源码分析tomcat的启动

2019-05-29 19:18:39

最近安妹看私信发现技术文章的呼声很高

有几位小伙伴点名看tomcat的文章

劳模如安妹,

现在就给大家推荐一些超优质的技术文章

嗯,就从tomcat开始吧



tomcat系列(一)

-从源码分析tomcat的启动

文 / 炭烧生蚝



在整个tomcat系列文章讲解之前, 我想说的是虽然整个tomcat体系比较复杂, 但是tomcat中的代码并不难读, 只要认真花点功夫, 一定能啃下来.

由于篇幅的原因, 很难把tomcat所有的知识点都放到同一篇文章中, 我将把tomcat系列文章分为tomcat的启动, tomcat中各模块的介绍和tomcat中的设计模式三部分, 欢迎阅读与关注.



 一:通过idea搭建tomcat源码阅读环境 

    *首先我们到tomcat的皇冠盘网址官网(http://tomcat.apache.org/)上下载tomcat的源码, 本文分析的tomcat版本是tomcat7.0.94.

  • *进入皇冠盘网址官网后在左边的目录中选择tomcat7, 然后到页面末尾的源码区进行下载.

  • 1.jpg

  • *下载完成后打开idea, 选择file->open->选择tomcat的源码目录

  • 然后到项目配置中设置jdk和源码目录. file->project structure->project sdk

2.jpg


3.jpg


  • *设置完毕后我们便可以开始愉快的源码阅读之旅了. (eclipse或其他ide搭建环境的思路也是差不多的, 可以摸索一下).


 二:startup源码分析


  • *当我们初学tomcat的时候, 肯定先要学习怎样启动tomcat. 在tomcat的bin目录下有两个启动tomcat的文件, 一个是startup.bat, 它用于windows环境下启动tomcat; 另一个是startup.sh, 它用于linux环境下tomcat的启动. 两个文件中的逻辑是一样的, 我们只分析其中的startup.bat.

  • *下面给出startup.bat的源码

@echo off

rem licensed to the apache software foundation (asf) under one or more

rem contributor license agreements.  see the notice file distributed with

rem this work for additional information regarding 最新皇冠会员网址 copyright ownership.

rem the asf licenses this file to you under the apache license, version 2.0

rem (the "license"); you may not use this file except in compliance with

rem the license.  you may obtain a copy of the license at

rem

rem     http://www.apache.org/licenses/license-2.0

rem

rem unless required by applicable law or agreed to in writing, software

rem distributed under the license is distributed on an "as is" basis,

rem without warranties or conditions of any kind, either express or implied.

rem see the license for the specific language governing permissions and

rem limitations under the license.


rem ---------------------------------------------------------------------------

rem start script for the catalina server

rem ---------------------------------------------------------------------------


setlocal


rem guess catalina_home if not defined

set "current_dir=�%"

if not "�talina_home%" == "" goto gothome

set "catalina_home=%current_dir%"

if exist "�talina_home%\bin\catalina.bat" goto okhome

cd ..

set "catalina_home=�%"

cd "%current_dir%"

:gothome

if exist "�talina_home%\bin\catalina.bat" goto okhome

echo the catalina_home environment variable is not defined correctly

echo this environment variable is needed to run this program

goto end

:okhome


set "executable=�talina_home%\bin\catalina.bat"


rem check that target executable exists

if exist "%executable%" goto okexec

echo cannot find "%executable%"

echo this file is needed to run this program

goto end

:okexec


rem get remaining unshifted command line arguments and save them in the

set cmd_line_args=

:setargs

if ""%1""=="""" goto donesetargs

set cmd_line_args=%cmd_line_args% %1

shift

goto setargs

:donesetargs


call "%executable%" start %cmd_line_args%


:end


       *.bat文件中@echo是打印指令, 用于控制台输出信息, rem是注释符.

       *跳过开头的注释, 我们来到配置catalina_home的代码段, 执行startup.bat文件首先会设置catalina_home.

set "current_dir=�%"

if not "�talina_home%" == "" goto gothome

set "catalina_home=%current_dir%"

if exist "�talina_home%\bin\catalina.bat" goto okhome

cd ..

set "catalina_home=�%"

cd "%current_dir%"

:gothome

if exist "�talina_home%\bin\catalina.bat" goto okhome

echo the catalina_home environment variable is not defined correctly

echo this environment variable is needed to run this program

goto end

:okhome


       *先通过set指令把当前目录设置到一个名为current_dir的变量中,

       *如果系统中配置过catalina_home则跳到gothome代码段. 正常情况下我们的电脑都没有配置catalina_home, 所以往下执行, 把当前目录设置为catalina_home.

      *然后判断catalina_home目录下是否存在catalina.bat文件, 如果存在就跳到okhome代码块.

       *在okhome中, 会把catalina.bat文件的的路径赋给一个叫executable的变量, 然后会进一步判断这个路径是否存在, 存在则跳转到okexec代码块, 不存在的话会在控制台输出一些错误信息.

       *在okexec中, 会把setargs代码块的返回结果赋值给cmd_line_args变量, 这个变量用于存储启动参数.

      *setargs中首先会判断是否有参数, (if ""%1""==""""判断第一个参数是否为空), 如果没有参数则相当于参数项为空. 如果有参数则循环遍历所有的参数(每次拼接第一个参数).

    *最后执行call "%executable%" start %cmd_line_args%, 也就是说执行catalina.bat文件, 如果有参数则带上参数.

       *总结: startup.bat文件实际上就做了一件事情: 启动catalina.bat.

    *ps: 这样看来, 在windows下启动tomcat未必一定要通过startup.bat, 用catalina.bat start也是可以的.


catalina.bat源码分析


  • *为了不让文章看起来太臃肿, 这里就不先把整个文件贴出来了, 我们逐块代码进行分析.

  • *跳过开头的注释, 我们来到下面的代码段:

setlocal


rem suppress terminate batch job on ctrl c

if not ""%1"" == ""run"" goto mainentry

if "%temp%" == "" goto mainentry

if exist "%temp%\%~nx0.run" goto mainentry

echo y>"%temp%\%~nx0.run"

if not exist "%temp%\%~nx0.run" goto mainentry

echo y>"%temp%\%~nx0.y"

call "%~f0" %* <"%temp%\%~nx0.y"

rem use provided errorlevel

set retval=%errorlevel%

del /q "%temp%\%~nx0.y" >nul 2>&1

exit /b %retval%

:mainentry

del /q "%temp%\%~nx0.run" >nul 2>&1


  • *大多情况下我们启动tomcat都没有设置参数, 所以直接跳到mainentry代码段, 删除了一个临时文件后, 继续往下执行.

rem guess catalina_home if not defined

set "current_dir=�%"

if not "�talina_home%" == "" goto gothome

set "catalina_home=%current_dir%"

if exist "�talina_home%\bin\catalina.bat" goto okhome

cd ..

set "catalina_home=�%"

cd "%current_dir%"

:gothome


if exist "�talina_home%\bin\catalina.bat" goto okhome

echo the catalina_home environment variable is not defined correctly

echo this environment variable is needed to run this program

goto end

:okhome


rem copy catalina_base from catalina_home if not defined

if not "�talina_base%" == "" goto gotbase

set "catalina_base=�talina_home%"


  • *可以看到这段代码与startup.bat中开头的代码相似, 在确定catalina_home下有catalina.bat后把catalina_home赋给变量catalina_base.

rem get standard environment variables

if not exist "�talina_base%\bin\setenv.bat" goto checksetenvhome

call "�talina_base%\bin\setenv.bat"

goto setenvdone

:checksetenvhome

if exist "�talina_home%\bin\setenv.bat" call "�talina_home%\bin\setenv.bat"

:setenvdone


rem get standard java environment variables

if exist "�talina_home%\bin\setclasspath.bat" goto oksetclasspath

echo cannot find "�talina_home%\bin\setclasspath.bat"

echo this file is needed to run this program

goto end

:oksetclasspath

call "�talina_home%\bin\setclasspath.bat" %1

if errorlevel 1 goto end


rem add on extra jar file to classpath

rem note that there are no quotes as we do not want to introduce random

rem quotes into the classpath

if "%classpath%" == "" goto emptyclasspath

set "classpath=%classpath%;"

:emptyclasspath

set "classpath=%classpath%�talina_home%\bin\bootstrap.jar"


  • *上面这段代码依次执行了setenv.bat和setclasspath.bat文件, 目的是获得classpath, 相信会java的同学应该都会在配置环境变量时都配置过classpath, 系统拿到classpath路径后把它和catalina_home拼接在一起, 最终定位到一个叫bootstrap.jar的文件. 虽然后面还有很多代码, 但是这里必须暂停提示一下: bootstrap.jar将是我们启动tomcat的环境.

  • *接下来从gottmpdir代码块到noendorsedvar代码块进行了一些配置, 由于不是主要内容暂且跳过.

echo using catalina_base:   "�talina_base%"

echo using catalina_home:   "�talina_home%"

echo using catalina_tmpdir: "�talina_tmpdir%"

if ""%1"" == ""debug"" goto use_jdk

echo using jre_home:        "%jre_home%"

goto java_dir_displayed

:use_jdk

echo using java_home:       "%java_home%"

:java_dir_displayed

echo using classpath:       "%classpath%"


set _execjava=%_runjava%

set mainclass=org.apache.catalina.startup.bootstrap

set action=start

set security_policy_file=

set debug_opts=

set jpda=


if not ""%1"" == ""jpda"" goto nojpda

set jpda=jpda

if not "%jpda_transport%" == "" goto gotjpdatransport

set jpda_transport=dt_socket

:gotjpdatransport

if not "%jpda_address%" == "" goto gotjpdaaddress

set jpda_address=8000

:gotjpdaaddress

if not "%jpda_suspend%" == "" goto gotjpdasuspend

set jpda_suspend=n

:gotjpdasuspend

if not "%jpda_opts%" == "" goto gotjpdaopts

set jpda_opts=-agentlib:jdwp=transport=%jpda_transport%,address=%jpda_address%,server=y,suspend=%jpda_suspend%

:gotjpdaopts

shift

:nojpda


if ""%1"" == ""debug"" goto dodebug

if ""%1"" == ""run"" goto dorun

if ""%1"" == ""start"" goto dostart

if ""%1"" == ""stop"" goto dostop

if ""%1"" == ""configtest"" goto doconfigtest

if ""%1"" == ""version"" goto doversion


  • *接下来, 我们能看到一些重要的信息, 其中的重点是

  • *set _execjava=%_runjava%设置了jdk中bin目录下的java.exe文件路径.

  • *set mainclass=org.apache.catalina.startup.bootstrap设置了tomcat的启动类为bootstrap这个类. (后面会分析这个类)

  • *set action=start设置tomcat启动

  • *大家可以留意这些参数, 最后执行tomcat的启动时会用到.

if not ""%1"" == ""jpda"" goto nojpda

set jpda=jpda

if not "%jpda_transport%" == "" goto gotjpdatransport

set jpda_transport=dt_socket

:gotjpdatransport

if not "%jpda_address%" == "" goto gotjpdaaddress

set jpda_address=8000

:gotjpdaaddress

if not "%jpda_suspend%" == "" goto gotjpdasuspend

set jpda_suspend=n

:gotjpdasuspend

if not "%jpda_opts%" == "" goto gotjpdaopts

set jpda_opts=-agentlib:jdwp=transport=%jpda_transport%,address=%jpda_address%,server=y,suspend=%jpda_suspend%

:gotjpdaopts

shift

:nojpda


if ""%1"" == ""debug"" goto dodebug

if ""%1"" == ""run"" goto dorun

if ""%1"" == ""start"" goto dostart

if ""%1"" == ""stop"" goto dostop

if ""%1"" == ""configtest"" goto doconfigtest

if ""%1"" == ""version"" goto doversion


  • *接着判断第一个参数是否是jpda, 是则进行一些设定. 而正常情况下第一个参数是start, 所以跳过这段代码. 接着会判断第一个参数的内容, 根据判断, 我们会跳到dostart代码段. (有余力的同学不妨看下debug, run等启动方式)

:dostart

shift

if "%title%" == "" set title=tomcat

set _execjava=start "%title%" %_runjava%

if not ""%1"" == ""-security"" goto execcmd

shift

echo using security manager

set "security_policy_file=�talina_base%\conf\catalina.policy"

goto execcmd


  • *可以看到dostart中无非也是设定一些参数, 最终会跳转到execcmd代码段

:execcmd

rem get remaining unshifted command line arguments and save them in the

set cmd_line_args=

:setargs

if ""%1""=="""" goto donesetargs

set cmd_line_args=%cmd_line_args% %1

shift

goto setargs

:donesetargs


  • *可以看到这段代码也是在拼接参数, 把参数拼接到一个叫cmd_line_args的变量中, 接下来就是catalina最后的一段代码了.

rem execute java with the applicable properties

if not "%jpda%" == "" goto dojpda

if not "%security_policy_file%" == "" goto dosecurity

%_execjava% %logging_config% %logging_manager% %java_opts% �talina_opts% �bug_opts% -d%endorsed_prop%="%java_endorsed_dirs%" -classpath "%classpath%" -dcatalina.base="�talina_base%" -dcatalina.home="�talina_home%" -djava.io.tmpdir="�talina_tmpdir%" %mainclass% %cmd_line_args% �tion%

goto end

:dosecurity

%_execjava% %logging_config% %logging_manager% %java_opts% �talina_opts% �bug_opts% -d%endorsed_prop%="%java_endorsed_dirs%" -classpath "%classpath%" -djava.security.manager -djava.security.policy=="%security_policy_file%" -dcatalina.base="�talina_base%" -dcatalina.home="�talina_home%" -djava.io.tmpdir="�talina_tmpdir%" %mainclass% %cmd_line_args% �tion%

goto end

:dojpda

if not "%security_policy_file%" == "" goto dosecurityjpda

%_execjava% %logging_config% %logging_manager% %java_opts% %jpda_opts% �talina_opts% �bug_opts% -d%endorsed_prop%="%java_endorsed_dirs%" -classpath "%classpath%" -dcatalina.base="�talina_base%" -dcatalina.home="�talina_home%" -djava.io.tmpdir="�talina_tmpdir%" %mainclass% %cmd_line_args% �tion%

goto end

:dosecurityjpda

%_execjava% %logging_config% %logging_manager% %java_opts% %jpda_opts% �talina_opts% �bug_opts% -d%endorsed_prop%="%java_endorsed_dirs%" -classpath "%classpath%" -djava.security.manager -djava.security.policy=="%security_policy_file%" -dcatalina.base="�talina_base%" -dcatalina.home="�talina_home%" -djava.io.tmpdir="�talina_tmpdir%" %mainclass% %cmd_line_args% �tion%

goto end


:end



  • *跳过前面两行判断后, 来到了关键语句:


%_execjava% %logging_config% %logging_manager% %java_opts% �talina_opts% �bug_opts% -

d%endorsed_prop%="%java_endorsed_dirs%" -classpath "%classpath%" -dcatalina.base="�talina_base%" -

dcatalina.home="�talina_home%" -djava.io.tmpdir="�talina_tmpdir%" %mainclass% %cmd_line_args% �tion%

  • *_execjava也就是_runjava, 也就是平时说的java指令, 但在之前的dostart代码块中把_execjava改为了start "%title%" %_runjava%所以系统会另启一个命令行窗口, 名字叫tomcat.

  • *在拼接一系列参数后, 我们会看见%mainclass%, 也就是org.apache.catalina.startup.bootstrap启动类, 拼接完启动参数后, 最后拼接的是�tion%, 也就是start.

  • *总结: catalina.bat最终执行了bootstrap类中的main方法.

  • *ps: 分析完整个catalina.bat我们发现我们可以通过设定不同的参数让tomcat以不同的方式运行. 在ide中我们是可以选择debug等模式启动tomcat的, 也可以为其配置参数, 在catalina.bat中我们看到了启动tomcat背后的运作流程.


 tomcat启动流程分析


  • *上面我们从运行startup.bat一路分析来到bootstrap的启动, 下面我们先对tomcat中的组件进行一次总览. 由于篇幅的原因, tomcat中各个模块的关系将留到下一篇文章叙述, 这里先给出一张tomcat中各模块的关系图.

4.jpg


  • *从图中我们可以看出tomcat中模块还是挺多的, 那么tomcat是如何启动这些模块的呢? 请看下面这张示意图.

5.jpg


  • *由图中我们可以看到从bootstrap类的main方法开始, tomcat会以链的方式逐级调用各个模块的init()方法进行初始化, 待各个模块都初始化后, 又会逐级调用各个模块的start()方法启动各个模块. 下面我们通过部分源码看一下这个过程.

  • *首先我们来到bootstrap类的main方法

6.jpg


  • *我们可以看到, bootstrap类首先会创建一个本类对象, 然后调用init()方法进行初始化. 执行完init()方法后会判断启动参数的值, 由于我们采取默认的启动方式, 所以main方法的参数是start, 会进入下面的判断代码块

640.webp (1).jpg

  • *可以看到在设置等待后, 调用了本类对象的load()方法. 我们跟进查看load()方法的源码.

8.jpg

  • *可以看到方法的最后通过反射的方式调用了成员变量catalinadaemon的load()方法. 通过跟踪源码我们可以看到catalinadaemon是bootstrap类的一个私有成员变量

/**

 * daemon reference.

 */

private object catalinadaemon = null;


  • *它会在bootstrap的init()方法中通过反射的方式完成初始化. 下面我们回过头来看init()方法的源码.

8.jpg


  • *可以看到init()方法创建了一个catalina对象, 并把该对象赋给了catalinadaemon.

  • *再回过头来看之前分享给大家的启动流程图, 对应的是这一块


9.jpg


  • *后面的过程我就不逐一分析了, 你可以按照这张图去跟踪代码进行验证. 最终所有模块都能被初始化并启动.

  • *看到这里相信你应该对tomcat的启动过程已经有一个清晰的认识了, 但是这仅仅是理解tomcat的开始, 下一篇文章开始我将会详细介绍tomcat中的组件, 以及组件间的关系. 了解了各个组件后, tomcat的结构于你而言将不再神秘.


来源:https://blog.csdn.net/weixin_41357767/article/details/90610417 




如此干货贴,大家记得收藏哦~

来国信安学it,进大厂面试不再遥不可及

下一个高薪程序员就是你! 2019 冲鸭!


关于皇冠盘网址

国信安教育以it技术相关专业及信息安全高技能人才培养为主要业务方向,开设的重点专业有:大数据、java、web前端、软件测试、网络安全、ui设计、php等。十余年来,国信安教育培养了数万名it高技能人才,分别就业于:腾讯、阿里巴巴、京东、华为、卫士通、bbd、文轩网等知名企业及众多的高新产业信息技术企业,为四川省的软件产业发展及成都市成为国家软件名城,在人才培养及输出方面做出了重大贡献。



相关推荐

网站地图