Tomcat虚拟主机
作用
# 1.多个实例运行相同的应用,实现负载均衡,支持高并发处理,解决session问题.
# 2.多个实例运行不同的应用(类似虚拟主机)
多实例Example
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 |
先部署一个单节点Tomcat
1.安装JDK环境
# 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
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
/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}
配置实例server.xml
\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+Tomcat负载均衡
修改配置文件,重启服务
# 为了测试看效果使用不同的页面
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
配置Nginx
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
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