nginx redis tomcat 分布式web应用 session共享

  目标:多台tomcat 使用redis实现共享session。redis的安装请参阅:centos上安装redis

  nginx 作为目前最流行的开源反向代理HTTP Server,用于实现资源缓存、web server负载均衡等功能,由于其轻量级、高性能、高可靠等特点在互联网项目中有着非常普遍的应用,相关概念网上有丰富的介绍。分布式web server集群部署后需要实现session共享,本文主要介绍了使用 redis 服务器进行 session 统一存储管理的共享方案。

  

   首先你需要一台机器上安装过如下软件,并进行了如下配置:

  Version Port
 nginx 1.8 80
 tomcat_1 7.0.64 8080
 tomcat_2 7.0.64 8090
 redis 2.8.19 6379

另外需要将一些jar添加到两个tomcat lib 目录,tomcat lib 增加过jar后截图如下(点击下载全部jar):

两台tomcat的conf下context.xml文件修改如下:

?
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
<? xml version='1.0' encoding='utf-8'?>
< Context >
 
     <!-- Default set of monitored resources -->
     < WatchedResource >WEB-INF/web.xml</ WatchedResource >
 
     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
     <!--
     <Manager pathname="" />
     -->
 
     <!-- Uncomment this to enable Comet connection tacking (provides events
          on session expiration as well as webapp lifecycle) -->
     <!--
     <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
     -->
 
     < Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
       < Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
            host="localhost"
            port="6379"
            database="0"
            maxInactiveInterval="60" />
 
</ Context >

 

我们还需要给nginx配置文件中增加轮询机制,配置文件如下:

?
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
upstream site { 
     server localhost:8080;
     server localhost:9080;
}
 
 
server {
     listen       80 default_server;
     server_name 你的域名;
 
 
     location / {
         index  index_tel.jsp index.jsp index.html index.htm ; 
         proxy_redirect          off;   
         proxy_set_header        Host            $host;   
         proxy_set_header        X-Real-IP       $remote_addr;   
         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;   
         client_max_body_size    10m;   
         client_body_buffer_size 128k;   
         proxy_buffers           32 4k; 
         proxy_connect_timeout   3;   
         proxy_send_timeout      30;   
         proxy_read_timeout      30;  
         proxy_pass http://site;
     }
}

 最后我们进行测试,往tomcat1 ROOT下放个a.jsp:

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page contentType= "text/html;charset=UTF-8" language= "java" %>
<html>
<head>
     <title>测试session同步</title>
</head>
<body>
这是tomcat 8080 ,当前sessionId:<%=session.getId()%>
<%
   session.setAttribute( "name" , "我的博客" );
%>
</body>
</html>

 往tomcat1 ROOT下放个a.jsp,但内容不同:

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page contentType= "text/html;charset=UTF-8" language= "java" %>
<html>
<head>
     <title>测试session同步</title>
</head>
<body>
这是tomcat 8090 ,当前sessionId:<%=session.getId()%>
 
从session中取name:
<%=session.getAttribute( "name" )%>
</body>
</html>

 最后我们重启两个tomcat,nginx。然后访问http://你的域名/a.jsp,会看到会切换到不同的tomcat下,但是sessionid相同,并且都可以在session中取到name这个属性的值。

下面是我的截图:

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