Build redis on redis server 192.168.50.140 , the default port of redis is 6379
Redis build:
redis depends on gcc , install it first:
1 |
yum install -y gcc-c++ |
Download redis , I am using redis-3.2.1.tar.gz , upload it to linux /usr/local/redis-src/ , unzip it
Enter the decompressed directory redis-3.2.1 , execute the make command to compile
Install to the directory /usr/local/redis
implement:
1 |
make PREFIX=/usr/local/redis install |
After the installation is complete, copy the redis configuration file to the installation directory, redis.conf is the redis configuration file, redis.conf is in the redis source directory , and the port defaults to 6379 .
Excuting an order:
1 |
cp /usr/local/redis-src/redis-3.2.1/redis.conf /usr/local/redis/ |
Start and stop redis in the redis installation directory :
start up:
1 |
./bin/redis-server ./redis.conf |
This startup method is called front-end startup and must remain in the current window. If ctrl + c exits, then redis also exits. It is not recommended to use
Then the backend starts:
First modify the value of daemonize in redis.conf , open it and you can see that the default is no , modify it to daemonize yes , and start it. You can also modify the default port 6379 of redis to other values in this configuration file.
Close redis :
1 |
./bin/redis-cli shutdown |
At this point, the redis server is built.
Tomcat integrates with redis to achieve session sharing:
If the environment is tomcat7 + jdk1.6 :
In the tomcat directory of all servers that need to share sessions :
Add the following five jar packages to the lib directory . Note that the version should be the same, otherwise errors are prone to occur. The following tests are available:
Add to content.xml in the conf directory : configure the redis service
1 2 3 4 5 6 |
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve"/> <Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="192.168.50.140" port="6379" database="0" maxInactiveInterval="60" /> |
If the environment is tomcat7 + jdk1.7 or 1.8 :
In the tomcat directory of all servers that need to share sessions :
Add the following three jar packages to the lib directory, and the test passes:
Add to content.xml in the conf directory : configure the redis service
1 2 3 4 5 6 |
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="192.168.50.140" port="6379" database="0" maxInactiveInterval="60"/> |
根据我这测试,是jkd1.8+tomcat7,在137和139两台tomcat中加入jar包且进行如上配置:
上传jar包
修改content.xml
启动redis服务,重新启动所有tomcat,启动nginx,刷新nginx页面,两台tomcat页面可以看到sessionid值不变,关闭某台tomcat,nginx中sessionid不变,说明session是共享的。
问题:
注意开通防火墙6379端口
有可能此时访问会报错,redis无法访问,这是由于redis的安全机制,默认只有127.0.0.1才能访问,在redis.conf中可以找到bind 127.0.0.1,你可以将此ip改为访问者ip,
如果有多个访问者,也可以把bind 127.0.0.1注释掉,然后在配置文件中找到protected-mode,修改protected-mode yes改为protected-mode no 关闭redis保护模式即可
详细可以参考这:http://www.cnblogs.com/liusxg/p/5712493.html
经过大牛指点:添加两个注意点:
1.按照如上配置,使用redis数据库,放入session中的对象必须要实现java.io.Serializable接口,使用memcache的可以不用实现Serializable接口
原因是:因为tomcat里使用的将session放置redis使用的工具类,是使用的jdk序列化模式存储的,这一点也是很容易理解的,session.setAttribute(String key, Object value),存储Object类型
object放入redis中又要能取出来,只能是序列化进行存储了,然后取出的时候进行反序列化。
所以我们在session中存储的任何对象,都必须实现序列化接口。
2.按照如上配置,使用redis做session存储空间时,web应用的session-time的时间单位会变成[秒],而不是原本的[分]
原因是:因为tomcat里使用的将session放置redis使用的工具类,在存储时为对tomcat容器时间做转换,
在redis中设置过期时间是使用秒作为单位的,有个命令叫expire可以设置redis键值过期时间,所以在context.xml配置文件中我们需要制定session过期时间(默认是60秒,配成1800即30分钟),这一点很重要。
请注意!!!!
context.xml配置说明:
1 2 3 4 5 6 7 8 9 10 |
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" //这里是redis服务器地址 host="192.168.50.140" //这里是redis端口,redis默认端口是6379 port="6379" //这里是redis数据库中的标识,标识第0个,默认使用0即可 database="0" //需要注意的是这里由于redis过期时间默认设置为60,单位是秒,session过期时间为30分钟,所以需要设置为1800对应30分钟 maxInactiveInterval="1800"/> |