实验环境:
server1:172.25.129.1 nginx tomcat memcached
server4:172.25.129.4 tomcat memcached
实验:
1.nginx安装配置
详细安装过程请点击这里,注意加载sticky模块
[root@server1 ~]# killall -9 nginx
[root@server1 ~]# tar zfx nginx-sticky-module-ng.tar.gz
[root@server1 ~]# cd nginx-1.10.1/
[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.10.1]# make && make install
2.tomcat安装
[root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s jdk1.7.0_79/ java ##创建软链接,方便后期版本更新
vim /etc/profile
source /etc/profile ##加载环境变量
[root@server1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server1 local]# cd tomcat/bin/
[root@server1 bin]# ./startup.sh ##打开tomcat
[root@server1 tomcat]# netstat -antple ##查看端口,8080为它的端口
[root@server1 tomcat]#cd /usr/local/tomcat/webapps/ROOT
[root@server1 ROOT]# vim test.jsp ##编辑tomcat默认发布文件
The time is <%=new java.util.Date() %>
[root@server1 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf ##编辑nginx配置文件
[root@server1 ROOT]# nginx -s reload
测试:在浏览器输入172.25.129.1:8080/test.jsp为java页面 输入172.25.129.1为php页面
3.tomcat负载均衡的实现
在server4上:
[root@server4 ~]# tar zfx jdk-7u79-linux-x64.tar.gz -C /usr/local
[root@server4 ~]# tar zfx apache-tomcat-7.0.90.tar.gz -C /usr/local
[root@server4 ~]# vim /etc/profile
[root@server4 ~]# source /etc/profile
[root@server4 ~]# cd /usr/local
[root@server4 local]# ls
apache-tomcat-7.0.90 etc include lib libexec share
bin games jdk1.7.0_79 lib64 sbin src
[root@server4 local]# ln -s jdk1.7.0_79/ java
[root@server4 local]# ln -s apache-tomcat-7.0.90/ tomcat
[root@server4 local]# ll
[root@server1 ROOT]# java 或者javac测试环境是否配好
[root@server4 local]# cd tomcat/webapps/ROOT/
[root@server4 local]# cd tomcat/
[root@server4 tomcat]# bin/startup.sh
[root@server4 tomcat]# cd /usr/local/tomcat/webapps/ROOT/
[root@server4 ROOT]# vim test.jsp
Server4 The time is <%=new java.util.Date() %> ##server2的tset.jsp加上server2
在server1中:
[root@server1 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf
[root@server1 ROOT]# nginx -s reload
在浏览器测试,会轮询访问server1和server4
4.Nginx+Tomcat+Memcached集群的实现
[root@server1 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf ##在上边的实验基础上改变以下即可
[root@server1 nginx-1.10.1]# nginx -s reload
[root@server1 ~]# cd /usr/local/tomcat/bin
[root@server1 bin]# sh shutdown.sh
[root@server1 bin]# yum install memcached -y
[root@server1 bin]# /etc/init.d/memcached start
[root@server1 tomcat]# cd lib/
[root@server1 lib]# ls ##jar包在官网下载,放到lib目录下即可
[root@server1 lib]# rm -f memcached-session-manager-tc6-1.6.3.jar
[root@server1 conf]# vim context.xml
在server4中:
[root@server4 bin]# sh shutdown.sh
[root@server4 ROOT]# yum install memcached -y
[root@server4 ROOT]# /etc/init.d/memcached start
[root@server4 lib]# rm -f memcached-session-manager-tc6-1.6.3.jar
[root@server4 tomcat]# cd conf/
[root@server4 conf]# vim context.xml
[root@server4 bin]# sh startup.sh
在浏览器测试,当关闭server4的tomcat后,server1的tomcat会自动接管并存在server4的session信息
[root@server4 bin]#yum install telnet 在server1和4都安装
[root@server4 bin]#telnet localhost 11211 ##查看session信息
get A55DCB3DD56E57DBAB2D0773D26FE9EF-n1
[root@server4 ROOT]# sh /usr/local/tomcat/bin/shutdown.sh ##关闭后在浏览器中测试
[root@server1 bin]# /etc/init.d/memcached stop