Bootstrap

Tomcat详解1-基础

前景提要:

展示web页面的有很多应用,如apache,nginx,为什么还要发展tomcat,因为apache和nginx对于静态页面的处理响应速度很好,但是apache因为是多进程处理,io是一个进程多线程处理,线程处理比进程快,而且底层io请求也不一样,io请求主要有5类,select,poll,epoll,io多路复用和io信号, select主要是轮询模式,每次都需要主动询问,连接数过多,就会性能差,poll连接数大大增加,apacheio请求使用的是poll请求,需要通过遍历文件来获取已经就绪的进程,随着连接数的增长,效率也会下降,epoll是通过调用函数来返回请求,不在需要遍历文件,通过调用函数返回请求,nginx调用的epoll请求,所以一般都使用nginx处理静态网页

tomcat主要是负责动态请求,它对于静态页面的处理不如nginx,因为nginx有缓存等功能,tomcat每次都需要从磁盘加载文件,而nginx会将上一次请求缓存在本地的内存和磁盘中,如果下次请求就可以直接从缓存中读取直接返回了

tomcat主要是基于Java动态网页技术的web页面工具, 浏览器第一次请求test.jsp时, tomcat服务器会自动将test.jsp转化成test.jsp.java这么一个类,并将 该文件编译成class文件。编译完毕后再运行class文件来响应浏览器的请求。如果以后访问test.jsp就不 再重新编译jsp文件了,直接调用class文件来响应浏览器。后续如果Tomcat检测到JSP页面改动了的话, 会重新编译

Tomcat的核心分为3个部分: (1)Web容器:处理静态页面; (2)JSP容器:把jsp页面翻译成一般的 servlet (3)catalina: 是一个servlet容器,用于处理servlet

servlet本质就是一段java程序-----html输出和java输出混在一起,修改非常麻烦

JSP提供一个html模板,直接将java运行生成的数据直接对html进行填空,java源代码不需要很大调整, JSP是基于Servlet实现,JSP将表现和逻辑分离,这样页面开发人员更好的注重页面表现力更好服务客 户。 不过最终 JSP 还需要先转换为 Servlet的源代码.java文件(Tomcat中使用Jasper转换),只不过这个转换过程无需人工完成,是通过工具自动实现的,然后再编译成.class文件,最后才可以在JVM中运行。 但是还是无法实现前后端真正分离

安装tomcat

基于jdk8+tomcat9做的实验,均为二进制安装

jdk安装:

[root@rocky8 ~]#tar xzvf  jdk-8u401-linux-x64.tar.gz -C /usr/local
[root@rocky8 local]#cd /usr/local
[root@rocky8 local]#ln -s  jdk1.8.0_401/ jdk
[root@rocky8 ~]#vim /etc/profile.d/jdk.sh
[root@rocky8 ~]#. /etc/profile.d/jdk.sh
[root@rocky8 ~]#cat /etc/profile.d/jdk.sh
#!/bin/bash
export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
[root@rocky8 ~]#java -version
java version "1.8.0_401"
Java(TM) SE Runtime Environment (build 1.8.0_401-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.401-b10, mixed mode)
[root@rocky8 ~]#which java
/usr/local/jdk/bin/java

tomcat安装:

[root@rocky8 ~]#tar xzvf apache-tomcat-9.0.87.tar.gz -C /usr/local
[root@rocky8 local]#echo 'PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
[root@rocky8 local]#. /etc/profile.d/tomcat.sh
[root@rocky8 local]#echo $PATH
/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/jdk/bin
[root@rocky8 local]#catalina.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Usage: catalina.sh ( commands ... )
commands:
  debug             Start Catalina in a debugger
  debug -security   Debug Catalina with a security manager
  jpda start        Start Catalina under JPDA debugger
  run               Start Catalina in the current window
  run -security     Start in the current window with security manager
  start             Start Catalina in a separate window
  start -security   Start in a separate window with security manager
  stop              Stop Catalina, waiting up to 5 seconds for the process to end
  stop n            Stop Catalina, waiting up to n seconds for the process to end
  stop -force       Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
  stop n -force     Stop Catalina, wait up to n seconds and then use kill -KILL if still running
  configtest        Run a basic syntax check on server.xml - check exit code for result
  version           What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined
[root@rocky8 local]#catalina.sh version
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Server version: Apache Tomcat/9.0.87
Server built:   Mar 11 2024 10:12:34 UTC
Server number:  9.0.87.0
OS Name:        Linux
OS Version:     4.18.0-372.9.1.el8.x86_64
Architecture:   amd64
JVM Version:    1.8.0_401-b10
JVM Vendor:     Oracle Corporation
[root@rocky8 local]#startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@rocky8 local]#
[root@rocky8 local]#ps aux|grep tomcat
root        1997 12.6  4.8 2850888 88152 pts/0   Sl   11:40   0:01 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root        2026  0.0  0.0  12140  1192 pts/0    S+   11:40   0:00 grep --color=auto tomcat

