Tomcat

发布于 29 天前  60 次阅读


一、web01

1.安装jdk

[root@web01 ~]#rpm -i jdk-8u181-linux-x64.rpm 

2.安装Tomcat

[root@web01 ~]#mkdir /soft
[root@web01 /soft]#tar xf apache-tomcat-9.0.98.tar.gz 
[root@web01 /soft]#ln -s apache-tomcat-9.0.98 tomcat

3.修改配置文件

[root@web01 /soft/tomcat/conf]#vim server.xml 
 <Host name="www.zr.com"  appBase="/code/zrlog"
            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="logs"
               prefix="zrlog_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

4.启动tomc

[root@web01 ~]#systemctl start tomcat

5.下载zrlog

[root@web01 /code/zrlog]#ll
total 10980
-rw-r--r-- 1 root root 11243048 Aug 19 20:58 zrlog-2.2.1-efbe9f9-release.war

6.修改war包的名称为ROOT

[root@web01 /code/zrlog]#ll
total 10980
drwxr-x--- 9 root root      126 Dec 23 15:06 ROOT
-rw-r--r-- 1 root root 11243048 Aug 19 20:58 ROOT.war

二、web02

1.安装jdk

[root@web02 ~]#rpm -i jdk-8u181-linux-x64.rpm 

2.安装Tomcat

 [root@web02 /soft]# scp -r 10.0.0.7:/soft /
 [root@web02 /soft]#ln -s apache-tomcat-9.0.98/ tomcat

3.配置systemctl启动方式

[root@web01 ~]#vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=Apache Tomcat Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/soft/tomcat/bin/startup.sh
ExecStop=/soft/tomcat/bin/shutdown.sh
ExecRetart=/soft/tomcat/bin/shutdown.sh && sleep2 && /soft/tomcat/bin/startup.sh
[Install]
WantedBy=multi-user.target
[root@web01 ~]#systemctl daemon-reload
[root@web01 ~]#systemctl enable tomcat
[root@web01 ~]#systemctl start tomcat

4.同步代码

[root@web02 /soft]#scp -r 10.0.0.7:/code/zrlog  /code

5.重启tomcat

[root@web02 /code/zrlog]#systemctl restart tomcat.service 

三、部署负载均衡

1.修改配置文件

[root@lb /etc/nginx/conf.d]#vim zrlog.conf
upstream zr {
        server 172.16.1.7:8080;
        server 172.16.1.8:8080;
        keepalive 16;
}
server {
        listen 443 ssl;
        server_name www.zr.com;
        ssl_certificate   ssl_key/server.crt;
        ssl_certificate_key  ssl_key/server.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

        location / {
        proxy_pass http://zr;
        include proxy_params;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
}
server {
        listen 80;
        server_name www.zr.com;
        return 302 https://$server_name$request_uri;
}

四、静态资源共享

1.查看上传图片位置

[root@web01 /code/zrlog/ROOT]#ll attached/image/20241223/20241223155050_536.jpg
-rw-r----- 1 root root 66464 Dec 23 15:50 attached/image/20241223/20241223155050_536.jpg

2.nfs配置共享zrlog的文件目录

[root@nfs ~]#vim /etc/exports
/data/wordpress  172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zhihu      172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/admin      172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/zrlog      172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)

[root@nfs ~]# systemctl restart nfs

3.web01和web02挂载

将图片推送至NFS共享目录 推送image目录到31
[root@web01 zrlog]# scp -r ROOT/attached/image 10.0.0.31:/data/zrlog/
[root@web01 /code/zrlog/ROOT/attached]#mount -t nfs 172.16.1.31:/data/zrlog /code/zrlog/ROOT/attached/
[root@web02 /code/zrlog/ROOT/attached]#mount -t nfs 172.16.1.31:/data/zrlog /code/zrlog/ROOT/attached

4.在NFS服务端递归授权目录权限

[root@nfs ~]# chown -R www.www /data/zrlog/

五、Tomcat会话保持

  • 官网的推荐的不使用,如果使用则不能超过4个节点
  • 使用第三方插件实现tomcat将会话写入到redis

1.web01配置文件

<Host name="session.oldboy.com"  appBase="/code/session"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="session_tomcat" suffix=".log"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
</Host>

2.准备代码

[root@web01 /code]#mkdir -p session/ROOT
[root@web01 conf]# vim /code/session/ROOT/index.jsp
<body>
        <%
        //HttpSession session = request.getSession(true);
        System.out.println(session.getCreationTime());
        out.println("<br> web01 SESSION ID:" + session.getId() + "<br>");
        out.println("Session created time is :" + session.getCreationTime()
        + "<br>");
        %>
</body>

3.web02

# 同步WEB01的配置
[root@web01 /code]#scp /soft/tomcat/conf/server.xml 172.16.1.8:/soft/tomcat/conf/

[root@web01 /code]#mkdir -p /code/session/ROOT
[root@web02 ROOT]# vim /code/session/ROOT/index.jsp
<body>
        <%
        //HttpSession session = request.getSession(true);
        System.out.println(session.getCreationTime());
        out.println("<br> web02 SESSION ID:" + session.getId() + "<br>");
        out.println("Session created time is :" + session.getCreationTime()
        + "<br>");
        %>
</body>
[root@web02 /code/session]#systemctl restart tomcat.service 

4.接入负载均衡

[root@lb /etc/nginx/conf.d]#vim se.conf
upstream se {
        server 172.16.1.7:8080;
        server 172.16.1.8:8080;
        keepalive 16;
}
server {
        listen 443 ssl;
        server_name session.oldboy.com;
        ssl_certificate   ssl_key/server.crt;
        ssl_certificate_key  ssl_key/server.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

        location / {
        proxy_pass http://se;
        include proxy_params;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
}
server {
        listen 80;
        server_name session.oldboy.com;
        return 302 https://$server_name$request_uri;
}
  • 访问结果session一直是变化的。

5.下载第三方插件配置接入redis

1.上传redis插件
[root@lb ~]#ll
total 900
-rw-r--r-- 1 root root 921429 Dec 23 11:13 tomcat-cluster-redis-session-manager.zip

2.解压插件
[root@web01 ~]#unzip tomcat-cluster-redis-session-manager.zip 
[root@web01 ~]#ll
total 900
drwxr-xr-x 3 root root     96 Dec 23 16:41 __MACOSX
drwxr-xr-x 4 root root     64 Aug 12  2019 tomcat-cluster-redis-session-manager
-rw-r--r-- 1 root root 921429 Dec 23 11:13 tomcat-cluster-redis-session-manager.zip
3.拷贝jars到tomcat的/lib目录中
[root@web01 ~]#cp tomcat-cluster-redis-session-manager/lib/* /soft/tomcat/lib/
4.拷贝conf下的redis.properties文件,到tomcat的conf文件
[root@web01 ~]#cp tomcat-cluster-redis-session-manager/conf/redis-data-cache.properties /soft/tomcat/conf/
5.将配置文件中连接redis地址修改为如下地址即可
[root@web01 ~]#vim /soft/tomcat/conf/redis-data-cache.properties 
#-- Redis data-cache configuration
  
# - ${ENV_VARIABLE_NAME}

#- redis hosts. ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, ....
redis.hosts=172.16.1.51:6379
6.添加如下两行至tomcat/conf/context.xml  (添加在</Context> 上一行 )
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />

7.同步到web02
[root@web01 ~]# rsync -avz --delete /soft/tomcat/ 10.0.0.8:/soft/tomcat
修改完成后重启Tomcat
  • 访问结果session不变

小棱
最后更新于 2024-12-24