如下均爲看了別人博客補充出來的步驟記錄。沒有太多原理上的說明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都是一致的,證實成功了