Tomcat及其负载均衡

一、实验环境

wKioL1mJ7CzixWHGAACQElPzutw841.png

二、实验步骤

1、部署Tomcat 1

1)启动后关闭防火墙

#service   iptables  stop

2)安装JDK,配置Java环境

#tar  xf  jdk-7u65-linux-x64.gz

#mv   jdk1.7.0_65/    /usr/local/java

/etc/profile.d/下建立java.sh脚本

#vim   /etc/profile.d/java.sh

wKiom1mJ7D7ySafxAAAJ4PD9am0367.png

#source   /etc/profile.d/java.sh

查看java的版本

#java   -version

wKiom1mJ7E_RzgZwAAAlQ5OH3Kk987.png

3)安装配置Tomcat

#tar   xf   apache-tomcat-7.0.54.tar.gz

#mv   apache-tomcat-7.0.54   /usr/local/tomcat7

启动tomcat

#/usr/local/tomcat7/bin/startup.sh

wKiom1mJ7GLjO2xUAAAnXmel-b0215.png#netstat   -napt | grep  8080

wKioL1mJ7JCBspBuAAAGj3JlY8E062.png

测试访问;http://192.168.2.11:8080

wKioL1mJ7KrzkXWHAAHw1dQqVfk415.png

关闭tomcat  运行/usr/local/tomcat7/bin/shutdown.sh

4)建立javaweb站点

创建目录

#mkdir  -pv  /web/webapp1

wKiom1mJ7OSwY77PAAAGrw-XiAA075.png

建立测试页

#vi   /web/webapp1/index.jsp

#more  /web/webapp1/index.jsp

wKioL1mJ7PSRoqQDAAAE5WBb2OA818.png

修改tomcatserverXml文件

#vi   /usr/local/tomcat7/conf/server.xml

wKioL1mJ7QyQtzIBAAAUz2t_xLU232.png

关闭启动Tomcat 再重新启动

#/usr/local/tomcat7/bin/shutdown.sh

#/usr/local/tomcat7/bin/startup.sh

测试网页:http://192.168.2.11:8080

wKiom1mJ7RzT4pFdAAApaTs4nmI551.png

2、部署Tomcat 2 server与部署Tomcat 1 server基本相同

1)启动后关闭防火墙

2)安装JDK,配置Java环境

/etc/profile.d/下建立java.sh脚本   

3)安装配置Tomcat

启动tomcat

4)建立javaweb站点

创建目录

#mkdir  -pv  /web/webapp1

建立测试页

#vi   /web/webapp1/index.jsp

#more  /web/webapp1/index.jsp

wKioL1mJ7f-QXD70AAAFXI3AkwE267.png

修改tomcatserverXml文件

#vi   /usr/local/tomcat7/conf/server.xml

关闭启动Tomcat 再重新启动

#/usr/local/tomcat7/bin/shutdown.sh

#/usr/local/tomcat7/bin/startup.sh

测试网页:http://192.168.2.12:8080

wKioL1mJ7i7QCIrOAAAmQ6ssTaw841.png 

3、配置Nginx服务

1)关闭防火墙

#service   iptables  stop

2)安装相关软件包

#yum   install  -y  pcre-devel  zlib-devel  openssl-devel

3)解压安装Nginx

wKiom1mJ7pHAkj3jAAA-M26O2gI308.png4)配置nginx.conf

#vi   /usr/local/nginx/conf/nginx.conf

http{.......}加入

wKioL1mJ7qWTABg_AAAPtHyxzqU194.png

http{..............}-server{..................}-location{...........}中加入一行proxy_passhttp://tomcat_server;

wKioL1mJ7rTx3-iBAAAMcXFweRM165.png

5)测试Nginx配置是否正确

#/usr/local/nginx/sbin/nginx  -t

wKiom1mJ7sPjZ1SLAAAOwqB7zmE456.png启动nginx服务

#/usr/local/nginx/sbin/nginx  -c  /usr/local/nginx/conf/nginx.conf

#netstat  -anpt | grep  nginx

wKiom1mJ7tmS5UnNAAAHXRWsi4I331.png

测试负载均衡:http//192.168.2.10反复刷新,会看到两个页面来回切换

wKiom1mJ7ufQB1d7AAArggodxig486.png

wKioL1mJ7w_hXhwHAAAlSN5e-ps978.png