Nginx - session共享

1.负载均衡 session问题

通过负载均衡,我们可以把请求分发到不同的 Tomcat 来缓解服务器的压力,但是这里存在一个问题: 当同一个用户第一次访问tomcat_8111 并且登录成功, 而第二次访问却被分配到了tomcat_8222, 这里并没有记录他的登陆状态,那么就会呈现未登录状态了,严重伤害了用户体验。

在这里插入图片描述

2.解决办法

2.1 ip_hash

通过ip地址标记用户,如果多次请求都是从同一个ip来的,那么就都分配到同一个tomcat.
这样就不会出现负载均衡 session问题了. 处理手段也很简单,如图所示在upstream最后加上ip_hash;就行了。

不过这种方案并不完美,当如下几种情况发生时就有问题:

  1. 大量请求来之某个局域网,那么相当于就没有负载均衡了
  2. 如果tomcat_8111 挂了,那么此时nginx只能把请求交给tomcat_8222,但是这里却没有记录session,用户体验依然受影响。

在这里插入图片描述

2.2 redis+tomcat-sessoin-manager

既然第一种解决办法有问题,那么就采用第二种解决办法:用Redis来存取session.
Redis是什么呢?说简单点就是个独立的Hashmap,用来存放键值对的。
这样当tomcat1需要保存session值的时候,就可以把它放在Redis上,需要取的时候,也从Redis上取。
那么考虑这个情景:

  1. 用户提交账号密码的行为被分配在了tomcat8111上,登陆信息被存放在redis里。
  2. 当用户第二次访问的时候,被分配到了tomcat8222上
  3. 那么此时tomcat8222就会从redis去获取相关信息,一看有对应信息,那么就会呈现登陆状态。

这样就规避了解决办法一: ip_hash里会出现的两种问题了。

在这里插入图片描述

2.2.1 启动redis

2.2.2 往tomcat中添加jar包

Tomcat需要链接 redis,所以需要专门的jar包。 一共有3个jar包:
jedis-2.5.2.jar,
commons-pool2-2.0.jar,
tomcat-redis-session-manager1.2.jar。

下载解压后,放在tomat8111的lib目录下。注:不要放在webapp里面去了哦
下载解压后,放在tomat8222的lib目录下。注:不要放在webapp里面去了哦

两个tomcat都要放

在这里插入图片描述

2.2.3 修改context.xml

然后修改tomcat/conf/context.xml ,增加下面这坨东西

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
  <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
   host="127.0.0.1"  
   port="6379"  
   database="0"  
   maxInactiveInterval="60" /> 
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<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="127.0.0.1"  
   port="6379"  
   database="0"  
   maxInactiveInterval="60" /> 

</Context>

2.2.4 重启tomcat

两个tomcat都要重启

2.2.5 测试

Redis session共享机制和nginx其实无关,是发生在nginx之后的事情,所以直接访问login.jsp,然后登陆,并观察到已登陆状态

http://127.0.0.1:8111/login.jsp

在这里插入图片描述

然后直接访问tomcat8222

http://127.0.0.1:8222/login.jsp

虽然没有在tomcat8222上登陆,但是可以观察到已经呈现为登陆状态了

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值