环境介绍:centos6.6
192.168.142.129 nginx,redis,memcached
192.168.142.130 tomcat7
192.168.142.131 tomcat7
一、基于nginx的ip_hash算法
1、安装配置环境
----------------------------tomcat安装配置---------------------------------
tar zxf jdk-8u121-linux-x64.gz -C /usr/local/
cd /usr/local/
ln -sv jdk1.8.0_121 java
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile.d/java.sh
java -version
tar zxf apache-tomcat-7.0.63.tar.gz -C /usr/local/
vim /usr/local/apache-tomcat-7.0.63/webapps/ROOT/index.jsp #192.168.142.130的测试页面
<%@ page language="java" %>
TomcatA
TomcatA.example.com
<% session.setAttribute("example.com","example.com"); %>
Session ID <%= session.getId() %> Created on <%= session.getCreationTime() %>
vim /usr/local/apache-tomcat-7.0.63/webapps/ROOT/index.jsp #192.168.142.131的测试页面
<%@ page language="java" %>
TomcatB
TomcatB.example.com
<% session.setAttribute("example.com","example.com"); %>
Session ID <%= session.getId() %> Created on <%= session.getCreationTime() %>
/usr/local/apache-tomcat-7.0.63/bin/startup.sh
--------------------------nginx安装配置-----------------------------------
vim /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://archives.fedoraproject.org/pub/archive/epel/6/$basearch
enabled=1
gpgcheck=0
yum -y install nginx
vim /etc/nginx/conf.d/default.conf
upstream www.tomcat.org {
ip_hash;
server 192.168.142.130:8080;
server 192.168.142.131:8080;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.142.129;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://www.tomcat.org;
index index.jsp index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
service nginx start
2、访问nginx测试session是否变化
客户端请求ip进行hash,再通过hash值选择后端server,多次刷新仍会调度到相同主机,测试成功。
二、基于redis实现session共享
1、安装配置
yum install redis -y
vim /etc/redis.conf
bind 192.168.142.130
daemonize yes #支持后台运行
vim /etc/nginx/conf.d/default.conf
upstream www.tomcat.org {
# ip_hash; #不能选择ip_hash算法
server 192.168.142.130:8080;
server 192.168.142.131:8080;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.142.129;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://www.tomcat.org;
index index.jsp index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
service redis start
service nginx restart
-------------------------------------tomcat------------------------------------
下载 commons-pool-1.6.jar;jedis-2.1.0.jar;tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar到tomcat的/usr/local/apache-tomcat-7.0.63/lib目录下。
vim /usr/local/apache-tomcat-7.0.63/conf/context.xmlhost="192.168.142.129" #redis主机地址
port="6379"
database="0"
maxInactiveInterval="60"/>
cd /usr/local/apache-tomcat-7.0.63
./bin/shutdown.sh
./bin/startup.sh
2、访问nginx测试session是否变化
刷新后nginx调度到不同主机,但是session不变,测试成功。
三、基于memcached实现session共享
1、安装配置
yum install -y memcached
vim /etc/nginx/conf.d/default.conf
upstream www.tomcat.org {
# ip_hash; #不能选择ip_hash算法
server 192.168.142.130:8080;
server 192.168.142.131:8080;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 192.168.142.129;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://www.tomcat.org;
index index.jsp index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
service memcached start
service nginx restart
---------------------------------tomcat相关配置---------------------------------
链接: https://pan.baidu.com/s/16RKKXrLkC6Jz_vfyihUUtA
提取码: ttm5
下载相关包到tomcat的/usr/local/apache-tomcat-7.0.63/lib目录下。
javolution-5.5.1.jar;kryo-1.03.jar;kryo-serializers-0.10.jar;memcached-2.5.jar
memcached-session-manager-1.5.1.jar;memcached-session-manager-tc7-1.5.1.jar
minlog-1.2.jar;msm-javolution-serializer-1.5.1.jar;msm-kryo-serializer-1.6.4.jar
reflectasm-0.9.jar;spymemcached-2.7.3.jar
vim /usr/local/apache-tomcat-7.0.63/conf/context.xml
memcachedNodes="n1:192.168.142.129:11211"
requestUriIgnorePattern=".*\.(png|gif|jpg|css|js|ico|jpeg|htm|html)$"
sessionBackupTimeout="1800000"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
2、访问nginx测试session是否变化
刷新后nginx调度到不同主机,但是session不变,测试成功。