Java#Tomcat多实例站点部署

it2025-04-02  2

文章目录

Tomcat多站点部署1、复制程序文件2、启动tomcat多实例3、检查端口查看是否启动4、在浏览器访问,进行测试5、tomcat反向代理集群5、tomcat反向代理集群浏览器验证

Tomcat多站点部署

实际应用中的情况多实例(多进程):同一个程序启动多次,分为两种情况: 第一种:一台机器跑多个站点,通过端口,划分虚拟主机,配合负载均衡 第二种:一个机器跑一个站点多个实例,配合负载均衡

1、复制程序文件

[root@java-tomcat1 ~]# cd /data/application/ [root@java-tomcat1 application]# ls tomcat [root@java-tomcat1 application]# cp -r tomcat/ tomcat_2 [root@java-tomcat1 application]# ls tomcat tomcat_2 修改端口,以启动多实例。多实例之间端口不能一致 [root@java-tomcat1 application]# sed -i 's#8005#8011#;s#8080#8081#' tomcat/conf/server.xml [root@java-tomcat1 application]# sed -i 's#8005#8012#;s#8080#8082#' tomcat_2/conf/server.xml [root@java-tomcat1 application]# sed -i 's#8009#8019#' tomcat/conf/server.xml [root@java-tomcat1 application]# sed -i 's#8009#8029#' tomcat_2/conf/server.xml [root@java-tomcat1 application]# diff tomcat/conf/server.xml tomcat_2/conf/server.xml #对比文件不同之处 22c22 < <Server port="8011" shutdown="SHUTDOWN"> --- > <Server port="8012" shutdown="SHUTDOWN"> 67c67 < Define a non-SSL/TLS HTTP/1.1 Connector on port 8081 --- > Define a non-SSL/TLS HTTP/1.1 Connector on port 8082 69c69 < <Connector port="8081" protocol="HTTP/1.1" --- > <Connector port="8082" protocol="HTTP/1.1" 75c75 < port="8081" protocol="HTTP/1.1" --- > port="8082" protocol="HTTP/1.1" 115,116c115,116 < <!-- Define an AJP 1.3 Connector on port 8019 --> < <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" /> --- > <!-- Define an AJP 1.3 Connector on port 8029 --> > <Connector port="8029" protocol="AJP/1.3" redirectPort="8443" />

2、启动tomcat多实例

[root@java-tomcat1 application]# cp -r /opt/webapps/ROOT/ tomcat/webapps/ [root@java-tomcat1 application]# cp -r /opt/webapps/ROOT/ tomcat_2/webapps/ [root@java-tomcat1 application]# echo 8081 >> tomcat/webapps/ROOT/index.jsp [root@java-tomcat1 application]# echo 8082 >> tomcat_2/webapps/ROOT/index.jsp 启动: [root@java-tomcat1 application]# cd /data/application/tomcat_2/bin/ [root@java-tomcat1 bin]# vim start.sh #!/bin/bash #tomcat_2 export CATALINA_BASE="/data/application/tomcat_2" case "$1" in start) $CATALINA_BASE/bin/startup.sh ;; stop) $CATALINA_BASE/bin/shutdown.sh esac [root@java-tomcat1 bin]# chmod +x start #修改catalina.sh ---添加如下内容 [root@java-tomcat1 bin]# vim catalina.sh CATALINA_HOME=/data/application/tomcat_2 #添加的环境变量注意修改 [root@java-tomcat1 bin]# cd /data/application/tomcat/bin/ [root@java-tomcat1 bin]# vim start.sh #!/bin/bash #tomcat export CATALINA_BASE="/data/application/tomcat" case "$1" in start) $CATALINA_BASE/bin/startup.sh ;; stop) $CATALINA_BASE/bin/shutdown.sh esac [root@java-tomcat1 bin]# chmod +x start.sh [root@java-tomcat1 bin]# vim catalina.sh CATALINA_HOME=/data/application/tomcat # 如果多实例部署使用JDK版本不同,修改catalina.sh再这里定义java JAVA_HOME= JRE_HOME=

启动: [root@java-tomcat1 ~]# /data/application/tomcat/bin/start.sh start [root@java-tomcat1 ~]# /data/application/tomcat_2/bin/start.sh start

我自己的实验与上面稍有不同,我部署的是三站点,站点的不同之处是标题栏不同

3、检查端口查看是否启动

4、在浏览器访问,进行测试

可以看到浏览器标题窗口的显示的内容不同

5、tomcat反向代理集群

1、负载均衡器说明

关闭防火墙和selinux

yum安装nginx [root@nginx-proxy ~]# cd /etc/yum.repos.d/ [root@nginx-proxy yum.repos.d]# vim nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1 [root@nginx-proxy yum.repos.d]# yum install yum-utils -y [root@nginx-proxy yum.repos.d]# yum install nginx -y

2、配置负载均衡器

备份原配置文件并修改

[root@nginx-proxy ~]# cd /etc/nginx/conf.d/ [root@nginx-proxy conf.d]# cp default.conf default.conf.bak [root@nginx-proxy conf.d]# mv default.conf tomcat.conf [root@nginx-proxy conf.d]# vim tomcat.conf server { listen 80; server_name localhost; access_log /var/log/nginx/proxy.access.log main; location / { proxy_pass http://testweb; proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 创建upstream配置文件: [root@nginx-proxy conf.d]# vim upstream.conf upstream testweb { server 192.168.50.114:8081 weight=1 max_fails=1 fail_timeout=2s; server 192.168.50.114:8082 weight=1 max_fails=1 fail_timeout=2s; }

启动nginx

[root@nginx-proxy ~]# systemctl start nginx

3、使用命令进行访问测试

使用curl 命令进行测试,tail进行关键字提取

[root@nginx-proxy ~]# curl -s 192.168.50.118 | tail -1 8082 [root@nginx-proxy ~]# curl -s 192.168.50.118 | tail -1 8081

5、tomcat反向代理集群浏览器验证

最新回复(0)