1. tomcat

tomcat

  • 一、tomcat
    • 1、作用
    • 2、安装tomcat
      • 2.1 官方地址
      • 2.2 安装jdk
      • 2.3 安装tomcat
      • 2.4 tomcat相关文件
      • 2.5 启动
      • 2.6 测试部署java项目
      • 2.7 设置开机自启动
  • 二、部署java项目流程
    • 1、拉取代码
    • 2、安装maven编译工具
    • 3、修改maven仓库地址
    • 4、部署项目运行环境
    • 5、编译项目
    • 6、部署项目
    • 7、通过nginx实现反向代理
  • 三、tomcat配置文件 server.xml
  • 四、tomcat多实例部署
    • 1、规划实例目录
    • 2、创建实例目录
    • 3、编辑实例配置
    • 4、准备实例的启动脚本
    • 5、启动多实例
    • 6、nginx实现多实例的负载均衡

一、tomcat

1、作用

Apache开源的、java中间件

2、安装tomcat

2.1 官方地址

https://tomcat.apache.org/

2.2 安装jdk

[root@ca ~]# tar xf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@ca ~]# ls /usr/local/
bin  etc  games  include  jdk1.8.0_91  lib  lib64  libexec  sbin  share  src
[root@ca ~]# 
[root@ca ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export PATH=$PATH:$JAVA_HOME/bin
[root@ca ~]# source /etc/profile
[root@ca ~]# 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)

2.3 安装tomcat

[root@ca ~]# tar xf apache-tomcat-9.0.80.tar.gz -C /usr/local/
[root@ca ~]# ls /usr/local/
apache-tomcat-9.0.80  bin  etc  games  include  jdk1.8.0_91  lib  lib64  libexec  sbin  share  src
[root@ca ~]# 
[root@ca ~]# mv /usr/local/apache-tomcat-9.0.80/ /usr/local/tomcat90
[root@ca ~]# vim /etc/profile
export CATALINA_HOME=/usr/local/tomcat90
export JAVA_OPTS="-Xms128m -Xmx512m"

JAVA_OPTS:指定最大、最小内存

[root@ca ~]# source /etc/profile

2.4 tomcat相关文件

安装目录/bin: 可执行命令
安装目录/conf:server.xml主配置文件
安装目录/logs:日志
安装目录/webapps:项目目录

2.5 启动

[root@ca ~]# /usr/local/tomcat90/bin/catalina.sh start 
Using CATALINA_BASE:   /usr/local/tomcat90
Using CATALINA_HOME:   /usr/local/tomcat90
Using CATALINA_TMPDIR: /usr/local/tomcat90/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91
Using CLASSPATH:       /usr/local/tomcat90/bin/bootstrap.jar:/usr/local/tomcat90/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

[root@ca ~]# netstat -tunlp | grep java 
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      2321/java           
tcp6       0      0 :::8080                 :::*                    LISTEN      2321/java      

8005: 默认的实例端口
8080:默认的http服务端口   
[root@ca ~]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@ca ~]# firewall-cmd --permanent --add-port=8005/tcp
success
[root@ca ~]# firewall-cmd --reload 
success

2.6 测试部署java项目

[root@ca ~]# cp project.war /usr/local/tomcat90/webapps/
[root@ca ~]# ls /usr/local/tomcat90/webapps/
project  project.war

2.7 设置开机自启动

[root@ca ~]# vim /etc/rc.d/rc.local 
export JAVA_HOME=/usr/local/jdk1.8.0_91
export PATH=$PATH:$JAVA_HOME/bin
/usr/local/tomcat90/bin/catalina.sh start

二、部署java项目流程

1、拉取代码

[root@ca ~]# yum install -y git
[root@ca ~]# git config --global user.name "Martin"
[root@ca ~]# git config --global user.email "Martin@qq.com"
[root@ca ~]# git clone https://gitee.com/martinwjc/cloudbook.git
Cloning into 'cloudbook'...
remote: Enumerating objects: 156, done.
remote: Counting objects: 100% (156/156), done.
remote: Compressing objects: 100% (116/116), done.

2、安装maven编译工具

[root@ca ~]# tar xf apache-maven-3.6.3-bin.tar.gz -C /usr/local/
[root@ca ~]# mv /usr/local/apache-maven-3.6.3/ /usr/local/maven36
[root@ca ~]# ls /usr/local/maven36
bin  boot  conf  lib  LICENSE  NOTICE  README.txt

