486、Java工具和中间件06 -【Nginx - Sesion共享】 2021.03.08

目录

0、负载均衡 session问题

1、解决办法一: ip_hash

2、解决办法二: redis+tomcat-sessoin-manager

3、接下来进行具体操作

4、启动redis

5、jar包

6、修改context.xml

7、重启tomcat

8、测试访问tomcat8111

9、测试访问tomcat8222

10、下载已经配置好了的tomcat

11、参考链接


 

0、负载均衡 session问题

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

负载均衡 session问题

1、解决办法一: ip_hash

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

不过这种方案并不完美,当如下几种情况发生时就有问题:
1. 大量请求来之某个局域网,那么相当于就没有负载均衡了
2. 如果tomcat_8111 挂了,那么此时nginx只能把请求交给tomcat_8222,但是这里却没有记录session,用户体验依然受影响。

解决办法一: ip_hash

 

2、解决办法二: redis+tomcat-sessoin-manager

既然第一种解决办法有问题,那么就采用第二种解决办法:用Redis来存取session.
Redis是什么呢?说简单点就是个独立的Hashmap,用来存放键值对的。
这样当tomcat1需要保存session值的时候,就可以把它放在Redis上,需要取的时候,也从Redis上取。
那么考虑这个情景: 
1. 用户提交账号密码的行为被分配在了tomcat8111上,登陆信息被存放在redis里。 
2. 当用户第二次访问的时候,被分配到了tomcat8222上
3. 那么此时tomcat8222就会从redis去获取相关信息,一看有对应信息,那么就会呈现登陆状态。

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

注: 对 redis 不熟悉的同学,请先了解下 redis教程

解决办法二: redis+tomcat-sessoin-manager

3、接下来进行具体操作

接下来进行具体操作,分几个步骤
1. 启动redis
2. 给两个tomcat使用jar包
3. 配置两个tomcat
4. 重启两个tomcat
5. 测试

4、启动redis

下载右上角的redis-2.4.5-win32-win64.zip,解压后运行对应(32位或者64位)的redis-server.exe程序

启动redis

5、jar包

Tomcat需要链接 redis,所以需要专门的jar包,这些包都放在了右上角的tomcat-redis.rar里。 一共有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都要放

jar包

6、修改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" />


两个tomcat都要改

<?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>

7、重启tomcat

两个tomcat都要重启

8、测试访问tomcat8111

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

http://127.0.0.1:8111/login.jsp

测试访问tomcat8111

9、测试访问tomcat8222

然后直接访问tomcat8222

http://127.0.0.1:8222/login.jsp


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

测试访问tomcat8222

10、下载已经配置好了的tomcat

如果对上述tomcat配置办法吃不准,请直接使用右上角的已经配置好了的:tomcat-redis-8111-8222.rar

11、参考链接

[01] How2j - 工具和中间件 - Nginx - Session共享

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值