参考:http://www.cnblogs.com/lengfo/p/4260363.html
近期为CAS-SSO准备搭建Session共享,小编研究了下基于Nginx负载均衡,分布式Tomcat+Redis 存储Session,实现分布式Tomcat的Session共享,for window下~~~不要问我为什么window下(linux的资源太多了~~~)
原理:
首先,sessionID的产生,当用户发起请求时,web服务器将产生一个sessionID返回给用户,由浏览器cookie记录该sessionID,下次再请求,浏览器将cookie记录的sessionID一并发给web服务器,web服务器判断请求头中含有sessionID,则不再新建sessionID,也说明该用户已登录or已访问.........
注:浏览器发送的请求中有个Host字段,标记请求的目标服务器,即使访问的应用是相同的,但由于不同的目标请求服务器,产生并返回的sessionID肯定不一样的....所以这里必须有负载均衡器,如Nginx or Haproxy来做转发,确保请求的目标服务器都指向负载均衡那台,然后通过cookie=sessionID,发送给web服务器,获取对应的session内容。
其次,采用Redis做session缓存服务器,将原本tomcat自身存储的session,转移存储到redis缓存服务器上,实现分布式的tomcat共享session。
1.下载Nginx for winow(linux的请无视)
2.下载tomcat7
3.下载redis for window64 只有64bit,不要问为什么没有32位(linux的请无视)
4.下载tomcat7依赖redis 必须的jar tomcat-redis-session-manager-1.2-tomcat-7.jar、jedis-2.1.0.jar、commons-pool-1.6.jar 注意版本必须配对..jedis-2.1对应commons-pool-1.6;
详见附件
5.配置nginx.conf,红色部分为新增修改地方
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { #采用轮询方式,配置对应的tomcat访问,“site”可以自定义 upstream site { server 10.89.113.112:8080 weight=1; server 10.92.2.23:8091 weight=1; } 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; server { listen 80; #server_name 用于访问Nginx的url,可以用域名如“www.xxx.com” 自定义 server_name 10.89.113.184; #charset koi8-r; #access_log logs/host.access.log main; #指定web上下文 location /web-one { #root html; #index index.html index.htm index.jsp; #proxy_pass "site"必须同upstream 配置的名称一致 proxy_pass http://site; #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
2.配置redis.windows.conf ,搜索maxheap 并新增maxheap 1024000000,可以写bat启动脚本startup.bat
redis-server redis.windows.conf
3.新建web-app,修改index.jsp,新增打印sessionId,用于判断是否session共享成功
<html> <body> <h2>Hello World!! web-one-tomcat.1 </h2> SessionID:<%=session.getId()%> </body> </html>
4. 将tomcat7依赖的redis 相关的jar tomcat-redis-session-manager-1.2-tomcat-7.jar、jedis-2.1.0.jar、commons- pool-1.6.jar copyto “lib”文件夹下
5.修改“conf”文件夹下的context.xml,将原先的<Manager>内容注释掉;新增以下内容
<!--redis config-->
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<!-- host: optional: defaults to "localhost" -->
<!-- port: optional: defaults to "6379" -->
<!-- database: optional: defaults to "0" -->
<!-- maxInactiveInterval:optional: defaults to "60" (in seconds) -->
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="10.89.113.112" <!-- host: optional: defaults to "localhost" -->
port="6379" <!-- port: optional: defaults to "6379" -->
database="0"<!-- database: optional: defaults to "0" -->
maxInactiveInterval="180" /> <!-- maxInactiveInterval:optional: defaults to "60" (in seconds) -->
6.依次按照,启动redis、tocmat、nginx~~
7.访问http://10.89.113.184/web-one <---nginx所配置的访问地址,将自动负载路由到不同的tomcat上,根据现实出相同的sessionID,实现session间共享;
相关推荐
自己做的一个nginx+tomcat+redis完成session共享小例子,包括所需jar,适合初学者,希望大家一起学习交流
Nginx+tomcat+redis软负载均衡需要的一整套资源,还包括了JDK,依赖包等文件
包含的jar包如下: commons-pool2-2.0.jar jedis-2.5.2.jar tomcat-juli-adapters.jar tomcat-juli.jar tomcat-redis-session-manager1.2.jar
redis缓存服务器Nginx+Tomcat+redis+MySQL实现session会话共享
Nginx+Tomcat+Redis实现session共享,通过Nginx作为前端的负载,把请求分发到后端的Tomcat服务器上,提高并发数;但是单纯的通过Nginx的ip_hash负载是很多问题的。只要用户一切换网络或者后端Tomcat主机宕机session就...
Nginx+Tomcat+Redis实现负载均衡过程中session所需架包
最近研究session共享,弄了半天终于搭建起来了,基于nginx+tomcat+redis,实现session共享,这里分享给大家,希望能够你能够为己所用。
Nginx+Tomcat+Redis实现服务器负载均衡,和Session共享
keepalived+nginx+tomcat+redis+mysql所需的jdk包,之前csdn不让上传超过200m得只能分开上传。
目前只支持tomcat8.5,其它版本的tomcat还没测试,自己配置过很多nginx+tomcat集群+redis实现session共享的系统,经过仔细总结得出这些,可以说是知识的结晶,欢迎大家来下载,不好的地方可以多提,有问题看到的话会...
springboot +shiro+redis实现session共享(方案二)1
超详细Nginx+Tomcat+Redis搭建高性能负载均衡集群Session共享搭建说明,按文档步骤可轻松搭建并实现session共享
nginx+tomcat6+redis实现session共享Jar文件 配置流程可参考http://blog.csdn.net/llgyzb/article/details/56835283
ssm+redis+nginx实现session共享和负载均衡,大家一起来研究吧
nginx+tomcat+redis集群部署环境,包括nginx tomcat redis及集群部署所需jar包,jedis-2.6.1.jar,commons-pool2-2.2.jar,tomcat-redis-session-manager1.2.jar 具体步骤可以参见我的博文
国产中标麒麟操作系统nginx+tomcat+redis+mysql安装部署手册(CSDN).zip
一个稳定Web服务器架构必须实现高可用与负载均衡。这里配置Keepalived + Nginx + Tomcat + Redis的架构,其中:keepalived用于管理Virtual IP,与nginx一起...后端使用Tomcat管理web服务,并利用Redis实现session共享。
tomcat+redis+nginx实现session共享所需jar包,包含commons-pool2-2.4.2.jar、jedis-2.8.0.jar、tomcat-redis-session-manager-2.0.0.jar
Nginx+Tomcat+Redis实现应用服务器集群负载均衡和Session共享所需要的所有jar包,包括如下jar包:commons-logging-1.2.jar,commons-pool2-2.6.0.jar,jedis-2.9.0.jar,tomcat-juli-7.0.90.jar,tomcat-redis-...