如下均为看了别人博客补充出来的步骤记录。没有太多原理上的说明html
session同步的方法是配置tomcat,配合redis目的。显然这种方法是入门级的。java
实际上我的认为更专业的途径应该是要在代码层面控制,就是经过spring整合redisnginx
准备redis
1.tomcat7 * 3spring
2.nginx 1.7.2tomcat
3.redis 2.8.9session
配置tomcatapp
在一台机器上运行3个tomcat:jsp
(1).修改http访问端口(默认为8080端口,这里分别改8081 8082 8083): tcp
<Connector port=”8080” protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ URIEncoding=”gb2312″/>
(2).修改Shutdown端口(默认为8005端口,这里分别改8005 8006 8007):
<Server port=”8005” shutdown=”SHUTDOWN”>
(3).修改JVM启动端口(默认为8009端口,这里分别改8009 8010 8011):
<Connector port=”8009” protocol=”AJP/1.3″ redirectPort=”8443″ />
若是仍是报端口占用错误
在startup.bat与catalina.bat中开始加入
set CATALINA_HOME=E:\fuzai\tomcat3(每一个tomcat的位置不同)
使用redis管理session,修改tomcat文件夹中conf/context.xml文件,在context节点下添加以下配置:
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" /> <Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />
这里须要3个jar,都要放到tomcat lib目录(这三个jar,须要注意版本的问题,这里提供的版本是没问题的)
http://download.csdn.net/detail/chibangyan/8479051
配置nginx
修改nginx文件目中的conf/nginx.conf文件为:
#user nobody; worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream localhost { server localhost:8081 weight=1; server localhost:8082 weight=2; server localhost:8083 weight=3; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://localhost; proxy_set_header X-Real-IP $remote_addr; client_max_body_size 100m; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
分别启动redis、nginx和三台tomcat。
在tomcat下,分别加入一个jsp,测试session 是否同步
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>shared session</title> </head> <body> <br>session id=<%=session.getId()%> <br>tomcat 3 </body> </html>
若是发现三个tomcat 123 的sessionID都是一致的,证实成功了