centos6系统tomcat7实现session共享的几种办法
Posted 宋鹏超
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos6系统tomcat7实现session共享的几种办法相关的知识,希望对你有一定的参考价值。
环境介绍: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" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.example.com</h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("example.com","example.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
vim /usr/local/apache-tomcat-7.0.63/webapps/ROOT/index.jsp #192.168.142.131的测试页面
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.example.com</h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("example.com","example.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
/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------------------------------------
链接: https://pan.baidu.com/s/16RKKXrLkC6Jz_vfyihUUtA
提取码: ttm5
下载 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.xml
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="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
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
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不变,测试成功。
以上是关于centos6系统tomcat7实现session共享的几种办法的主要内容,如果未能解决你的问题,请参考以下文章
Tomcat7中使用nginx+redis实现session共享。将jar包引入后,在context.xml中引入文件,出现异常,报错!
linux apache tomcat7集群中session共享失败