redis+tomcat8.5.x

使用开源方案:redisson

github地址:

https://github.com/redisson/redisson/tree/master/redisson-tomcat

redisson.conf 文件配置:

https://github.com/redisson/redisson/wiki/2.-Configuration#26-single-instance-mode

 

使用方法:

第一步:下载对应的jdk、tomcat版本jar包,一共两个:redisson-all-VERSION.jar、redisson-tomcat-VERSION.jar

第二步:将下载好的jar包上传到tomcat的lib目录下: /usr/local/src/tomcat8/lib

第三步:在/usr/local/src/tomcat8/conf/context.xml 配置redisson.conf 文件目录

configPath="${catalina.base}/conf/redisson.conf" readMode="REDIS" updateMode="DEFAULT"/>

 

备注:配置属性里的readMode和updateMode需要注意(选用redis存储session时,需要将readMode改为REDIS):

readMode - read attributes mode. Two modes are available:

  • MEMORY - read attributes stored in local Tomcat Session. Default mode.
  • REDIS - read directly from Redis.

updateMode - attributes update mode. Two modes are available:

  • DEFAULT - session attributes are stored into Redis only through setAttribute method. Default mode.
  • AFTER_REQUEST - all session attributes are stored into Redis after each request.

第四步:配置redisson需要的数据信息 redisson.conf (参考redisson的wiki文档,我使用的单节点模式,找到相应的配置内容):

{
   "singleServerConfig":{
      "address": "redis://127.0.0.1:6379",
      "password":"****your password***",
      "database":0,
      "idleConnectionTimeout":10000,
      "pingTimeout":1000,
      "connectTimeout":10000,
      "timeout":3000,
      "retryAttempts":3,
      "retryInterval":1500,
      "subscriptionsPerConnection":5,
      "clientName":null,
      "subscriptionConnectionMinimumIdleSize":1,
      "subscriptionConnectionPoolSize":50,
      "connectionMinimumIdleSize":32,
      "connectionPoolSize":64,
      "dnsMonitoringInterval":5000
   },
   "threads":0,
   "nettyThreads":0,
   "codec":{
      "class":"org.redisson.codec.JsonJacksonCodec"
   },
   "transportMode":"NIO"
}

第五步:重启tomcat后就可以看到redis里有redisson为key的session缓存数据了

 

参考文章:https://yq.aliyun.com/articles/597340

你可能感兴趣的:(java,tomcat,redis)