1.环境准备(安装redis报错)
nginx,两个tomcat,redis (因为测试全部安装在同一台)
安装redis:
cd redis-4.0.9.tar.gz && cd redis-4.0.9 && make && cd src && make test
#make test的是报错1如下:
You need tcl 8.5 or newer in order to run the Redis test
make: *** [test] Error 1解决:yum -y install tcl
#报错2如下:
[exception]: Executing test client: NOREPLICAS Not enough good slaves to write..
NOREPLICAS Not enough good slaves to write.解决:
vim tests/integration/replication-2.tcl
- after 1000
+ after 10000
继续:make test && make install
启动redis :./redis-server
2.测试文件(在tomcat的webapps下面新建一个项目,两个tomcat项目名字一样,文件也一样,因为测试,访问的文件内容(最好加上session id)设置不一样看效果
3.tomcat配置
添加支持redis会话共享的插件(3个jar包)到tomcat/lib目录下
然后更改tomcat/conf下context.xml文件【context】节点下加如下代码【两个tomcat配置文件都要改下】:
注:这是配置redis的链接信息,如果没有密码可以把passowrd项去掉 ,tomcat7 ,和tomcat8 对应支持resissession的jar也不一样(有三个),tomcat7 的插件在网上容易找到,现在网上插件(jar包)不支持tomcat8,能用的都是改过源码的,比较难找(本人暂时没有找到可以用的)----------------------针对tomcat7的配置----------------------------
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="127.0.0.1"
port="6379"
database="0"
password="666"
maxInactiveInterval="60" />
-------------------------针对tomcat8的配置-----------------------
<Valve className="com.demo.redis_session.RedisSessionHandlerValve" />
<Manager className="com.demo.redis_session.RedisSessionManager"
host="127.0.0.1" port="6379" database="0" password="666" maxInactiveInterval="60" />
4.nginx配置 (附上主要代码)
cat /etc/nginx/conf.d/default.confupstream images_server {#ip_hash;server 192.168.110.28:8080;server 192.168.110.28:8081;}server {listen 80;server_name vm1.ql.com;location / {root /usr/share/nginx/html;index index.html index.htm;}location ~*/NginxTest/ {proxy_pass http://images_server;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}
5.结果验证
浏览器本地访问:(本地hosts加上域名解析)
http://vm1.ql.com/NginxTest/index.jsp
不断刷新浏览器,显示页面内容不一样,但session-id一直,则可说明已经实现了会话共享。