|
|
@ -0,0 +1,519 @@
|
|
|
|
|
|
|
|
<h1><center>Tomcat</center></h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 一:Tomcat简介
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.简介
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
![image-20230518232204478](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230518232204478.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat是WEB容器/WEB中间件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat官网:https://tomcat.apache.org/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.端口
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat自身服务的端口:8005
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat和其他应用通信的端口:8009
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat给客户端浏览器访问页面使用的端口:8080
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 3.运行环境
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JDK是 Java 语言的软件开发工具包
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat运行需要JDK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 二:Tomcat安装
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.上传
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
略
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.安装
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# tar xf apache-tomcat-7.0.34.tar.gz -C /usr/local/
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# tar xf jdk-8u91-linux-x64.tar.gz -C /usr/local/
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# ln -s /usr/local/jdk1.8.0_91 /usr/local/java
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# ln -s /usr/local/apache-tomcat-7.0.34 /usr/local/tomcat
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 3.环境变量
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# vim /etc/profile
|
|
|
|
|
|
|
|
JAVA_HOME=/usr/local/java
|
|
|
|
|
|
|
|
PATH=$JAVA_HOME/bin:$PATH
|
|
|
|
|
|
|
|
export JAVA_HOME PATH
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# source /etc/profile
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 4.检测
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# java -version
|
|
|
|
|
|
|
|
java version "1.8.0_91"
|
|
|
|
|
|
|
|
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
|
|
|
|
|
|
|
|
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 5.启动Tomcat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# /usr/local/tomcat/bin/startup.sh
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 6.管理方式
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
启动tomcat的方法:
|
|
|
|
|
|
|
|
1.直接执行/usr/local/tomcat/bin/startup.sh
|
|
|
|
|
|
|
|
2.直接执行/usr/local/tomcat/bin/catalina.sh start
|
|
|
|
|
|
|
|
关闭tomcat的方法:
|
|
|
|
|
|
|
|
1.直接执行/usr/local/tomcat/bin/shutdown.sh
|
|
|
|
|
|
|
|
2.直接执行/usr/local/tomcat/bin/catalina.sh stop
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 7.检测
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# netstat -tnlp |grep java
|
|
|
|
|
|
|
|
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 6191/java
|
|
|
|
|
|
|
|
tcp 0 0 :::8009 :::* LISTEN 6191/java
|
|
|
|
|
|
|
|
tcp 0 0 :::8080 :::* LISTEN 6191/java
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 8.测试
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IP+端口访问
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 9.Tomcat目录
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat主目录介绍
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@java-tomcat1 tomcat]# tree -L 1
|
|
|
|
|
|
|
|
.
|
|
|
|
|
|
|
|
├── bin #存放tomcat的管理脚本(二进制文件)
|
|
|
|
|
|
|
|
├── BUILDING.txt
|
|
|
|
|
|
|
|
├── conf #tomcat的配置文件(server.xml)
|
|
|
|
|
|
|
|
├── CONTRIBUTING.md
|
|
|
|
|
|
|
|
├── lib #web应用调用的jar包存放路径
|
|
|
|
|
|
|
|
├── LICENSE
|
|
|
|
|
|
|
|
├── logs #tomcat日志存放目录
|
|
|
|
|
|
|
|
├── NOTICE
|
|
|
|
|
|
|
|
├── README.md
|
|
|
|
|
|
|
|
├── RELEASE-NOTES
|
|
|
|
|
|
|
|
├── RUNNING.txt
|
|
|
|
|
|
|
|
├── temp #存放临时文件
|
|
|
|
|
|
|
|
├── webapps #默认网站发布目录
|
|
|
|
|
|
|
|
└── work #存放编译生产的.java与.class文件
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Webapps目录介绍
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@java-tomcat1 webapps]# tree -L 1
|
|
|
|
|
|
|
|
.
|
|
|
|
|
|
|
|
├── docs #tomcat的帮助文档
|
|
|
|
|
|
|
|
├── examples #web应用实例
|
|
|
|
|
|
|
|
├── host-manager #主机管理
|
|
|
|
|
|
|
|
├── manager #管理
|
|
|
|
|
|
|
|
└── ROOT #默认站点根目录
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat配置文件目录介
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
[root@java-tomcat1 conf]# tree -L 1
|
|
|
|
|
|
|
|
.
|
|
|
|
|
|
|
|
├── Catalina
|
|
|
|
|
|
|
|
├── catalina.policy
|
|
|
|
|
|
|
|
├── catalina.properties
|
|
|
|
|
|
|
|
├── context.xml
|
|
|
|
|
|
|
|
├── logging.properties
|
|
|
|
|
|
|
|
├── logs
|
|
|
|
|
|
|
|
├── server.xml # tomcat 主配置文件
|
|
|
|
|
|
|
|
├── server.xml.bak
|
|
|
|
|
|
|
|
├── server.xml.bak2
|
|
|
|
|
|
|
|
├── tomcat-users.xml # tomcat 管理用户配置文件
|
|
|
|
|
|
|
|
├── tomcat-users.xsd
|
|
|
|
|
|
|
|
└── web.xml
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 三:部署JSPGOU项目
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.环境准备
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Tomcat服务器正常运行Tomcat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
数据库服务器部署
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.框架
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
单机架构:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230518233730711.png" style="zoom:33%;" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
集群架构:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230518234046405.png" alt="image-20230518234046405" style="zoom:33%;" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 3.数据库部署
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
安装:略
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
创建数据库:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
create database jspgou default charset=utf8;
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
授权:略
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
上传数据库文件:略
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
修改配置(仅限jspgou项目):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# vim /etc/my.cnf
|
|
|
|
|
|
|
|
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
|
|
|
|
|
|
|
|
explicit_defaults_for_timestamp=1
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数解释:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
STRICT_TRANS_TABLES:在该模式下,如果一个值不能插入到一个事务表中,则中断当前的操作,对非事务表不做任何限制
|
|
|
|
|
|
|
|
NO_ZERO_IN_DATE:在严格模式,不接受月或日部分为0的日期。如果使用IGNORE选项,我们为类似的日期插入'0000-00-00'。在非严格模式,可以接受该日期,但会生成警告。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ERROR_FOR_DIVISION_BY_ZERO:在严格模式,在INSERT或UPDATE过程中,如果被零除(或MOD(X,0)),则产生错误(否则为警告)。如果未给出该模式,被零除时MySQL返回NULL。如果用到INSERT IGNORE或UPDATE IGNORE中,MySQL生成被零除警告,但操作结果为NULL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NO_AUTO_CREATE_USER:防止GRANT自动创建新用户,除非还指定了密码。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NO_ENGINE_SUBSTITUTION:如果需要的存储引擎被禁用或未编译,那么抛出错误。不设置此值时,用默认的存储引擎替代,并抛出一个异常
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
数据库导入:略(数据库文件来自4.1解压的sql文件)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# mysql -u root -p123 -D jspgou < jspgou.sql
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 4.项目上线(Tomcat)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
解压项目:略
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
将数据库文件远程拷贝到数据库
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
将项目放到指定目录下;网站发布目录
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# cp -r ROOT/ /usr/usr/local/tomcat/webapps/
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
更改数据库连接
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# vim /usr/local/tomcat/webapps/ROOT\WEB-INF\config\jdbc.properties
|
|
|
|
|
|
|
|
jdbc.url=jdbc:mysql://127.0.0.1:3306/创建好的数据库名?characterEncoding=UTF-8
|
|
|
|
|
|
|
|
jdbc.username=root
|
|
|
|
|
|
|
|
jdbc.password=123
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 5.访问测试
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
系统管理后台登录:http://localhost:8080/jeeadmin/jspgou/index.do
|
|
|
|
|
|
|
|
用户名:admin
|
|
|
|
|
|
|
|
密 码:123456
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 6.Tomcat启动慢解决方案
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# yum install rng-tools
|
|
|
|
|
|
|
|
[root@xingdiancloud ~]# systemctl start rngd
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 四:Tomcat扩展
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.Tomcat配置文件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server.xml: Tomcat的主配置文件,包含Service, Connector, Engine, Realm, Valve, Hosts主组件的相关配置信息;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
web.xml:遵循Servlet规范标准的配置文件,用于配置servlet,并为所有的Web应用程序提供包括MIME映射等默认配置信息;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tomcat-user.xml:Realm认证时用到的相关角色、用户和密码等信息;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catalina.policy:Java相关的安全策略配置文件,在系统资源级别上提供访问控制的能力;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catalina.properties:Tomcat内部package的定义及访问相关的控制,也包括对通过类装载器装载的内容的控制;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.properties: Tomcat6通过自己内部实现的JAVA日志记录器来记录操作相关的日志;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.xml:所有host的默认配置信息;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
1、Server组件:
|
|
|
|
|
|
|
|
<Server port=”8005” shutdown=”SHUTDOWN”>
|
|
|
|
|
|
|
|
port: 接收shutdown指令的端口,默认仅允许通过本机访问,默认为8005;
|
|
|
|
|
|
|
|
shutdown:发往此Server用于实现关闭tomcat实例的命令字符串,默认为SHUTDOWN;
|
|
|
|
|
|
|
|
2、Service组件:
|
|
|
|
|
|
|
|
Service主要用于关联一个引擎和与此引擎相关的连接器,每个连接器通过一个特定的端口和协议接收入站请求交将其转发至关联的引擎进行处理。因此,Service要包含一个引擎、一个或多个连接器。
|
|
|
|
|
|
|
|
如上面示例中的定义:
|
|
|
|
|
|
|
|
<Service name=”Catalina”>
|
|
|
|
|
|
|
|
这定义了一个名为Catalina的Service,此名字也会在产生相关的日志信息时记录在日志文件当中。
|
|
|
|
|
|
|
|
Service相关的属性:
|
|
|
|
|
|
|
|
className: 用于实现service的类名,一般都是org.apache.catalina.core.StandardService。
|
|
|
|
|
|
|
|
name:此服务的名称,默认为Catalina;
|
|
|
|
|
|
|
|
3、Connector组件:
|
|
|
|
|
|
|
|
进入Tomcat的请求可以根据Tomcat的工作模式分为如下两类:
|
|
|
|
|
|
|
|
Tomcat作为应用程序服务器:请求来自于前端的web服务器,这可能是Apache, IIS, Nginx等;
|
|
|
|
|
|
|
|
Tomcat作为独立服务器:请求来自于web浏览器;
|
|
|
|
|
|
|
|
Tomcat应该考虑工作情形并为相应情形下的请求分别定义好需要的连接器才能正确接收来自于客户端的请求。一个引擎可以有一个或多个连接器,以适应多种请求方式。
|
|
|
|
|
|
|
|
定义连接器可以使用多种属性,有些属性也只适用于某特定的连接器类型。一般说来,常见于server.xml中的连接器类型通常有4种:
|
|
|
|
|
|
|
|
1) HTTP连接器 2) SSL连接器 3) AJP 1.3连接器 4) proxy连接器
|
|
|
|
|
|
|
|
如上面示例server.xml中定义的HTTP连接器:
|
|
|
|
|
|
|
|
<Connector port=”8080″ protocol=”HTTP/1.1″
|
|
|
|
|
|
|
|
maxThreads=”150″ connectionTimeout=”20000″
|
|
|
|
|
|
|
|
redirectPort=”8443″/>
|
|
|
|
|
|
|
|
4、Engine组件:
|
|
|
|
|
|
|
|
Engine是Servlet处理器的一个实例,即servlet引擎,默认为定义在server.xml中的Catalina。Engine需要defaultHost属性来为其定义一个接收所有发往非明确定义虚拟主机的请求的host组件。如前面示例中定义的:
|
|
|
|
|
|
|
|
<Engine name=”Catalina” defaultHost=”localhost”>
|
|
|
|
|
|
|
|
5、Host组件:
|
|
|
|
|
|
|
|
位于Engine容器中用于接收请求并进行相应处理的主机或虚拟主机,如前面示例中的定义:
|
|
|
|
|
|
|
|
<Host name=”localhost” appBase=”webapps”
|
|
|
|
|
|
|
|
unpackWARs=”true” autoDeploy=”true”
|
|
|
|
|
|
|
|
xmlValidation=”false” xmlNamespaceAware=”false”>
|
|
|
|
|
|
|
|
</Host>
|
|
|
|
|
|
|
|
autoDeploy:在Tomcat处于运行状态时放置于appBase目录中的应用程序文件是否自动进行deploy;默认为true;
|
|
|
|
|
|
|
|
unpackWars:在启用此webapps时是否对WAR格式的归档文件先进行展开;默认为true;
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.Tomcat管理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
1.编辑一个文件tomcat
|
|
|
|
|
|
|
|
[root@tomcat ~]# vim tomcat
|
|
|
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
bash /usr/local/tomcat/bin/catalina.sh $1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2.给tomcat文件加执行权限
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[root@tomcat ~]# chmod a+x tomcat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3.把tomcat放到/usr/bin目录下
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[root@tomcat ~]# mv tomcat /usr/bin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4.使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[root@tomcat ~]# tomcat stop 停止
|
|
|
|
|
|
|
|
[root@tomcat ~]# tomcat start 启动
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 五:Tomcat多实例
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.简介
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
多实例运行不同的应用(类似虚拟主机)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
多实例运行相同的应用(实现负载均衡)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.多实例部署
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
在一个工作目录下创建多实例目录
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
instance1: mdkir -pv /usr/local/tomcat/instance1/{conf,logs,temp,work} 8081 8091
|
|
|
|
|
|
|
|
instance2: mdkir -pv /usr/local/tomcat/instance2/{conf,logs,temp,work} 8082 8092
|
|
|
|
|
|
|
|
instance3: mdkir -pv /usr/local/tomcat/instance3/{conf,logs,temp,work} 8083 8093
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
创建多实例目录,根据实例多少确定
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
分别创建目录instance1 instance2 instance3,拷贝原来的conf,log,temp,work到3个目录下
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@www ~]# tree -d -L 2 /usr/local/tomcat/
|
|
|
|
|
|
|
|
/usr/local/tomcat/
|
|
|
|
|
|
|
|
├── bin
|
|
|
|
|
|
|
|
├── instance1
|
|
|
|
|
|
|
|
│ ├── conf
|
|
|
|
|
|
|
|
│ ├── logs
|
|
|
|
|
|
|
|
│ ├── temp
|
|
|
|
|
|
|
|
│ └── work
|
|
|
|
|
|
|
|
├── instance2
|
|
|
|
|
|
|
|
│ ├── conf
|
|
|
|
|
|
|
|
│ ├── logs
|
|
|
|
|
|
|
|
│ ├── temp
|
|
|
|
|
|
|
|
│ └── work
|
|
|
|
|
|
|
|
├── instance3
|
|
|
|
|
|
|
|
│ ├── conf
|
|
|
|
|
|
|
|
│ ├── logs
|
|
|
|
|
|
|
|
│ ├── temp
|
|
|
|
|
|
|
|
│ └── work
|
|
|
|
|
|
|
|
└── lib
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
拷贝原来单实例的webapps到/下(/usr/local/tomcat/webapps) 路径可以自定义,但要和配置文件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@www ~]# cp -r /usr/local/tomcat/webapps /
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
配置实例server.xml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@www ~]# vim /usr/local/tomcat/instance1/conf/server.xml
|
|
|
|
|
|
|
|
<Server port="8091" shutdown="SHUTDOWN"> //修改8005
|
|
|
|
|
|
|
|
<Connector port="8081" protocol="HTTP/1.1" //修改8080
|
|
|
|
|
|
|
|
connectionTimeout="20000"
|
|
|
|
|
|
|
|
redirectPort="8443" />
|
|
|
|
|
|
|
|
删除8009的配置
|
|
|
|
|
|
|
|
<Host name="localhost" appBase="/webapps" //修改网站基准目录
|
|
|
|
|
|
|
|
unpackWARs="true" autoDeploy="true">
|
|
|
|
|
|
|
|
......
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
脚本内容这个脚本在每个实例中都要有 启动tomcat(实例)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
[root@www ~]# vim /usr/local/tomcat/instance1/ins1.sh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
. /etc/init.d/functions
|
|
|
|
|
|
|
|
export CATALINA_BASE="/usr/local/tomcat/instance1"
|
|
|
|
|
|
|
|
export CATALINA_HOME="/usr/local/tomcat"
|
|
|
|
|
|
|
|
case $1 in
|
|
|
|
|
|
|
|
start)
|
|
|
|
|
|
|
|
$CATALINA_HOME/bin/startup.sh
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
stop)
|
|
|
|
|
|
|
|
$CATALINA_HOME/bin/shutdown.sh
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
restart)
|
|
|
|
|
|
|
|
$CATALINA_HOME/bin/shutdown.sh
|
|
|
|
|
|
|
|
sleep 5
|
|
|
|
|
|
|
|
$CATALINA_HOME/bin/startup.sh
|
|
|
|
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
启动测试
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@www ~]# /usr/local/tomcat/instance1/ins1.sh start
|
|
|
|
|
|
|
|
[root@www ~]# /usr/local/tomcat/instance2/ins2.sh start
|
|
|
|
|
|
|
|
[root@www ~]# /usr/local/tomcat/instance3/ins3.sh start、
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[root@www ~]# ss -tnlp |grep :80
|
|
|
|
|
|
|
|
LISTEN 0 100 :::8081 :::* users:(("java",pid=7288,fd=48))
|
|
|
|
|
|
|
|
LISTEN 0 100 :::8082 :::* users:(("java",pid=7468,fd=48))
|
|
|
|
|
|
|
|
LISTEN 0 100 :::8083 :::* users:(("java",pid=7496,fd=48))
|
|
|
|
|
|
|
|
LISTEN 0 1 ::ffff:127.0.0.1:8091 :::* users:(("java",pid=7288,fd=61))
|
|
|
|
|
|
|
|
LISTEN 0 1 ::ffff:127.0.0.1:8092 :::* users:(("java",pid=7468,fd=61))
|
|
|
|
|
|
|
|
LISTEN 0 1 ::ffff:127.0.0.1:8093 :::* users:(("java",pid=7496,fd=61))
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
访问测试
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230519000724259.png" alt="image-20230519000724259" style="zoom:50%;" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 六:Tomcat负载均衡
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.Nginx配置
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
|
|
|
|
|
|
|
|
这个地方是 地址池,这里包含了后端服务器的地址和端口,这里的名字随便启。
|
|
|
|
|
|
|
|
http {
|
|
|
|
|
|
|
|
upstream tomcat_pool {
|
|
|
|
|
|
|
|
#ip_hash; 最大失败次数 失败超时时间
|
|
|
|
|
|
|
|
server 192.168.122.105:8081 weight=1 max_fails=2 fail_timeout=2;
|
|
|
|
|
|
|
|
server 192.168.122.105:8082 weight=1 max_fails=2 fail_timeout=2;
|
|
|
|
|
|
|
|
server 192.168.122.105:8083 weight=1 max_fails=2 fail_timeout=2;
|
|
|
|
|
|
|
|
server 192.168.122.105:8084 weight=1 max_fails=2 fail_timeout=2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
负载均衡的算法: RR round robin session ip_hash 是常用的负载均衡的算法
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
|
|
|
location / {
|
|
|
|
|
|
|
|
proxy_pass http://tomcat_pool;
|
|
|
|
|
|
|
|
proxy_redirect off;
|
|
|
|
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 七:Tomcat配置ssl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
部署tomcat服务,项目做到用https访问,使用nginx去做,访问任意一个子网站,都是https
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
略
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 八:Tomcat扩展
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.日志
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@java-tomcat1 ~]# cd /data/application/tomcat/conf/
|
|
|
|
|
|
|
|
[root@java-tomcat1 conf]# vim server.xml
|
|
|
|
|
|
|
|
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/www/logs"
|
|
|
|
|
|
|
|
prefix="jenkins-" suffix="-access_log"
|
|
|
|
|
|
|
|
pattern="%{X-Real-IP}i - %v %t "%r" - %s %b %T "%{Referer}i" "%{User-Agent}i" %a "-" "-"" />
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
日志格式参数文件:https://tomcat.apache.org/tomcat-8.5-doc/config/valve.html#Access_Logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2. JVM 参数优化
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
[root@java-tomcat1 conf]# cd ../bin/
|
|
|
|
|
|
|
|
[root@java-tomcat1 bin]# cp catalina.sh catalina.sh.bak
|
|
|
|
|
|
|
|
[root@java-tomcat1 bin]# vim catalina.sh
|
|
|
|
|
|
|
|
JAVA_OPTS="$JAVA_OPTS -Xms4096m -Xmx4096m -XX:PermSize=1024m -XX:MaxPermSize=2048m"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
设置初始堆、非堆内存大小以及年轻代
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
-Xms50m -Xmx200m -XX:PermSize=30m -XX:MaxPermSize=60m
|
|
|
|
|
|
|
|
-Xloggc:eclipse_gc.log (设置垃圾回收日志打印的文件,文件名称可以自定义)
|
|
|
|
|
|
|
|
-XX:+PrintGCTimeStamps (打印垃圾回收时间信息时的时间格式)
|
|
|
|
|
|
|
|
-XX:+PrintGCDetails (打印垃圾回收详情)
|
|
|
|
|
|
|
|
-XX:PermSize:设置永久代(perm gen)初始值。默认值为物理内存的1/64。
|
|
|
|
|
|
|
|
-XX:MaxPermSize:设置持久代最大值。物理内存的1/4。
|
|
|
|
|
|
|
|
-Xms:初始堆大小,默认为物理内存的1/64(<1GB)
|
|
|
|
|
|
|
|
-Xmx:最大堆大小
|
|
|
|
|
|
|
|
-Xmn:新生代的内存空间大小
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 3.gc日志
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
阅读GC日志,我们可以了解Java虚拟机内存分配与回收策略(开发使用)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
# vim catalina.sh
|
|
|
|
|
|
|
|
JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/data/logs/gc-%t.log"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
![image-20230519001618185](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230519001618185.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 九:项目实战
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.jar包项目
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.war包项目
|