#Tomcat學習(3)#Tomcat多例項配置和反向代理叢集配置

瀟灑小同學發表於2020-10-22

Tomcat多例項配置多例項(多程式)

:同一個程式啟動多次,分為兩種情況:

第一種:一臺機器跑多個站點;

第二種:一個機器跑一個站點多個例項,配合負載均衡

1、複製程式檔案
[root@localhost ~]# cd /usr/local
[root@localhost local]# cp -r tomcat/ tomcat_2
[root@localhost local]# ls
bin  games    java  lib64    sbin   src     tomcat_2
etc  include  lib   libexec  share  tomcat  

修改埠,以啟動多例項。多例項之間埠不能一致
[root@localhost local]# sed -i 's#8005#8011#;s#8080#8081#' tomcat/conf/server.xml
[root@localhost local]# sed -i 's#8005#8012#;s#8080#8082#' tomcat_2/conf/server.xml

[root@localhost local]# sed -i 's#8009#8019#' tomcat/conf/server.xml
[root@localhost local]# sed -i 's#8009#8029#' tomcat_2/conf/server.xml 

[root@localhost local]# 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" />
啟動tomcat多例項
修改兩臺tomcat主頁標頭檔案資訊,便於區分
[root@localhost local]# vim tomcat/webapps/ROOT/index.jsp 
[root@localhost local]# vim tomcat_2/webapps/ROOT/index.jsp 

啟動配置如下:
[root@localhosts local]# cd /usr/local/tomcat/bin/
[root@localhosts bin]# vim start.sh
#!/bin/bash
#tomcat
export CATALINA_BASE="/usr/local/tomcat"

case "$1" in

start)
    $CATALINA_BASE/bin/startup.sh
    ;;
stop)
    $CATALINA_BASE/bin/shutdown.sh
esac
[root@localhost bin]# chmod +x start.sh
#修改catalina.sh ---新增如下內容
[root@localhost bin]# vim catalina.sh
CATALINA_HOME=/usr/local/tomcat  #新增的環境變數注意修改
----------------------------------------------------------
[root@localhost bin]# cd /usr/local/tomcat_2/bin/
[root@localhost bin]# vim start.sh
#!/bin/bash
#tomcat
export CATALINA_BASE="/usr/local/tomcat"

case "$1" in

start)
    $CATALINA_BASE/bin/startup.sh
    ;;
stop)
    $CATALINA_BASE/bin/shutdown.sh
esac
[root@localhost bin]# chmod +x start.sh
[root@localhost bin]# vim catalina.sh
CATALINA_HOME=/usr/local/tomcat_2
# 如果多例項部署使用JDK版本不同,修改catalina.sh再這裡定義java
JAVA_HOME=
JRE_HOME=

在這裡插入圖片描述

啟動:
[root@localhost ~]# /usr/local/tomcat/bin/start.sh start
[root@localhost ~]# /usr/local/tomcat_2/bin/start.sh start
檢查埠檢視是否啟動:

[root@localhost local]# netstat -lntp | grep java 
tcp6       0      0 127.0.0.1:8011          :::*                    LISTEN      1729/java           
tcp6       0      0 127.0.0.1:8012          :::*                    LISTEN      1783/java           
tcp6       0      0 :::8081                 :::*                    LISTEN      1729/java           
tcp6       0      0 :::8082                 :::*                    LISTEN      1783/java           
tcp6       0      0 :::8019                 :::*                    LISTEN      1729/java           
tcp6       0      0 :::8029                 :::*                    LISTEN      1783/java
2、在瀏覽器訪問,進行測試

檢查多例項的啟動

http://192.168.204.131:8081
在這裡插入圖片描述

http://192.168.204.131:8082
在這裡插入圖片描述


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]# vim default.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.204.131:8081 weight=1 max_fails=1 fail_timeout=2s;
	server 192.168.204.131: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.204.134 | tail -1 
8082
[root@nginx-proxy ~]# curl -s 192.168.204.134 | tail -1 
8081
4、在瀏覽器上進行訪問測試

#使用負載均衡器那臺的ip進行訪問
http://192.168.204.134

在這裡插入圖片描述

相關文章