分布式架构中使用redis缓存实现session共享

session共享问题

1.为什么要做到session共享?

需要实现共享session的原因就在于,多个网站要使用同一份session数据,例如保存用户登录状态的session

在这里插入图片描述这时候,如果用户的登录请求在web服务器1进行,那么session就只存储在web1上面,如果哪次的请求在web2处理需要用户信息时候就会存在获取失败的问题。
使用reids缓存解决的原理
在这里插入图片描述

2.使用redis缓存实现session

下边贴一下我的redis的配置,可供参考

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
        <property name="maxIdle" value="0" />
        <property name="maxTotal" value="20" />
        <property name="maxWaitMillis" value="1000" />
        <property name="testOnBorrow" value="true" />
    </bean>

    <!-- redis连接配置,依次为主机ip,端口,是否使用池,(usePool=true)redis的池配置 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="192.168.127.142" p:port="6379" p:database="0" p:pool-config-ref="jedisPoolConfig">
    </bean>

    <!-- 配置spring-session -->
    <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <!-- 过期时间100分钟 -->
        <property name="maxInactiveIntervalInSeconds" value="6000"></property>
    </bean>
    <!--去掉redis client的CONFIG-->
    <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>

    <!-- string序列化bean -->
    <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>


    <!-- jdk序列化bean -->
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>

    <!-- genericjackjson 2json序列化 -->
    <bean id="genericJackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

    <!-- 配置一个操作的模板对象 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <!-- 配置key序列化的方式 -->
        <property name="keySerializer" ref="stringSerializer"/>
        <!-- 配置value的序列化方式 -->
        <property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/>
        <!-- hash的配置 -->
        <property name="hashKeySerializer" ref="stringSerializer"/>
        <property name="hashValueSerializer" ref="genericJackson2JsonRedisSerializer"/>
    </bean>

</beans>

最为web项目,那么他的根就是web.xml我们同样要在web.xml中处理一下session,将所有的session拦截进行过滤

<filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

3.实现的效果

所有服务器的session操作就可以从这里进行

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值