java session基于redis共享方案

session基于redis共享有两种基本的方案
1、基于容器自身的扩展,比如tomcat的session-manage,可以参考如下
地址进行配置https://github.com/jcoleman/tomcat-redis-session-manager
这个方案只适用tomcat容器,而且容器需要配置,这里不具体展开,有需要的可以参考上面的地址进行配置。但是这个方案有一个好处,可以适用于struts2和springmvc的场景,而且对springmvc没有版本要求。

2、基于spring-session的方案,spring-session的好处不仅仅是session共享,它还可以应用于多终端session共享,websocket,restful api等场景。下面具体说明一下
配置。特别说明spring-session是基于springmvc4.0以后的版本的,所以版本不匹配的就不能使用。

mave配置

<dependency>
        <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-data-redis</artifactId>
                <version>1.2.2.RELEASE</version>
                <type>pom</type>
        </dependency>
        <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.2.9.RELEASE</version>
        </dependency>

xml配置
spring.xml

    <bean id="redisHttpSessionConfiguration"  class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" >
        <property name="maxInactiveIntervalInSeconds" value="1800" />
    </bean>

    <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="192.168.17.24" />
        <property name="port" value="6379" />
    </bean>

web.xml

  <!-- 分布式Session共享Filter -->
  <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>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
  </filter-mapping>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that. Here is an example code for implementing Redis to share sessions in Java: ``` import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; public class RedisSessionManager { private static final String REDIS_HOST = "localhost"; private static final int REDIS_PORT = 6379; private static final int MAX_IDLE = 10; private static final int MAX_TOTAL = 30; private static final int MAX_WAIT_MILLIS = 10000; private static final int TIMEOUT = 1000; private static final String SESSION_PREFIX = "session:"; private static final Map<String, HttpSession> sessions = new HashMap<>(); private static JedisPool jedisPool = null; static { JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(MAX_IDLE); poolConfig.setMaxTotal(MAX_TOTAL); poolConfig.setMaxWaitMillis(MAX_WAIT_MILLIS); jedisPool = new JedisPool(poolConfig, REDIS_HOST, REDIS_PORT, TIMEOUT); } public static HttpSession createSession() { Jedis jedis = jedisPool.getResource(); String sessionId = SESSION_PREFIX + UUID.randomUUID().toString(); HttpSession session = new RedisHttpSession(sessionId, jedis); sessions.put(sessionId, session); return session; } public static HttpSession getSession(String sessionId) { HttpSession session = sessions.get(sessionId); if (session == null) { Jedis jedis = jedisPool.getResource(); session = new RedisHttpSession(sessionId, jedis); sessions.put(sessionId, session); } return session; } public static void removeSession(String sessionId) { sessions.remove(sessionId); Jedis jedis = jedisPool.getResource(); jedis.del(sessionId); jedis.close(); } } ``` This code uses the Jedis library to connect to Redis and store session data. It provides methods for creating, getting and removing sessions. The sessions are stored in a HashMap in memory for fast access, but are also persisted to Redis so they can be shared across multiple servers. I hope this helps!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值