lSky's blog

session共享之nginx+tomcat+redis

环境
centos6.4
nginx :192.168.44.137
tomcat1 :192.168.44.137
tomcat2 :192.168.44.139
redis :192.168.44.137
相关安装包:http://download.csdn.net/detail/tshangshi/9644017
nginx在前端做负载均衡,转发给后端tomcat进行处理,session存在reids服务器,实现session共享

redis

1.安装redis

tar xvzf redis-stable.tar.gz

cd redis-stable

make

2.修改配置文件

vim redis.conf

1
2
bind 192.168.44.137 #绑定的主机地址
daemonize yes #以守护进程的方式运行

3.运行redis-server

cd src && ./redis-server ../redis.conf

Nginx做负载均衡

vim /etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
upstream mytest.com {
server 192.168.44.137:8080;
server 192.168.44.139:8080;
}
server {
listen 80;
server_name www.nginx2.com;
index index.jsp index.html index.htm;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://mytest.com;
proxy_cookie_path /nginx2/ /;
}

Tomcat

1.下载依赖jar包,放在 $TOMCAT_HOME/lib 目录下
tomcat-redis-session-manager-VERSION.jar
jedis-2.5.2.jar
commons-pool2-2.2.jar
2.context.xml(两个tomcat配置相同)

vim $TOMCAT_HOME/conf/context.xml

添加

1
2
3
4
5
6
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="192.168.44.137"
port="6379"
database="0"
maxInactiveInterval="60" />

3.server.xml

vim $TOMCAT_HOME/conf/server.xml

tomcat1:

1
2
3
4
<Engine name="Catalina" defaultHost="192.168.44.137" jvmRoute="tomcat1">
<Host name="192.168.44.137" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" reloadable="true" docBase="/opt/rtomcat/webapps/test/" />

tomcat2:

1
2
3
4
<Engine name="Catalina" defaultHost="192.168.44.139" jvmRoute="tomcat2">
<Host name="192.168.44.139" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" reloadable="true" docBase="/opt/rtomcat/webapps/test/" />

4.测试页面 /opt/rtomcat/webapps/test/index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Tomcat Cluster Demo</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
System.out.println("application:" + application.getAttribute(dataName));
application.setAttribute(dataName, dataValue);
}
out.print("<b>Session List</b>");
Enumeration<String> e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="index.jsp" method="POST">
Name:<input type=text size=20 name="dataName">
<br>
Value:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>