[root@ca ~]# vim /etc/profile
export MAVEN_HOME=/usr/local/maven36
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

[root@ca ~]# source /etc/profile
[root@ca ~]# mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/maven36
Java version: 1.8.0_91, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_91/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"
[root@ca ~]# 

3、修改maven仓库地址

[root@ca conf]# vim /usr/local/maven36/conf/settings.xml 

 55   <localRepository>/data</localRepository>


146   <mirrors>
147 <mirror>
148      <id>aliyunmaven</id>
149      <mirrorOf>*</mirrorOf>
150      <name>阿里云公共仓库</name>
151     <url>https://maven.aliyun.com/repository/public</url>
152  </mirror>
153  
154  <mirror>
155     <id>nexus-aliyun</id>
156     <mirrorOf>central</mirrorOf>
157      <name>Nexus aliyun</name>
158     <url>http://maven.aliyun.com/nexus/content/groups/public</url>
159  </mirror>
160   </mirrors>

4、部署项目运行环境

安装jdk、tomcat、MySQL

[root@ca ~]# yum install -y mariadb-server
[root@ca ~]# systemctl enable --now mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@ca ~]# 
[root@ca ~]# mysql -uroot 
MariaDB [(none)]> update mysql.user set password=PASSWORD("root") where user="root" and host="localhost";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit

导入项目需要的数据库

[root@ca cloudbook]# ls
cloudbook  cloudlibrary.sql
[root@ca cloudbook]# 
[root@ca cloudbook]# mysql -uroot -proot < cloudlibrary.sql 

5、编译项目

[root@localhost cloudbook]# ls
cloudbook.iml  pom.xml  src  target

[root@localhost cloudbook]# mvn package 
...........
[INFO] Webapp assembled in [373 msecs]
[INFO] Building war: /root/book/cloudbook/target/cloudbook-1.0-SNAPSHOT.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:38 min
[INFO] Finished at: 2023-09-08T14:15:46+08:00
[INFO] ------------------------------------------------------------------------
[root@ca cloudbook]# pwd
/root/cloudbook/cloudbook
[root@ca cloudbook]# 
[root@ca cloudbook]# ls target/
classes  cloudbook-1.0-SNAPSHOT  cloudbook-1.0-SNAPSHOT.war  generated-sources  maven-archiver  maven-status
[root@ca cloudbook]# 

6、部署项目

[root@ca cloudbook]# cp target/cloudbook-1.0-SNAPSHOT.war  /usr/local/tomcat90/webapps/cloudbook.war
[root@ca cloudbook]# /usr/local/tomcat90/bin/catalina.sh stop
Using CATALINA_BASE:   /usr/local/tomcat90
Using CATALINA_HOME:   /usr/local/tomcat90
Using CATALINA_TMPDIR: /usr/local/tomcat90/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91
Using CLASSPATH:       /usr/local/tomcat90/bin/bootstrap.jar:/usr/local/tomcat90/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
[root@ca cloudbook]# /usr/local/tomcat90/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat90
Using CATALINA_HOME:   /usr/local/tomcat90
Using CATALINA_TMPDIR: /usr/local/tomcat90/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91
Using CLASSPATH:       /usr/local/tomcat90/bin/bootstrap.jar:/usr/local/tomcat90/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
[root@ca cloudbook]# 
[root@ca cloudbook]# ls /usr/local/tomcat90/webapps/
cloudbook  cloudbook.war  project  project.war

7、通过nginx实现反向代理

        location /kr {
            proxy_pass http://192.168.140.11:8080/project;
            proxy_set_header X-REAL-IP $remote_addr;
        }

        location /sj {
            proxy_pass http://192.168.140.11:8080/cloudbook;
            proxy_set_header X-REAL-IP $remote_addr;
        }

修改tomcat配置文件,指定日志格式

[root@ca ~]# vim /usr/local/tomcat90/conf/server.xml 
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%{X-REAL-IP}i %t &quot;%r&quot; %s %b %{Referer}i %{User-Agent}i" />
192.168.140.1 [31/May/2024:11:07:42 +0800] "GET /cloudbook HTTP/1.0" 302 - - Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Mobile Safari/537.36

三、tomcat配置文件 server.xml

1、tomcat实例端口

<Server port="8005" shutdown="SHUTDOWN">

