网址:https://github.com/jcoleman/tomcat-redis-session-manager
同样修改 tomcat 的 conf 目录下的 context.xml 文件:
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60"/>
以上是以 1.2 版为例子,需要用的 jar 包:
tomcat-redis-session-manager-1.2-tomcat-6.jar
jedis-2.1.0.jar
commons-pool-1.6.jar
来自:http://www.cnblogs.com/interdrp/p/4056525.html
在Apache Tomcat 7设置redis作为session store
redis已经有组件支持直接在tomcat7中设置下将redis作为tomcat默认的session存储器,下面介绍下配置过程
1.从http://redis.io/下载redis,按照redis服务端
1
2
3
4
|
wget http:
//download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
|
2.启动redis
1
2
|
cd RedisDirectory/src
./redis-server --port
6379
|
3.从http://tomcat.apache.org/download-70.cgi下载tomcat7,按照tomcat7
4.从https://github.com/xetorthio/jedis/downloads下载jedis作为java的redis客户端,从https://github.com/jcoleman/tomcat-redis-session-manager/downloads下载tomcat的redis session manager插件,从http://commons.apache.org/proper/commons-pool/download_pool.cgi下载apache的common pool包,将这几个jar包拷贝到tomcat7的lib目录下
5.修改tomcat的conf下的context.xml文件,添加或者修改下面的配置
1
2
3
4
5
6
|
>Valve className=
"com.radiadesign.catalina.session.RedisSessionHandlerValve"
/>
>Manager className=
"com.radiadesign.catalina.session.RedisSessionManager"
host=
"localhost"
<!-- optional: defaults to
"localhost"
-->
port=
"6379"
<!-- optional: defaults to
"6379"
-->
database=
"0"
<!-- optional: defaults to
"0"
-->
maxInactiveInterval=
"60"
<!-- optional: defaults to
"60"
(in seconds) --> />
|
6.重启tomcat后就可以看到session存储到redis上了。
英文原文:http://shivganesh.com/category/database/no-sql/redis-no-sql/
来自:http://www.javaarch.net/jiagoushi/1024.htm
一、nginx+tomcat+memcached (依赖包下载)
1.memcached配置:(v1.4.13)
节点1(192.168.159.131:11444)
节点2(192.168.159.131:11333)
2.tomcat配置
tomcat1(192.168.159.128:8081)
tomcat2(192.168.159.128:8082)
3.nginx安装在192.168.159.131。
首先,是配置tomcat,使其将session保存到memcached上。有两种方法:
方法一:在server.xml中配置。
找到host节点,加入
- <Context docBase="/var/www/html" path="">
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.159.131:11444 n2:192.168.159.131:11333"
- requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
- sessionBackupAsync="false" sessionBackupTimeout="3000"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- copyCollectionsForSerialization="false" />
- </Context>
其次,配置nginx,用于测试session保持共享。方法二:在context.xml中配置。
找到Context节点,加入
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.159.131:11444"
- requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
- sessionBackupAsync="false" sessionBackupTimeout="3000"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- copyCollectionsForSerialization="false" />
- upstream xxy.com {
- server 192.168.159.128:8081 ;
- server 192.168.159.128:8082 ;
- }
- log_format www_xy_com '$remote_addr - $remote_user [$time_local] $request '
- '"$status" $body_bytes_sent "$http_referer"'
- '"$http_user_agent" "$http_x_forwarded_for"';
- server
- {
- listen 80;
- server_name xxy.com;
- location / {
- proxy_pass http://xxy.com;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- access_log /data/base_files/logs/www.xy.log www_xy_com;
- }
最后,将你的应用放到两个tomcat中,并依次启动memcached、tomcat、nginx。访问你的nginx,可以发现两个tomcat中的session可以保持共享了。
二、nginx+tomcat+redis (依赖包下载)
1.redis配置(192.168.159.131:16300)(v2.8.3)
2.tomcat配置
tomcat1(192.168.159.130:8081)
tomcat2(192.168.159.130:8082)
3.nginx安装在192.168.159.131。
首先,是配置tomcat,使其将session保存到redis上。有两种方法,也是在server.xml或context.xml中配置,不同的是memcached只需要添加一个manager标签,而redis需要增加的内容如下:(注意:valve标签一定要在manager前面。)
- <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
- <Manager className="com.radiadesign.catalina.session.RedisSessionManager"
- host="192.168.159.131"
- port="16300"
- database="0"
- maxInactiveInterval="60"/>
其次,配置nginx,用于测试session保持共享。
- upstream redis.xxy.com {
- server 192.168.159.130:8081;
- server 192.168.159.130:8082;
- }
- log_format www_xy_com '$remote_addr - $remote_user [$time_local] $request '
- '"$status" $body_bytes_sent "$http_referer"'
- '"$http_user_agent" "$http_x_forwarded_for"';
- server
- {
- listen 80;
- server_name redis.xxy.com;
- location / {
- proxy_pass http://redis.xxy.com;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- access_log /data/base_files/logs/redis.xxy.log www_xy_com;
- }
最后,将你的应用放到两个tomcat中,并依次启动redis、tomcat、nginx。访问你的nginx,可以发现两个tomcat中的session可以保持共享了。
来自:http://blog.csdn.net/fu9958/article/details/17325563
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="127.0.0.1"
port="6379"
password="123456"
database="0"
maxInactiveInterval="30"/>