tomcat

发布于:2025-08-15 ⋅ 阅读:(19) ⋅ 点赞:(0)

tomcat

安装

#安装java环境
[root@tomcat ~]# yum install java-1.8.0-openjdk.x86_64 -y
#查看java版本
[root@tomcatA ~]# java -version
openjdk version "1.8.0_402"
OpenJDK Runtime Environment (build 1.8.0_402-b06)
OpenJDK 64-Bit Server VM (build 25.402-b06, mixed mode)
#查看java的环境目录
[root@tomcatA ~]# which java
/usr/bin/java
[root@tomcatA ~]# ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Jul 30 10:41 /usr/bin/java -> /etc/alternatives/java
#java的运行环境
[root@tomcatA ~]# cd /etc/alternatives/jre
[root@tomcatA jre]# ls
ASSEMBLY_EXCEPTION bin lib LICENSE THIRD_PARTY_README

访问

在这里插入图片描述

文件构成

bin 服务启动、停止等相关程序和文件
conf 配置文件
lib 库目录
logs 日志目录
webapps 应用程序,应用部署目录,相当于nginx的默认发布目录
work jsp 编译后的结果文件,建议提前预热访问
[root@tomcat-a ~]# ls /usr/local/tomcat/
bin  BUILDING.txt  conf  CONTRIBUTING.md  lib  LICENSE  logs  NOTICE  README.md  RELEASE-NOTES  RUNNING.txt  temp  test.jsp  webapps  work

生成启动文件

主配置文件

[root@tomcat-a ~]# vim /usr/local/tomcat/conf/tomcat.conf

生成启动文件

vim /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

用户、目录

useradd -s /sbin/nologin -M tomcat
chown tomcat.tomcat /usr/local/tomcat/ -R

启动

[root@tomcat-a ~]# systemctl enable --now tomcat

反向代理

利用nginx反向代理

        location ~ \.jsp$ {
        proxy_pass http://tomcat;
        }

测试:

修改hosts文件

在这里插入图片描述

在这里插入图片描述

负载均衡

upstream tomcat {
        server 192.168.101.120:8080;
        server 192.168.101.130:8080;
}
server {
        listen 80;
        server_name www.jxe.org;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location ~ \.jsp$ {
        proxy_pass http://tomcat;
        }
}

session共享服务器

vim /usr/local/tomcat/conf/context.xml
tomact-a
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:192.168.101.120:11211,n2:192.168.101.130:11211"
        failoverNodes="n1"
        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
        />
</Context>

tomcat-b
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
    <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:192.168.101.120:11211,n2:192.168.101.130:11211"
        failoverNodes="n2"
        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
        />
</Context>

nginx

upstream tomcat {
   hash $cookie_JSESSIONID;
        server 192.168.101.120:8080;
        server 192.168.101.130:8080;
}
server {
        listen 80;
        server_name www.jxe.org;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        try_files $uri $uri.html $uri/index.html /error/default.html;

        location ~ \.jsp$ {
        proxy_pass http://tomcat;
        }
}

测试
在这里插入图片描述

关闭tomcat-b

systemctl stop tomcat

继续输入
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到