2、提供http服务的端口

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />

3、提供AJP协议的端口

    <Connector protocol="AJP/1.3"
               address="::1"
               port="8009"
               redirectPort="8443"
               maxParameterCount="1000"
               />

4、定义项目目录

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

appBase="目录"

5、定义访问日志位置、格式

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%{X-REAL-IP}i %t &quot;%r&quot; %s %b %{Referer}i %{User-Agent}i" />

catalina.out: 错误日志 

6、定义tomcat线程池

    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="300" minSpareThreads="8"/>
maxThreads=:		 最大线程数
minSpareThreads=: 最少的空闲线程数量 

四、tomcat多实例部署

1、规划实例目录

实例端口: 8006	http服务: 9000	项目目录: /opt/tomcat01/webapps
实例端口: 8007	http服务: 9001	项目目录: /opt/tomcat02/webapps 

2、创建实例目录

[root@ca ~]# mkdir -p /opt/tomcat0{1,2}/{conf,webapps,logs}
[root@ca ~]# tree /opt/tomcat0{1,2}
/opt/tomcat01
├── conf
├── logs
└── webapps
/opt/tomcat02
├── conf
├── logs
└── webapps

6 directories, 0 files

[root@ca ~]# cp -ra /usr/local/tomcat90/conf/* /opt/tomcat01/conf/
[root@ca ~]# cp -ra /usr/local/tomcat90/conf/* /opt/tomcat02/conf/

3、编辑实例配置

[root@ca ~]# cat /opt/tomcat01/conf/server.xml

<Server port="8006" shutdown="SHUTDOWN">

    <Connector port="9000" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
                    <Host name="localhost"  appBase="/opt/tomcat01/webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/opt/tomcat01/logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%{X-REAL-IP}i %t &quot;%r&quot; %s %b %{Referer}i %{User-Agent}i" />

      </Host>

4、准备实例的启动脚本

[root@ca ~]# cat /opt/tomcat01/daemon.sh
#!/bin/bash
#

tomcat_start_cmd=/usr/local/tomcat90/bin/startup.sh
tomcat_stop_cmd=/usr/local/tomcat90/bin/shutdown.sh

export CATALINA_HOME=/usr/local/tomcat90
export CATALINA_BASE=/opt/tomcat01

case $1 in
   start)
     $tomcat_start_cmd
     ;;
   stop)
     $tomcat_stop_cmd
     ;;
   restart)
     $tomcat_stop_cmd
     sleep 2
     $tomcat_start_cmd
     ;;
esac
[root@ca ~]# chmod a+x /opt/tomcat01/daemon.sh

5、启动多实例

[root@ca ~]# /opt/tomcat01/daemon.sh start
Using CATALINA_BASE:   /opt/tomcat01
Using CATALINA_HOME:   /usr/local/tomcat90
Using CATALINA_TMPDIR: /opt/tomcat01/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91
Using CLASSPATH:       /usr/local/tomcat90/bin/bootstrap.jar:/usr/local/tomcat90/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

[root@ca ~]# /opt/tomcat02/daemon.sh start
Using CATALINA_BASE:   /opt/tomcat02
Using CATALINA_HOME:   /usr/local/tomcat90
Using CATALINA_TMPDIR: /opt/tomcat02/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91
Using CLASSPATH:       /usr/local/tomcat90/bin/bootstrap.jar:/usr/local/tomcat90/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.

[root@ca ~]# netstat -tunlp | grep java
tcp6       0      0 127.0.0.1:8006          :::*                    LISTEN      4189/java           
tcp6       0      0 127.0.0.1:8007          :::*                    LISTEN      4233/java           
tcp6       0      0 :::9000                 :::*                    LISTEN      4189/java           
tcp6       0      0 :::9001                 :::*                    LISTEN      4233/java           
tcp6       0      0 :::8080                 :::*                    LISTEN      4087/java           
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      4087/java   

6、nginx实现多实例的负载均衡

upstream shouji {
   server 192.168.140.11:9000;
   server 192.168.140.11:9001;
}

location /sj {
   proxy_pass http://shouji/phone_war;
   proxy_set_header X-REAL-IP $remote_addr;
}
上一篇:【从零开始部署SAM(Segment Anything Model )大模型 3 Ubuntu20 离线部署 C++】-运行


下一篇:热门新游 2024 植物大战僵尸杂交版 Mac 版本下载安装详细教程