一個應用程序在某一個端口啓動運行產生了一系列的進程就是一個實例,讓tomcat啓動兩個不一樣的相互獨立的進程,產生兩個不一樣的套接字,分別運行在不一樣的端口,讓不一樣的端口響應不一樣的請求,就是多實例.html
做用
java
# 1.多個實例運行相同的應用,實現負載均衡,支持高併發處理,解決session問題. # 2.多個實例運行不一樣的應用(相似虛擬主機)
配置三個Tomcat實例,分別運行/webapps/ROOT下的同一網站,達到負載均衡的做用python
實例目錄 | 工做端口 | 實例端口 |
---|---|---|
tomcat_instance1: /usr/local/tomcat/instance1/{conf,logs,temp,work} | 8081 | 8091 |
tomcat_instance2: /usr/local/tomcat/instance1/{conf,logs,temp,work} | 8082 | 8092 |
tomcat_instance3: /usr/local/tomcat/instance1/{conf,logs,temp,work} | 8083 | 8093 |
1.安裝JDK環境
linux
# CentOS7安裝Java,有tar包和rpm包兩種 1>. rpm -ivh jdk-8u121-linux-x64.rpm 2>. tar xvf jdk-8u151-linux-x64.tar.gz -C /usr/local/ cd /usr/local/ mv jdk1.8.0_151/ jdk tail -2 /etc/profile JAVA_HOME=/usr/local/jdk export PATH=$PATH:$JAVA_HOME/bin source /etc/profile
2.安裝Tomcat
nginx
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gz tar xvf apache-tomcat-8.5.47.tar.gz -C /usr/local/ ln -s /usr/local/apache-tomcat-8.5.47/ /usr/local/tomcat tail -1 /etc/profile export CATALINA_HOME=/usr/local/tomcat source /etc/profile env |grep -i home CATALINA_HOME=/usr/local/tomcat HOME=/root # 查看熵池的大小: cat /proc/sys/kernel/random/entropy_avail 180 增長熵池大小,解決Tomcat在CentOS 7巨慢的問題 # 安裝rng服務,增大熵池 yum -y install rng-tools systemctl start rngd && systemctl enable rngd # 啓動服務訪問Tomcat頁面快一百倍..
3.啓動Tomcat
web
/usr/local/tomcat/bin/catalina.sh start # ss -antp |grep java # LISTEN 0 100 *:8009 *:* users:(("java",pid=857,fd=58)) # LISTEN 0 100 *:8080 *:* users:(("java",pid=857,fd=53)) # LISTEN 0 1 127.0.0.1:8005 *:* users:(("java",pid=857,fd=73)) /usr/local/tomcat/bin/shutdown.sh
配置多實例目錄算法
mkdir /webapps/ROOT -p echo "welcome to tomcat mulit instance" > /webapps/ROOT/index.jsp echo "hello everyone" > /webapps/ROOT/a.jsp mkdir -p /usr/local/tomcat/instance{1,2,3}/{conf,logs,temp,work}
\cp /usr/local/tomcat/conf/* /usr/local/tomcat/instance1/conf/ \cp /usr/local/tomcat/conf/* /usr/local/tomcat/instance2/conf/ \cp /usr/local/tomcat/conf/* /usr/local/tomcat/instance3/conf/ # Context配置 # Context做用 # 路徑映射 # 應用獨立配置,例如單獨配置應用日誌,單獨配置應用訪問控制 vim /usr/local/tomcat/instance1/conf/server.xml <Server port="8091" shutdown="SHUTDOWN"> # 管理實例端口 <Connector port="8081" protocol="HTTP/1.1" # 提供web服務端口 connectionTimeout="20000" redirectPort="8443" /> <Host name="localhost" appBase="/webapps" # 若是兩個實例對應兩個不一樣的頁面,那麼直接修改appBase後端的路徑就能夠了 unpackWARs="true" autoDeploy="true"> vim /usr/local/tomcat/instance2/conf/server.xml <Server port="8092" shutdown="SHUTDOWN"> <Connector port="8082" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Host name="localhost" appBase="/webapps" unpackWARs="true" autoDeploy="true"> vim /usr/local/tomcat/instance3/conf/server.xml <Server port="8093" shutdown="SHUTDOWN"> <Connector port="8083" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Host name="localhost" appBase="/webapps" unpackWARs="true" autoDeploy="true">
chmod +x /usr/local/tomcat/instance1/ins1.sh #每一個tomcat下的instance都要建立一個ins1.sh,而且修改裏面的instance #!/bin/bash #instace1 script export JAVA_OPTS='-Xms64m -Xmx128m' #tomcat實例啓動時最小分配的內存爲64M,最大爲128M export CATALINA_HOME=/usr/local/tomcat #tomcat的安裝目錄 export CATALINA_BASE=/usr/local/tomcat/instance1 #實例1的目錄 case $1 in start) $CATALINA_HOME/bin/startup.sh ;; stop) $CATALINA_HOME/bin/shutdown.sh ;; restart) $CATALINA_HOME/bin/shutdown.sh sleep 3 $CATALINA_HOME/bin/startup.sh ;; esac chmod +x /usr/local/tomcat/instance1/ins1.sh cp instance1/ins1.sh instance2/ cp instance1/ins1.sh instance3/ vim instance3/ins1.sh vim instance2/ins1.sh # 修改tomcat安裝目錄instanace分別對應什麼目錄便可 /usr/local/tomcat/instance1/ins1.sh start /usr/local/tomcat/instance2/ins1.sh start /usr/local/tomcat/instance3/ins1.sh start elinks --dump 39.108.140.0:8081 # welcome to tomcat mulit instance elinks --dump 39.108.140.0:8082 # welcome to tomcat mulit instance elinks --dump 39.108.140.0:8083 # welcome to tomcat mulit instance
用戶在訪問時,須要手動輸入端口號,因此須要一個代理服務器,使用nginx作代理,用戶訪問nginx,而後由nginx訪問後端的tomcat。代理方案有兩種.數據庫
客戶端全部請求所有代理到後端Tomcat服務器apache
# 爲了測試看效果使用不一樣的頁面 mkdir /webapp{1..3} mkdir /webapps1/ROOT mkdir /webapps2/ROOT mkdir /webapps3/ROOT echo webapp1 > /webapps1/ROOT/index.jsp echo webapp2 > /webapps2/ROOT/index.jsp echo webapp3 > /webapps3/ROOT/index.jsp vim /usr/local/tomcat/instance1/conf/server.xml # 分別修改三個配置文件的appBase vim /usr/local/tomcat/instance2/conf/server.xml vim /usr/local/tomcat/instance3/conf/server.xml # 除了修改配置文件appBase,爲保證後端Tomcat服務器的日誌能夠記錄客戶端真實IP vim /usr/local/tomcat/instance1/conf/server.xml prefix="localhost_access_log" suffix=".txt" pattern="**%{x-real-ip}i** %l %u %t "%r" %s %b" /> /usr/local/tomcat/instance3/ins1.sh start /usr/local/tomcat/instance2/ins1.sh start /usr/local/tomcat/instance1/ins1.sh start
vim /etc/nginx/nginx.conf http { upstream tomcatsrv { server 39.108.140.0:8081 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8082 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8083 weight=1 max_fails=2 fail_timeout=2; } vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; location / { proxy_pass http://tomcatsrv; proxy_set_header x-real-ip $remote_addr; root /usr/share/nginx/html; index index.html index.htm; } nginx -s reload elinks --dump 39.108.140.0 webapp1 elinks --dump 39.108.140.0 webapp2 elinks --dump 39.108.140.0 webapp3 # 咱們去看下tomcat日誌是否記錄了真實日誌 tail -2 /usr/local/tomcat/instance1/logs/localhost_access_log.2019-11-05.txt 39.108.140.0 - - [05/Nov/2019:12:28:17 +0800] "GET / HTTP/1.0" 200 8 47.92.24.137 - - [05/Nov/2019:12:35:13 +0800] "GET / HTTP/1.0" 200 8
客戶端訪問靜態頁面由Nginx解析,客戶端如訪問jsp頁面訪問請求代理到後端Tomcat服務器
只須要準備一臺能解析靜態頁面的nginx,或者httpd,修改下配置文件便可vim
vim /etc/nginx/nginx.conf http { upstream nginxsrv { server 49.233.69.195:80 weight=1 max_fails=2 fail_timeout=2; } upstream tomcatsrv { server 39.108.140.0:8081 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8082 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8083 weight=1 max_fails=2 fail_timeout=2; } vim /etc/nginx/conf.d/default.conf location ~* \.html$ { root /usr/share/nginx/html; index index.html index.htm; proxy_pass http://nginxsrv; proxy_set_header x-real-ip $remote_addr; } location ~* \.jsp$ { proxy_pass http://tomcatsrv; proxy_set_header x-real-ip $remote_addr; } nginx -s reload elinks --dump 39.108.140.0/index.html welcome to nginx elinks --dump 39.108.140.0/index.jsp webapp1 elinks --dump 39.108.140.0/index.jsp webapp2
若是tomcat上面搭建的是一個實際的網站,點擊登陸去登陸,卻發現登陸不成功,多是session會話不一致的問題
session(會話) 暫時沒有使用共享方式,目前採用的會話保持,軟件方面能夠經過會話同步到數據庫是實現session會話共享。或者前方代理好比Nginx使用ip_hash之類的算法,一個用戶固定訪問後端的一個web服務器
即將同一個client的訪問始終調度到同一後端實例.