[root@rocky8 local]#ss -tnlp
State        Recv-Q       Send-Q                  Local Address:Port              Peer Address:Port       Process
LISTEN       0            128                           0.0.0.0:22                     0.0.0.0:*           users:(("sshd",pid=1041,fd=4))
LISTEN       0            100                         127.0.0.1:25                     0.0.0.0:*           users:(("master",pid=1496,fd=16))
LISTEN       0            128                              [::]:22                        [::]:*           users:(("sshd",pid=1041,fd=6))
LISTEN       0            100                             [::1]:25                        [::]:*           users:(("master",pid=1496,fd=17))
LISTEN       0            1                  [::ffff:127.0.0.1]:8005                         *:*           users:(("java",pid=1997,fd=67))
LISTEN       0            100                                 *:8080 

[root@rocky8 local]#shutdown.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:

tomcat添加自启动

[root@rocky8 local]##useradd -r -s /sbin/nologin tomcat

[root@rocky8 local]#cat  /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jdk
[root@rocky8 local]#chown -R tomcat.tomcat /usr/local/tomcat

[root@rocky8 local]#cat  /lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target

[root@rocky8 local]#systemctl daemon-reload
[root@rocky8 local]#
[root@rocky8 local]#systemctl enable --now tomcat

[root@rocky8 tomcat]#systemctl start tomcat.service
[root@rocky8 tomcat]#systemctl stop tomcat.service
[root@rocky8 tomcat]#systemctl status tomcat.service
● tomcat.service - Tomcat
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Fri 2024-03-22 14:36:17 CST; 21s ago
  Process: 2416 ExecStop=/usr/local/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
  Process: 2390 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 2398 (code=exited, status=0/SUCCESS)

Mar 22 14:36:16 rocky8.wang.org systemd[1]: Starting Tomcat...
Mar 22 14:36:16 rocky8.wang.org systemd[1]: Started Tomcat.
Mar 22 14:36:17 rocky8.wang.org systemd[1]: tomcat.service: Succeeded.
tomcat文件结构和组成

tomcat配置文件

server.xml 主配置文件/usr/local/tomcat/conf

web.xml conf下为全剧变量。默认为webpress提供部署相关配置

/usr/local/tomcat/webapps/{dir}/WEB-INF/web.xml 单独为某个服务提供配置修改

context.xml 用于定义所有web应用均需加载的Context配置 ,也可以为单独的web应用配置

日志

tomcat有五类日志:catalina、localhost、manager、admin、host-manager

tomcat处理请求

组件分层:

server:代表整个tomcat容器,一台主机可以起多台实列

service:服务,一个service中只有一个Engine,用来组织Engine和Connection的对应关系

Connection:使用了不同的协议。绑定了不同的端口,一个service可以处理不同的请求

Eninge:引擎。响应并处理用户请求,对请求发送给相应的虚拟主机,没有匹配。就发往缺省虚拟主机

Host:虚拟主机,可以实现多虚拟主机

Context:配置特定的url路径映射和目录的映射关系 url==>directory

假设客户的请求为http://localhost:8080/test/index.jsp

  1. 浏览器将请求发到后端虚拟主机上,匹配到8080端口,tomcat监听8080端口,默认协议为http协议。
  2. Connection处理此请求,等待他所在的service服务所在的Eninge响应,
  3. Enigene匹配到名为localhost的host,匹配不到,就交给默认主机处理,
  4. localhost获得请求test/index.jsp ,匹配所拥有的Context,对应目录的Context寻找index.jsp对应servlet,tomcat将自动转化为index.jsp.java文件,编译成index.jsp.class文件。然后运行此文件,得到最终的页面效果
  5. Context将结果返回给Host,Host返回给Eninge,Eninge返回给Connector,Connector最终在返回给浏览器

基于WEB的管理Server status和Manager APP实现应用部署

打开浏览器可以访问tomcat管理的默认管理页面,点击页面3个按钮都会出现提示403的错误提示

默认的管理页面被禁用,修改一下文件可以开启默认管理页面

 <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|10\.0\.0\.\d+" />
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
[root@rocky /usr/local/tomcat/webapps/host-manager/WEB-INF]$ cat manager.xml
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>

默认为 allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />可知为本地访问,由于当前访问主机为10.0.0网段,所以修改为allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|10\.0\.0\.\d+" />

[root@rocky /usr/local/tomcat/conf]$ cat tomcat-users.xml
   <role rolename="manager-gui"/>
   <role rolename="admin-gui"/>
   <user username="admin" password="123456" roles="manager-gui,admin-gui"/>
 </tomcat-users>

添加用户认证: manager-gui和admin-gui

;