win7:基于nginx tomcat redis分布式web应用的session共享配置实验

一、前言

        由于session占用内存过大,会造成服务器负担,并且基于高并发环境,需要进行负载均衡以及前后端分离,所以在本机进行了基于nginx+多tomcat+redis分布式web应用的session共享配置测试。另外,项目里有一个小需求,就是一个账户只能登陆一个客户端,将session存储到noSql中,对session进行校验拦截既能实现该需求,也能减少服务器压力,使tomcat无状态,即使单机tomcat宕机,也不会影响服务的正常运行。

        基本架构:

          redis可做集群配置。

二、测试环境

oswin7 64
nginxnginx-1.10.2
tomcatapache-tomcat-7.0.75-windows-x64
jdkjdk1.7_64
redisredis2.8

          注:redis和nginx最好还是要在linux上使用,win7只是用来测试!后续有时间会将linux的测试结果总结出来!

 jdk环境配置,在此不赘述!

redis安装,略!官方下载地址:http://redis.io/download,不过官方没有64位的Windows下的可执行程序,目前有个开源的托管在github上, 地址:https://github.com/ServiceStack/redis-windows 点击这个地方,下载文件并解压到自己的电脑目录下。

安装教程可参考 https://my.oschina.net/swchenml/blog/809791

在此安利一个redis桌面管理工具:redis desktop manager  https://redisdesktop.com/download

 三、tomcat配置

      所需jar包

     174200_RnYG_196698.png

   jar包下载地址:https://pan.baidu.com/s/1i53HLfJ

  下载最新的tomcat7: http://tomcat.apache.org/download-70.cgi

095635_8V79_196698.png

解压至文件夹,准备两个tomcat。

1、修改tomcat111   conf\server.xml  端口号 为8081 其他端口默认

095807_bfm4_196698.png

2、为避免tomcat222冲突   修改conf\server.xml  

server port="8006"和port="8082" 以及AJP port="8010"

100252_GQQW_196698.png

100311_btyi_196698.png

各自启动tomcat,测试是否正常运行。如若还是冲突,请为每个tomcat设置不同的jdk运行环境。

 

3、将上面所有的Jar包分别拷到两个Tomcat7安装目录下面的Lib目录下

4、在两个Tomcat 的conf/context.xml 文件里分别增加如下内容(或者在server.xml的context块中添加)

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
      <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
           host="localhost"
           port="6379"  
           database="0"
           maxInactiveInterval="60" />

6379是redis默认的对外访问端口号,maxInactiveInterval是session有效时间,单位s。

四、编写测试war包

eclipse编译好分别放置到两个tomcat的部署目录下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%
   // 获取session创建时间
   Date createTime = new Date(session.getCreationTime());
   // 获取最后访问页面的时间
   Date lastAccessTime = new Date(session.getLastAccessedTime());

   String title = "Tomcat1";
   Integer visitCount = new Integer(0);
   String visitCountKey = new String("visitCount");
   String userIDKey = new String("userID");
   String userID = new String("ABCD");

   // 检测网页是否由新的访问用户
   if (session.isNew()){
      title = "Tomcat1";
      session.setAttribute(userIDKey, userID);
      session.setAttribute(visitCountKey,  visitCount);
   } else {
	   visitCount = (Integer)session.getAttribute(visitCountKey);
	   visitCount += 1;
	   userID = (String)session.getAttribute(userIDKey);
	   session.setAttribute(visitCountKey,  visitCount);
   }
%>
<html>
<head>
<title>Session 跟踪</title>
</head>
<body>

<h1>Session 跟踪</h1>

<table border="1" align="center"> 
<tr bgcolor="#949494">
   <th>Session 信息</th>
   <th>值</th>
</tr> 
<tr>
   <td>id</td>
   <td><% out.print( session.getId()); %></td>
</tr> 
<tr>
   <td>创建时间</td>
   <td><% out.print(createTime); %></td>
</tr> 
<tr>
   <td>最后访问时间</td>
   <td><% out.print(lastAccessTime); %></td>
</tr> 
<tr>
   <td>用户 ID</td>
   <td><% out.print(userID); %></td>
</tr> 
<tr>
   <td>访问次数</td>
   <td><% out.print(visitCount); %></td>
</tr> 
</table> 
</body>
</html>

分别启动tomcat,各自访问不同端口号项目,可看到页面已经生成seesion,两个端口的值不相同。

102406_vc4y_196698.png

查看redis,发现session已成功存储到redis

102623_1acn_196698.png

102650_9kIP_196698.png

 

五、nginx集群配置

nginx的安装教程:https://my.oschina.net/swchenml/blog/808392

tomcat session manager 配置

修改D:\items\server\nginx-1.10.2\conf\nginx.conf ,启用 upstream 负载均衡 tomcat Cluster,默认使用轮询方式。

在#gzip  on;后添加

upstream tomcat  {  
        server localhost:8081;  
        server localhost:8082;
        ip_hash;		
    } 

修改

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            index  index.jsp index.html index.htm;
            add_header Cache-Control 'no-store';
			 client_max_body_size 100m;

			 proxy_set_header Host $http_host;

			 proxy_set_header X-Real-IP $remote_addr;

			 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

			# proxy_cache Z;

			 proxy_cache_valid 200 1m;

			 proxy_pass   http://tomcat;
        }

重启nginx:    net start/stop nginx 

六、测试

访问:http://localhost/testSession/  发现session进行了共享

104004_FQhz_196698.png

ok! 

参考资料:http://www.cnblogs.com/weixiaole/p/4431679.html

转载于:https://my.oschina.net/swchenml/blog/858458

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值