配置三个Tomcat实例,分别运行/webapps/ROOT下的同一网站,达到负载均衡的作用
实例目录 工作端口 实例端口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 -3 /etc/profile JAVA_HOME=http://www.likecs.com/usr/local/jdk export PATH=$JAVA_HOME/bin:$PATH export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar source /etc/profile2.安装Tomcat
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.55/bin/apache-tomcat-8.5.55.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=http://www.likecs.com/usr/local/tomcat source /etc/profile env |grep -i home CATALINA_HOME=http://www.likecs.com/usr/local/tomcat HOME=http://www.likecs.com/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 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 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 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=http://www.likecs.com/usr/local/tomcat #tomcat的安装目录 export CATALINA_BASE=http://www.likecs.com/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负载均衡用户在访问时,需要手动输入端口号,所以需要一个代理服务器,使用nginx做代理,用户访问nginx,然后由nginx访问后端的tomcat。代理方案有两种.