我很黑! 发表于 2018-11-3 08:45:57

redis+session的共享

  Shrio+Redis实现tomcat集群session共享
  一、背景
  当我们使用了nginx做项目集群以后,就会出现一个很严重的问题亟待解决,那就是:tomcat集群之间如何实现session共享的问题,如果这个问题不解决,就会出现登陆过后再次请求资源依旧需要登陆的问题。这篇文章我们就解决这个问题。
  二、实现步骤
  说明:本篇是在spring+shiro集成的基础上进行改进的,如果不知道spring和shiro怎么集成,请移步:spring集成shiro做登陆认证
  1.在pom.xml中添加shiro-redis和jedis的依赖
  
  org.crazycake
  shiro-redis
  2.4.2.1-RELEASE
  
  
  redis.clients
  jedis
  2.7.2
  
  2.首先我们需要对redis进行集成,在resources下新建config.properties
  #redis pool config
  redis.pool.maxActive=200
  redis.pool.maxIdle=100
  redis.pool.maxWait=100
  redis.pool.testOnBorrow=true
  #redis config
  redis.host=192.168.85.129
  redis.port=6379
  redis.timeout=2000
  redis.password=123456
  redis.dbindex=8
  redis.default.expire=1800000
  3.在resources/spring文件夹下新建spring-redis.xml来集成redis操作客户端
  
  
  Redis configuration

    
  
  
  
  

    
  
  
  
  
  
  

    
  
  
  
  4.添加redisClient.java作为访问redis的客户端
  package com.hafiz.www.redis;
  import org.crazycake.shiro.RedisManager;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import redis.clients.jedis.Jedis;
  import redis.clients.jedis.JedisPool;
  import java.util.Set;
  /**
  * Desc: Jedis 操作客户端
  * Created by hafiz.zhang on 2017/7/21.
  */

  public>  private static final Logger LOGGER = LoggerFactory.getLogger(RedisClient.class);
  private static JedisPool jedisPool = null;
  public RedisClient(JedisPool jedisPool) {
  this.jedisPool = jedisPool;
  }
  public void init() {
  super.init();
  }
  @Override
  public byte[] get(byte[] key) {
  Jedis jedis = jedisPool.getResource();
  byte[] value;
  try {
  value = jedis.get(key);
  } catch (Exception e) {
  LOGGER.error("redis key:{} get value occur exception", new String(key));
  throw new RuntimeException("redis operation error:", e);
  } finally {
  jedis.close();
  }
  return value;
  }
  @Override
  public byte[] set(byte[] key, byte[] value) {
  Jedis jedis = jedisPool.getResource();
  try {
  jedis.set(key, value);
  Integer expire = getExpire();
  if(expire != 0) {
  jedis.expire(key, expire);
  }
  } catch (Exception e) {
  LOGGER.error("redis key:{} set value:{} occur exception", new String(key), new String(value));
  throw new RuntimeException("redis operation error:", e);
  } finally {
  jedis.close();
  }
  return value;
  }
  @Override
  public byte[] set(byte[] key, byte[] value, int expire) {
  Jedis jedis = jedisPool.getResource();
  try {
  jedis.set(key, value);
  if(expire != 0) {
  jedis.expire(key, expire);
  }
  } catch (Exception e) {
  LOGGER.error("redis key:{} set value:{} in expire:{} occur exception", new String(key), new String(value), expire);
  throw new RuntimeException("redis operation error:", e);
  } finally {
  jedis.close();
  }
  return value;
  }
  @Override
  public void del(byte[] key) {
  Jedis jedis = jedisPool.getResource();
  try {
  jedis.del(key);
  } catch (Exception e) {
  LOGGER.error("redis key:{} del value occur exception", new String(key));
  throw new RuntimeException("redis operation error:", e);
  } finally {
  jedis.close();
  }
  }
  @Override
  public void flushDB() {
  Jedis jedis = jedisPool.getResource();
  try {
  jedis.flushDB();
  } catch (Exception e) {
  LOGGER.error("redis flushDB occur exception");
  throw new RuntimeException("redis operation error:", e);
  } finally {
  jedis.close();
  }
  }
  @Override
  public Long dbSize() {
  Long dbSize = Long.valueOf(0L);
  Jedis jedis = jedisPool.getResource();
  try {
  dbSize = jedis.dbSize();
  } catch (Exception e) {
  LOGGER.error("redis get dbSize occur exception");
  throw new RuntimeException("redis operation error:", e);
  } finally {
  jedis.close();
  }
  return dbSize;
  }
  @Override
  public Set keys(String pattern) {
  Set keys = null;
  Jedis jedis = jedisPool.getResource();
  try {
  keys = jedis.keys(pattern.getBytes());
  } catch (Exception e) {
  LOGGER.error("redis get keys in pattern:{} occur exception", pattern);
  throw new RuntimeException("redis operation error:", e);
  } finally {
  jedis.close();
  }
  return keys;
  }
  }
  5.接着,我们对spring-shiro.xml做如下修改(红色标记的部分)
  
  
  Shiro Configuration
  

    
  
  
  

    
  
  

  
  

    
  
  
  

    
  
  
  
  

    
  
  
  
  
  
  

    
  
  
  
  
  /login.json = anon
  /logout.json = anon
  /js/** = anon
  / = authc
  /** = auth
  
  
  
  
  6.spring.xml做如下修改
  
  
  
  
  
  到此我们就完了shiro+redis实现session共享的问题,其实也很简单,其中的实现逻辑也很简单,就是shiro的拦截器会首先去redis里面获取session,作为当本次请求的session.
  其他代码以及简单测试代码不再贴出,给出github地址:https://github.com/hafizzhang/shiro-session-cluster.git
  三、总结
  通过本文,我们就完成了spring+shiro+redis实现集群session共享的问题,经过亲测可行。但有一点遗憾的地方,就是每次请求至少会有8次redis的读操作,一次写操作,这个问题还没有找到很好的解决办法,本地使用ehcahe也不太现实,因为涉及到本地缓存和redis缓存同步的问题,如果你有好的办法,欢迎讨论交流学习!

页: [1]
查看完整版本: redis+session的共享