Article Directory
Tomcat multi-site deployment
Multi-instance (multi-process): the same program is started multiple times, divided into two situations: the
first: one machine runs multiple sites, through ports, divides virtual hosts, and cooperates with load balancing.
The second : One machine runs multiple instances of one site, with load balancing
1. Copy the program file
[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. Start multiple instances of 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
My own experiment is slightly different from the above. I deployed three sites. The difference between the sites is the title bar.
3. Check the port to see if it is activated
4. Visit in the browser and test
You can see that the content displayed in the browser title window is different
5. Tomcat reverse proxy cluster
1. Description of load balancer
Turn off the firewall and 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. Configure the load balancer
Back up the original configuration file and modify it
[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;
}
Start nginx
[root@nginx-proxy ~]# systemctl start nginx
3. Use commands for access testing
Use curl command for testing, tail for keyword extraction
[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 reverse proxy cluster browser verification