环境清单列表:(因为只有三台电脑,所有把Nginx和memcached放到一起)
应用服务器1:192.168.51.10;
应用服务器2:192.168.55.110;
memcached服务器:192.168.51.75;
Nginx地址:192.168.51.75
一、安装tomcate
1、下载tomcate7.0,下载地址:链接: https://pan.baidu.com/s/1dxiE9w 密码: nba7,把压缩包放在/home/ump/tomcat目录下。
先按照55.110上的tomcate
2、解压压缩包到/usr/local目录下,并进入到安装目录,命令:
unzip apache-tomcat-7.0.77-windows-x64.zip -d /usr/local cd /usr/local/apache-tomcat-7.0.77
3、修改conf/server.xml文件,因为192.168.55.110上面已经装有一个应用服务器,所以需要修改对应的端口,修改server端口,修改连接器的端口,修改后的内容如下:
4、启动tomcate,访问:http://192.168.55.110:9090,如果出现tomcate管理界面,说明tamcate安装成功。
5、用同样的方法,部署另一个tomcate到192.168.51.10,因为192.168.51.10上不存在端口冲突,所以server.xml不需要修改,解压后,启动,可以通过:http://192.168.51.10:8080/访问到tomcate管理控制台。
二、创建一个动态web工程,并部署到tomcate上
1、eclipse创建一个动态web工程,并新建一个index.jsp,内容如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Cluster Web Test sessionID:<%=session.getId() %>
长度:<%=session.getId().length() %>
this is Web 1
2、把创建的动态工程导出为war包。下载地址:链接: https://pan.baidu.com/s/1c2Waok4 密码: 2jaf
3、创建存放工程的目录,命令:
mkdir /home/ump/www
4、把ClusterProject.war解压到ClusterProject文件夹,把ClusterProject文件夹放入道/home/ump/www目录下,修改server.xml,添加ClusterProject工程,内容如下:
主要是添加绿色背景的语句。
5、重启Tomcat,并访问:http://192.168.55.110:9090/ClusterProject/index.jsp,如果出现下图,说明应用安装成功。
4、把ClusterProject.war安装同样的方法,部署到192.168.51.10上。为了与192.168.55.110上的应用区分,修改index.jsp中
this is Web 1
为this is Web 2
5、访问:http://192.168.51.10:8080/ClusterProject/index.jsp,如下图所示
至此,应用部署完毕。
三、安装Nginx,并配置负载均衡。(192.168.51.75)
1、Nginx的安装过程参考其他文章,不在赘述。
2、修改Nginx的配置文件,/etc/nginx/nginx.conf,修改内容如下:
# For more information on configuration, see:# * Official English Documentation: http://nginx.org/en/docs/# * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;worker_processes 1;error_log /var/log/nginx/error.log;#error_log /var/log/nginx/error.log notice;#error_log /var/log/nginx/error.log info;pid /run/nginx.pid;events { worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; index index.html index.htm; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; upstream myweb{ server 192.168.51.10:8080; server 192.168.55.110:9090; } server { listen 80 default_server; server_name localhost; root /usr/share/nginx/html; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://myweb; } # redirect server error pages to the static page /40x.html # error_page 404 /404.html; location = /40x.html { } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { } }}
只需要添加红色字体部分。
3、访问Nginx:http://192.168.51.75/ClusterProject/index.jsp,第一次访问显示图一,刷新显示图二,说明第一次访问192.168.51.10,第二次访问192.168.55.110
至此负载均衡搭建完毕。
四、安装memcached,安装过程参考其他文档,不在赘述。
memcached配置信息如下:
五、基于memcached配置tomcate,共享session
1、启动memcached,memcached -d -c 10240 -m 1024 -u root
2、下载memcached相关jar包和序列化对应的jar包,这里session序列化采用kryo,所有的jar包下载地址:链接: https://pan.baidu.com/s/1htV2vnm 密码: hkiy
3、把下载的所有jar,放入到tomcate安装目录的lib下面。
4、修改tomcate,server.xml,添加session管理。
5、同样,修改192.168.51.10上server.xml添加该session存储到memcached中。重启tomcate1和tomcate2。
6、访问http://192.168.51.75/ClusterProject/index.jsp,并刷新界面,如下图所示
可以看到this is Web1变成this is web2,说明刷新访问了tomcate1和tomcate2,但是上面的sessionId没有变化,说明,实现了session共享。
至此,tomcate+Nginx+memcached,搭建的session共享集群完毕。