目录
一. 复制程序文件
二. 启动tomcat多实例
三. Tomcat多实例+负载均衡
多实例(多进程):同一个程序启动多次,分为两种情况:
第一种:一台机器跑多个站点;
第二种:一个机器跑一个站点多个实例,配合负载均衡
注:该tomacat练习实验环境续接 Tomcat简介与安装-CSDN博客
一. 复制程序文件
[root@localhost ~]# cd /data/application/
[root@localhost application]# ls
tomcat[root@localhost application]# ls
tomcat[root@localhost application]# cp -r tomcat/ tomcat2
[root@localhost application]# cp -r tomcat/ tomcat3
[root@localhost application]# mv tomcat tomcat1
[root@localhost application]# ls
tomcat1 tomcat2 tomcat3#修改端口,以启动多实例。多实例之间端口不能一致
[root@localhost application]# sed -i 's#8005#8011#;s#8080#8081#' tomcat2/conf/server.xml
[root@localhost application]# sed -i 's#8005#8012#;s#8080#8082#' tomcat3/conf/server.xml
[root@localhost application]# sed -i 's#8009#8019#' tomcat2/conf/server.xml
[root@localhost application]# sed -i 's#8009#8029#' tomcat3/conf/server.xml#对比文件不同之处
[root@localhost application]# diff3 tomcat1/conf/server.xml tomcat2/conf/server.xml tomcat3/conf/server.xml
====
1:22c<Server port="8005" shutdown="SHUTDOWN">
2:22c<Server port="8011" shutdown="SHUTDOWN">
3:22c<Server port="8012" shutdown="SHUTDOWN">
====
1:67cDefine a non-SSL/TLS HTTP/1.1 Connector on port 8080
2:67cDefine a non-SSL/TLS HTTP/1.1 Connector on port 8081
3:67cDefine a non-SSL/TLS HTTP/1.1 Connector on port 8082
====
1:69c<Connector port="8080" protocol="HTTP/1.1"
2:69c<Connector port="8081" protocol="HTTP/1.1"
3:69c<Connector port="8082" protocol="HTTP/1.1"
====
1:77cport="8080" protocol="HTTP/1.1"
2:77cport="8081" protocol="HTTP/1.1"
3:77cport="8082" protocol="HTTP/1.1"
====
1:122c<!-- Define an AJP 1.3 Connector on port 8009 -->
2:122c<!-- Define an AJP 1.3 Connector on port 8019 -->
3:122c<!-- Define an AJP 1.3 Connector on port 8029 -->
====
1:126cport="8009"
2:126cport="8019"
3:126cport="8029"
二. 启动tomcat多实例
给每个示例加上一个数字,用来标识
[root@localhost application]# echo 8080 >> tomcat/webapps/ROOT/index.jsp
[root@localhost application]# echo 8081 >> tomcat2/webapps/ROOT/index.jsp
[root@localhost application]# echo 8082 >> tomcat3/webapps/ROOT/index.jsp
创建一键启动或关闭脚本
vim /usr/bin/tomcat
#!/usr/bin/bash
tomcat_home=/data/applicationfor i in `ls $tomcat_home | grep -P "tomcat\d+"`
do
export CATALINA_BASE=$tomcat_home/$i
case $1 in
start)
$CATALINA_BASE/bin/startup.sh >> /dev/null
echo $i已启动
;;
stop)
$CATALINA_BASE/bin/shutdown.sh >> /dev/null
echo $i已停止
;;
restart)
$CATALINA_BASE/bin/shutdown.sh >> /dev/null
echo $i已停止
$CATALINA_BASE/bin/startup.sh>> /dev/null
echo $i已启动
;;
esac
done
加执行权限
[root@localhost application]# chmod +x /usr/bin/tomcat
启动
[root@localhost application]# tomcat stop
tomcat1已停止
tomcat2已停止
tomcat3已停止
[root@localhost application]# tomcat start
tomcat1已启动
tomcat2已启动
tomcat3已启动
查看端口
[root@localhost application]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=855,fd=3))
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* users:(("java",pid=27625,fd=59))
LISTEN 0 1 [::ffff:127.0.0.1]:8011 *:* users:(("java",pid=27635,fd=58))
LISTEN 0 1 [::ffff:127.0.0.1]:8012 *:* users:(("java",pid=27648,fd=58))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=855,fd=4))
LISTEN 0 100 *:8080 *:* users:(("java",pid=27625,fd=52))
LISTEN 0 100 *:8081 *:* users:(("java",pid=27635,fd=52))
LISTEN 0 100 *:8082 *:* users:(("java",pid=27648,fd=52))
浏览器输入IP访问不同端口测试如图打开的三个实例:
三. Tomcat多实例+负载均衡
下载nginx
[root@localhost application]# yum install -y nginx
启动nginx
[root@localhost application]# systemctl enable --now nginx
编辑配置文件
[root@localhost application]# vim /etc/nginx/nginx.conf
#配置文件里新增一个块upstream ,然后在server块中新增一个location块,如下代码,配置文件里其他地方不用修改。
upstream testapp {server 192.168.226.20:8080;server 192.168.226.20:8081;server 192.168.226.20:8082;}server {listen 80;listen [::]:80;server_name _;root /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {proxy_pass http://testapp;}error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
更新nginx配置
[root@localhost application]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost application]# nginx -s reload
现在浏览器访问IP地址即可发现轮询前面配置的tomcat实例。